Iskripthi seBash-Molo Molo weHlabathi

Kule tutorial, sifunda indlela yokwenza kunye nokuqhuba iskripthi seBash esilula esiprinta 'Molo weHlabathi'.



Yintoni uShebang (#!)

Iskripthi ngasinye seShell siqala ngeShebang #! iisimboli. Oku kubhengezwe kumgca wokuqala weskripthi kwaye ngokusisiseko uxelela iqokobhe ukuba yeyiphi itoliki eza kusetyenziswa ukuhambisa iskripthi.

#!/bin/bash ...

Molo kwiHlabathi leBhash

Ngoku, siza kwenza i-bash script eprinta amagama athi 'Molo weHlabathi' kwisiphelo sendlela.


Kuqala yenza ifayile ebizwa hello_world.sh |. Sebenzisa i-terminal esiyibhalayo:

$ touch hello_world.sh

Ifayile yethu ngoku yenziwe.


Emva koko, vula ifayile kumhleli wakho owuthandayo; Ndisebenzisa i-nano, ke iya kuba:

$ nano hello_world.sh

Nje ukuba hello_world.sh ivuliwe kumhleli wakho, chwetheza le miyalelo ilandelayo:

#!/bin/bash echo 'Hello World'

Yenza iSkripthi seShell

Ngoku ukuze siprinte hello world, kufuneka sisebenzise iskripthi esasenzayo.

Zininzi iindlela zokwenza iskripthi


$ sh ./hello_world.sh ## incorrect $ ./hello_world.sh ## correct $ bash ./hello_world.sh ## correct

Indlela yokuqala ayichanekanga, kuba uxelela iqokobhe ukuba lisebenzise itoliki yeqokobhe hayi itoliki yetash.

Indlela yesibini ichanekile kuba sisebenzisa iskripthi esiya kuthi sisebenzise itoliki echaziweyo kwifayile, umgca wokuqala kwiskripthi esiyi _ _ + _ |.

Indlela yesithathu ichanekile kuba kule meko, sisebenzisa itoliki ye-bash efanayo nale ichazwe kwifayile.

Imvume iyaliwe xa kusenziwa iskripthi seShell

Ukuba uzama ukusebenzisa iskripthi sakho usebenzisa:


#!/bin/bash

Uya kubona impazamo yokufumana imvume. Kungenxa yokuba iskripthi asinayo imvume.

Ungasinika iskripthi ukuba siphumeze imvume ngokusebenzisa:

$ ./hello_world.sh -bash: ./hello_world.sh: Permission denied

Ngoku, ukuba uqhuba iskripthi kwakhona, uya kubona 'Molo weHlabathi' eprintiweyo:

$ chmod +x ./hello_world.sh