Otu esi eme ọnọdụ nbibi nbibi na Syntax na Scripts Shell


Anyị malitere usoro nbibi script shei site n'ịkọwa nhọrọ nbipu dị iche iche yana otu esi eme ka ụdị nbibi edemede shea nwee ike.

Mgbe ị dechara script shei gị, a na-atụ aro ka anyị lelee syntax dị n'edemede ahụ tupu ị na-agba ya, kama ilele mmepụta ha iji gosi na ha na-arụ ọrụ nke ọma.

N'akụkụ a nke usoro, anyị ga-aga site na iji syntax checking debugging mode. Cheta na anyị kọwara nhọrọ nbipu dị iche iche na akụkụ mbụ nke usoro isiokwu a na ebe a, anyị ga-eji ha mee ihe nbibi edemede.

Tupu anyị abanye na isi ihe ntuziaka a, ka anyị nyochaa nkenke ụdị ngwaa. A na-enyere ya aka site na nhọrọ nbibi nke -v, nke na-agwa shei ka ọ gosipụta ahịrị niile dị na edemede mgbe a na-agụ ha.

Iji gosi ka nke a si arụ ọrụ, n'okpuru bụ ihe atụ ederede shei iji tụgharịa onyonyo PNG ka ọ bụrụ usoro JPG.

Pịnye (ma ọ bụ detuo na mado) ya na faịlụ.

#!/bin/bash
#convert
for image in *.png; do
        convert  "$image"  "${image%.png}.jpg"
        echo "image $image converted to ${image%.png}.jpg"
done
exit 0

Wee chekwaa faịlụ ahụ ma mee ka edemede ahụ rụọ ọrụ site na iji iwu dị n'okpuru:

$ chmod +x script.sh

Anyị nwere ike ịkpọ edemede ahụ wee gosipụta ahịrị niile dị na ya ka shei na-agụ ya dị ka nke a:

$ bash -v script.sh

Na-alaghachi na isiokwu anyị na-emesi ike, -n na-eme ka ọnọdụ nlele syntax rụọ ọrụ. Ọ na-agwa shei ahụ ka ọ gụọ iwu niile, mana ọ naghị eme ya, ọ (shell) na-enyocha syntax ejiri.

Ọ bụrụ na enwere mperi na edemede shei gị, shei ahụ ga-ewepụta njehie na njedebe, ma ọ bụghị ya, ọ naghị egosipụta ihe ọ bụla.

Okwu syntax maka ịgbalite nyocha syntax bụ nke a:

$ bash -n script.sh

N'ihi na syntax dị na edemede ahụ ziri ezi, iwu dị n'elu agaghị egosipụta mmepụta ọ bụla. Ya mere, ka anyị gbalịa wepụ okwu emere nke mechiri loop wee hụ ma ọ na-egosi mperi:

N'okpuru bụ edemede shei gbanwetụrụ iji tụgharịa onyonyo png ka ọ bụrụ usoro jpg nwere ahụhụ.

#!/bin/bash
#script with a bug
#convert
for image in *.png; do
        convert  "$image"  "${image%.png}.jpg"
        echo "image $image converted to ${image%.png}.jpg"

exit 0

Chekwaa faịlụ ahụ, wee mee ya ka ị na-enyocha syntax na ya:

$ bash -n script.sh

Site na mmepụta dị n'elu, anyị nwere ike ịhụ na enwere nsogbu syntax na edemede anyị, maka loop na-efunahụ mmechi eme okwu isiokwu. Na shei ahụ na-achọ ya ruo na njedebe nke faịlụ ahụ ma ozugbo ọ hụghị ya (emechara), shei ahụ bipụtara njehie syntax:

script.sh: line 11: syntax error: unexpected end of file

Anyị nwekwara ike jikọta ọnọdụ verbose na ọnọdụ nlele syntax ọnụ:

$ bash -vn script.sh

N'aka nke ọzọ, anyị nwere ike mee ka ịlele syntax site n'ịgbanwee ahịrị mbụ nke edemede dị n'elu dị ka n'ọmụmaatụ na-esote.

#!/bin/bash -n
#altering the first line of a script to enable syntax checking

#convert
for image in *.png; do
    convert  "$image"  "${image%.png}.jpg"
    echo "image $image converted to ${image%.png}.jpg"

exit 0

Dị ka ọ dị na mbụ, chekwaa faịlụ ma mee ya ka ị na-enyocha syntax:

$ ./script.sh

script.sh: line 12: syntax error: unexpected end of file

Na mgbakwunye, anyị nwere ike were iwu arụnyere shei setịpụrụ iji mee ka ọnọdụ nbipu dị na edemede dị n'elu.

N'ihe atụ dị n'okpuru, anyị na-enyocha naanị syntax nke loop na edemede anyị.

#!/bin/bash
#using set shell built-in command to enable debugging
#convert

#enable debugging
set -n
for image in *.png; do
    convert  "$image"  "${image%.png}.jpg"
    echo "image $image converted to ${image%.png}.jpg"

#disable debugging
set +n
exit 0

Ọzọ, chekwaa faịlụ ahụ wee kpọọ edemede ahụ:

$ ./script.sh 

Na nchịkọta, anyị kwesịrị ijide n'aka na anyị na-enyocha n'ụzọ ziri ezi n'ụdị shea anyị iji jide njehie ọ bụla tupu anyị emee ha.

Iji zitere anyị ajụjụ ma ọ bụ nzaghachi ọ bụla gbasara ntuziaka a, jiri fọm nzaghachi dị n'okpuru. N'akụkụ nke atọ nke usoro isiokwu a, anyị ga-aga na ịkọwa na iji shei tracing debugging mode.