Linux / UNIX Tech Support Forum
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: ...
|
|||||||
| Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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 | ||
|
|
|
|||
|
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 |
|
||||
|
Here is code you wanna
Code:
#!/bin/bash
echo -n "Give the directory name : "
read dname
if [ ! -d $dname ];then
echo "Error - $dname is not directory"
exit 1
fi
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
done
if [ "$?" == "0" ]
then echo "This Script completed successfully"
else echo "This Script failed"
fi
|
|
|||
|
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 |
![]() |
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 | 27 | 06-05-2009 09:44 PM |
| hda: status timeout: status=0xd0 { Busy } | surmandal | Linux hardware | 3 | 24-03-2008 08:16 PM |
| displaying the executed command then echo the status | warren | Shell scripting | 4 | 29-11-2006 01:56 AM |