Uyenza njani kwaye ubize imisebenzi kwiBhash

Isikhokelo esikhawulezayo sendlela yokwenza kunye nokufowuna imisebenzi eBhash.

Umsebenzi ibhlokhi yekhowudi enokuphinda isetyenziswe ukwenza isenzo esithile. Ngemisebenzi, sifumana imodareyitha engcono kunye nenqanaba eliphezulu lokusetyenziswa kwakhona kwekhowudi.

I-Bash ibonelela ngemisebenzi eyakhelweyo ngaphakathi njenge echo kunye read, kodwa sinokwenza imisebenzi yethu.




Ukwenza uMsebenzi kwiBhash

Zimbini iindlela esinokwenza ngayo imisebenzi eBhash:

Enye indlela kukusebenzisa igama lomsebenzi, o.k.t.


functionName(){ // scope of function }

Uhlobo oluhlanganisiweyo:

functionName(){ echo 'hello'; }

Enye indlela kukubhengeza umsebenzi usebenzisa function igama elingundoqo:

function functionName { // scope of function }

Uhlobo oluhlanganisiweyo:

function functionName { echo 'hello'; }

Qaphela ukuba asiyifuni njani i - _ + _ | xa usebenzisa () igama eliphambili lokwenza umsebenzi.


Amanqaku abalulekileyo ekufuneka uwaqaphele malunga nemisebenzi yeBash:

  • Ikhowudi phakathi kwee-brace curly function ngumzimba womsebenzi kunye nomda
  • Xa ubiza umsebenzi, sisebenzisa igama lomsebenzi naphina kwiskripthi se-bash
  • Umsebenzi kufuneka uchazwe ngaphambi kokuba usetyenziswe
  • Xa usebenzisa ingxelo ehambelanayo, umyalelo wokugqibela kufuneka ube semicolon {}

Umzekelo:

Le khowudi ilandelayo yenza umsebenzi oprinta 'Molo weHlabathi' kwikhonsoli. Igama lomsebenzi ubiziwe printHero :

;

Ukubiza uMsebenzi kwiBhash

Siwubiza njani lo msebenzi ungasentla? Konke okufuneka ukwenze kwiskripthi sakho se-bash kukubhala igama lomsebenzi kwaye uya kubizwa.


Umzekelo:

#!/bin/bash printHello(){
echo 'Hello World!' }

Iziphumo:

#!/bin/bash printHello(){
echo 'Hello World!' } # Call printHello function from anywhere in the script by writing the name printHello


Ukupasa Iingxoxo

Umsebenzi ongasentla 'Hello World' ayinazo iiparameter. Nanini na xa siyibiza, sifumana iziphumo 'Molo weHlabathi'. Kodwa kuthekani ukuba sifuna ukwenza umsebenzi oqhelekileyo? Umzekelo singabiza umsebenzi ngengxoxo ethile kwaye iya kuprinta into esiyithumela kuyo.

Zimbini iindlela zokwenza oku.


Kuqala singaguqula printHello() sebenza ukuprinta iimpikiswano ezidluliselwe kuyo:

Umzekelo:

printHello()

Iziphumo:

#!/bin/bash printAny(){
echo 'Hello ' $1 } printAny World printAny DevQa printAny I love coding!

Qaphela ukuba ingxelo yesithathu yokuprinta Hello World Hello DevQA Hello I kukhutshwe kuphela 'Molo, Ndi'.


Kungenxa yokuba umsebenzi wethu wenzelwe ukuthatha kuphela iparameter eli-1 printAny I love coding!. Igama 'Ndiyakuthanda ukufaka iikhowudi!' ngenene ziiparamitha ezi-3.

Ukuba sifuna ukuyiprinta yonke kuya kufuneka sibeke iikowuti malunga nesicatshulwa

Umzekelo:

$1

Iziphumo:

#!/bin/bash printAny(){
echo 'Hello ' $1 } printAny 'I love coding!'

Omnye umzekelo, sinokudlula ngamanani ngokunjalo:

Hello I love coding

Iziphumo:

#!/bin/bash add() {
result=$(($1 + $2))
echo 'Result is: $result' } add 1 2


Amaxabiso abuyayo

Imisebenzi yeBhash nayo inokubuyisa amaxabiso.

Umzekelo:

Result is: 3

Iziphumo:

#!/bin/bash add() {
result=$(($1 + $2)) } add 1 2 echo 'The sum is: '$result

Enye indlela yokubuyisa amaxabiso emsebenzini kukwabela umphumo kuguquko olunokusetyenziswa xa kufuneka njalo.

Umzekelo:

The sum is: 3

Iziphumo:

#!/bin/bash add () { local result=$(($1 + $2)) echo '$result' } result='$(add 1 2)' echo 'The sum is: '$result