Isitatimende seBash If-Else Esinezibonelo

Uma ezinye izitatimende ku-bash scripting ziyefana nezinye izilimi zohlelo; kuyindlela yohlelo lokuthatha izinqumo.

Ku if-else izitatimende, ukwenziwa kwesitatimende kunqunywa ngokuya ngomphumela we- if isimo.

I-Bash If-Else Statement Syntax

I-syntax ye- if-else isitatimende ku-bash ngu:

if [condition] then
//if block code else // else block code fi

Uma isimo sihlaziya ku- true, i- if ikhodi ebhlokiwe iyenziwa, futhi uma isimo sihlaziya ku- false bese i- else ikhodi ebhlokiwe iyenziwa.

Qaphela:Ibhulokhi if kuphela uyabulawa uma isimo sihlaziya ku- iqiniso .
Esinye isitatimende kanye nenye ikhodi block kungakhethwa.

Kufanele siqede if isitatimende esine fi igama elingukhiye.

Ukuqhathanisa okunemibandela

Isisho esisetshenziswe ngokwakhiwa okunemibandela kufanele sihlaziye ukuthi true noma false. Isisho singaba yintambo eyodwa noma okuguqukayo.



  • ngaphansi kwe - kukhonjiswe ngu: $a -lt $b noma $a < $b
  • okukhulu kune - kukhonjiswe ngu: $a -gt $b noma $a > $b
  • ngaphansi noma kulingana ne- - kukhonjiswe ngu: $a -le $b noma $a <= $b
  • kukhulu noma kulingana no - kukhonjiswe ngu: $a -ge $b noma $a >= $b
  • ilingana ne- - kukhonjiswe ngu: $a -eq $b noma $a == $b
  • ayilingani ne- - kukhonjiswe ngu: $a -ne $b noma $a != $b

Ama-opharetha anengqondo

Isisho esiku | if isitatimende futhi singaba yinhlanganisela enengqondo yokuqhathanisa okuningi.

Ama-opharetha anengqondo yilawa:

  • kunengqondo futhi - kukhonjiswe ngu $a AND $b noma $a && $b ihlola ku- true lapho zombili eziguqukayo noma izitatimende ziyiqiniso.
  • kunengqondo noma - kukhonjiswe ngu $a OR $b noma $a || $b ihlola ku- true lapho okunye okuguqukayo noma izitatimende kuliqiniso.
Qaphela:Lapho usebenzisa opharetha abanengqondo, izinkulumo ezinemibandela kufanele zizungezwe abakaki ababili [[]].

Ngokwesibonelo:

#!/bin/bash first_name='John' last_name='Doe' if [[ $first_name = 'John' && $last_name = 'Doe' ]] then echo 'hello John Doe' fi

IBash uma Isibonelo

I- if isitatimende siyisitatimende esilula esinemibandela. Uma isimo ngaphakathi kwe- if[] ihlola ku- true bese i- if ikhodi block iyenziwa.

Isibonelo:

#!/bin/bash read -p 'Enter a number: ' mynumber if [ $mynumber -gt 10 ] then
echo 'The number you entered is greater than 10' fi
Qaphela:Esibonelweni esingenhla, uma sifaka inombolo engaphansi kuka-10, khona-ke akukho lutho oluphrintwayo.

IBash uma-esinye isibonelo

Lapho umphumela we if isimo | | + _ | bese ikhodi eku | false block kuyenziwa, inqobo nje uma kukhona eyodwa.

Ngokwesibonelo:

else

IBash if-elif-else Isibonelo

I- #!/bin/bash read -p 'Enter a number: ' mynumber if [ $mynumber -gt 10 ] then
echo 'The number you entered is greater than 10' else
echo 'The number you entered is less than 10' fi
(okunye uma) kusetshenziswa lapho kunezinombolo eziningi _ _ + _ | izimo.

Ngokwesibonelo:

elif

IBash Idliwe uma Isibonelo

Singaba futhi nesidleke if izitatimende.

Ngokwesibonelo:

#!/bin/bash read -p 'Enter your exam grade: ' grade if [ $grade -ge 80 ] then
echo 'You got A' elif [ $grade -ge 70 ] then
echo 'You got B' elif [ $grade -ge 60 ] then
echo 'You got C' else
echo 'Fail' fi