This is a discussion on exit status within the Shell scripting forums, part of the Development/Scripting category; Hi all, Just wondering, how can we get the exit status of a command inside the script. Lets say... Code: ...
|
|||||||
| Register | FAQ | Members List | Calendar | Forgotten your password? | Mark Forums Read |
|
|||
|
Hi all,
Just wondering, how can we get the exit status of a command inside the script. Lets say... Code:
for i in........
do command1
command2
command3
done
if [ $? = 0 ]
echo "Command completed successfully"
else echo "Command failed"
fi
But what I want is, if anything within the for loop fails, the exit status shall be greater than 0. in which case, it will echo .....Command failed. But this never happens Where am I going wrong. Thanks in advance for any help rc |
| Sponsored Links | ||
|
|
|
||||
|
Monk, your code upto for loop seems to correct but if...else..fi should be as follows :O
Code:
if [ $ST -eq 0 ]; then
echo "Ok"
else
echo "Failed
fi
ricc, I think $? in loop will only useful after command3 and not useful for command2 and command1, because it will always/store command3 exit status. You can do as follows: Code:
# exit status for all three command: EST1=0 EST2=0 EST3=0 for i in... do command1 EST1=$? command2 EST2=$? command3 EST3=$? done [ $EST1 -ne 0 ] && echo “Command1 Ok” || echo “Command1 Failed” [ $EST2 -ne 0 ] && echo “Command2 Ok” || echo “Command2 Failed” [ $EST3 -ne 0 ] && echo “Command3 Ok” || echo “Command3 Failed”
__________________
Rocky Jr. You may have my body & soul, but you will never touch my pride! If you have knowledge, let others light their candles at it. Certified to work on HP-UX / Sun Solaris / RedHat |
|
|||
|
Thanks rocky,
I have stumbled upon something. I want to know, can we get the exit status of the command in the for line...... like: Code:
#!/bin/bash # give a directory name to the script echo -n " Give the directory name : " read dname for i in $(ls dname) do if [ -f $i ] && [ -x $i ] then echo " $i is an executable file" elif [ -f $i ] && [ ! -x $i ] then echo " $i is not an executable file" elif [ -d $i ] then echo " $i is a directory" fi fi fi done if [ "$?" == "0" ] then echo "This Script completed successfully" else echo "This Script failed" fi I want that anywhere, any command encounters an error, the script should echo failed. Thanks for any and all the help. rc |
|
|||
|
Thats good---
One more thing.... Lets say.... we have something within a "for" script loop.. like: Code:
for i in $(cat file.txt | awk ' { print $1 }'| grep -vE "( xyz|abc|^-)")
do command 1
command 2
command 3
done
can we....?. I guess when we run something within () , it spawns another sub-shell for that. Thanks everyone...monk,rocky,vivek,sweta...everyone, for all of your unstinted support and replies. Cheers, rc |
|
||||
|
You're welcome!
From man pages. Code:
Each command in a pipeline is executed in its own subshell . The exit status of a pipeline is the exit status of the last command in the pipeline. If the reserved word `!' precedes the pipeline, the exit status is the logical negation of the exit status of the last command. Hope this clears some black clouds |
![]() |
| Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| multiple server status script | marinm | Linux software | 21 | 10-24-2008 02:16 AM |
| hda: status timeout: status=0xd0 { Busy } | surmandal | Linux hardware | 3 | 03-24-2008 09:16 PM |
| displaying the executed command then echo the status | warren | Shell scripting | 4 | 11-29-2006 02:56 AM |