2011-10-18

ලිනක්ස් Shell Programming පාඩම-කොටස 05



10.Exit Status කියන්නෙ මොකද්ද?
කලින් පාඩමේ අන්තිමට කරපු උදාහරණෙ අන්තිමට තිබුන මතකද exit 0 කියල එකක්. ඒ මොකද්ද කියල තමයි මම කියන්න යන්නෙ දැන්.

$ gedit my

#

# This is My 2nd shell Script

# Print multipal Lines using echo

clear

echo "mad Terminal $USER"

echo "mad Terminals date \c" ;date

echo "User info : \c" ; who | wc -l

echo "Calendar"


cal

exit 0
 
මේකයි කතාව Linux වලදි මොකක් හරි Command එකක් හරි shell Command එකක් හරි රන් කරහම ඒ රන් කරපු එක successful නම් එක් අගයකුත් successful නැත්තම් තවත් අගයකුත් return වෙනව.මෙන්න මේකට තමයි exit status එක කියල කියන්නෙ.මේ value එක ගබඩා වෙන්නෙ $? කියල විශේෂ variable එකකට.අපි ඒක කරල බලමුකො දැන්.

ඔයාලගෙ terminal එක open කරල මෙහෙම type කරන්න.
rm catAnddog
catAnddog කියන එකට කැමති නමක් දෙන්න (හැබැයි ඒ නමින් file එකක් හරි directory තියෙන්න බෑ එතන)
rm - කියන්නෙ අයින් කරන්න කියල (delete)
catAnddog කියන්නෙ file එකක නමක්

    rm catAnddog වලින් වෙන්නෙ දැනට ඉන්න directory එකේ තියෙන catAnddog කියන file එක අයින් කරන එක ඒත් එහෙම කරන්න එතන catAnddog කියල file එකක් නැති නිසා rm: cannot remove `catAnddog': No such file or directory කියල error පණිවිඩයක් පෙන්නාවි Terminal එකේ දැන් අපි command එකක් රන් කරානෙ ඒනිසා exit status එක වෙනස් වෙලා තියෙන්නෙ දැන් (ඒ කියන්නෙ $? කියන variable එකට අගයක් Assign වෙලා තියෙන්න ඔන) අපි දැන් බලමු ඒ අගය මොකද්ද කියල ඒක බලන්නෙ මෙහෙමයි terminal එකේ ටtype කරන්න echo $? කියල මේ කමාන්ඩ් එක රන් කරහම 1 කියල ප්‍රින්ට් උනා නේද ? දැන් ls කියල type කරල enter කරන්නකො (ls කියන කමාන්ඩ් එකෙන් පුලුවන් දැනට ඉන්න directory එකේ තියෙන සියලු files බලා ගන්න) දැන් ඔයා අයෙ echo $? රන් කරහම 0 කියල print උනා නේද? මෙන්න මේකයි වෙන්නෙ කමාන්ඩ් එක හරි shell එක හරි හරියට රන් උනේ නැත්තම් exit status එක 1 වෙනෙව හරියට රන් උනානම් exit status එක 0 වෙනව කලින් දෙවනියට රන් කරපු ls command එක error නැතුව රන් උන නිසා $? ගෙ අගය 0 උනා .
මෙන්න මෙතන තියන උදාහරණ කිහිපයත් බලන්න.
01. expr 5 + 3
echo $?

02. echo hi man
echo $?


03. wildwest canwork?
echo $?


04. date
echo $?


05. echon $?
echo $?


06. echo cal
echo $?


ඊළඟට මම දෙන්නම් තවත් shell වල භාවිතා කරන variables කිහිපයක් දැනට මේව ඒ තරම් නොතේරුනාට කමක් නෑ පස්සෙ මේව ගැන දැනගන්න ලැබෙයි.


$# : Number of command line arguments. Useful to test no. of command line args in shell script.

$* :All arguments to shell

$@ :Same as above

$- :Option supplied to shell

$$ :PID of shell

$! :PID of last started background process (started with &)

මේ උදාහරණෙන් ඔයාට යම් අදහසක් ලැබෙයි මේ ගැන
####################################################

$ vi demo
#!/bin/sh
#
# Script that demos, command line args
#
echo "Total number of command line argument are $#"
echo "$0 is script name"
echo "$1 is first argument"
echo "$2 is second argument"
echo "All of them are :- $* or $@"

###################################################

Share:

1 comments:

Anonymous said...

cool