Bhash Kana-Else Chirevo Nemienzaniso

Kana-zvimwe zvirevo mu bash kunyora zvakafanana nechero mimwe mitauro yekuronga; inzira yechirongwa chekuita sarudzo.

Mu if-else zvirevo, kuisirwa kwechivharo chechirevo kunotemerwa zvichibva pamhedzisiro ye if mamiriro.

Bhash Kana-Else Chirevo Syntax

Iyo syntax ye if-else chirevo mu bash ndechekuti:

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

Kana mamiriro acho akaongorora ku true, iyo if block kodhi inoitwa, uye kana mamiriro acho akaongorora ku false ipapo iyo else block kodhi inoitwa.

Ongorora:Iyo if block chete inourawa kana mamiriro acho akaongorora ku ichokwadi .
Icho chimwe chirevo uye imwe kodhi kiyi ndeyekusarudza.

Tinofanira kupedzisa if chirevo ne fi kiyi.

Mamiriro Ekuenzanisa

Chirevo chinoshandiswa nemamiriro ekuvaka chinofanira kuongorora kuti true kana false. Chirevo chinogona kuve tambo imwechete kana musiyano.



  • zvishoma pane - inoratidzirwa na: $a -lt $b kana $a < $b
  • mukuru kudarika - inoratidzirwa na: $a -gt $b kana $a > $b
  • zvishoma pane kana zvakaenzana ne - inoratidzirwa na: $a -le $b kana $a <= $b
  • mukuru kudarika kana kuenzana ne - inoratidzirwa na: $a -ge $b kana $a >= $b
  • zvakaenzana ne - inoratidzirwa na: $a -eq $b kana $a == $b
  • isina kuenzana ne - inoratidzirwa na: $a -ne $b kana $a != $b

Vanoshanda Vanonzwisisika

Chirevo mukati me if chirevo chinogona zvakare kuve musanganiswa une musoro wekuenzanisa kwakawanda.

Iwo anoshanda anoshanda ndeaya:

  • zvine musoro uye - yakatsanangurwa ne $a AND $b kana $a && $b inoongorora ku true apo misiyano miviri kana zvirevo zviri zvechokwadi.
  • zvine musoro kana - yakatsanangurwa ne $a OR $b kana $a || $b inoongorora ku true kana chimwe chezvakasiyana kana zvirevo zviri zvechokwadi.
Ongorora:Kana uchishandisa vashandisi vanonzwisisika, zvirevo zvine mamiriro zvinofanirwa kukomberedzwa nemabhureki maviri [[]].

Semuyenzaniso:

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

Bhash kana Muenzaniso

I if chirevo chinongova chirevo chine mutsigo. Kana mamiriro ari mukati me if[] inoongorora ku true ipapo iyo if kodhi block inoitwa.

Muenzaniso:

#!/bin/bash read -p 'Enter a number: ' mynumber if [ $mynumber -gt 10 ] then
echo 'The number you entered is greater than 10' fi
Ongorora:Mumuenzaniso wepamusoro, kana tikapinda nhamba iri pasi pegumi, saka hapana chinodhindwa.

Bhash kana-imwe Muenzaniso

Kana mhedzisiro ye if mamiriro ari false ipapo kodhi iri mu | _ _ _ | block inoitwa, chero paine imwe.

Semuyenzaniso:

else

Bhash kana-elif-imwe Semuenzaniso

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
(zvimwe if) inoshandiswa kana paine akawanda elif mamiriro.

Semuyenzaniso:

if

Bhash Nested kana Muenzaniso

Isu tinogona zvakare kuve nested #!/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
zvirevo.

Semuyenzaniso:

if