nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

exit status

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: ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 08-30-2005, 07:32 PM
Guest
 
Posts: n/a
Default exit status

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
the $?, if placed after the loop, will print the exit status of the script. In which case, it is always......Command completed successfully.

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
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-31-2005, 12:25 AM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

get status in loop itself
Code:
ST=0
for i in........
do command1
    command2
    command3
    ST=$?
done 

if [ "$ST" == "0" ]; then
do
    echo "Ok"
else
    echo "Failed
done
Reply With Quote
  #3 (permalink)  
Old 08-31-2005, 12:27 PM
Member
User
 
Join Date: Jul 2005
Posts: 85
Rep Power: 0
ricc
Default

Oh ! Great...
Thank you monk.

I would like to know...does it give $?=0 even if any one of the commands in the loop fails.

Thanks in advance
Reply With Quote
  #4 (permalink)  
Old 08-31-2005, 02:32 PM
rockdalinux's Avatar
Contributors
User
 
Join Date: May 2005
Location: Bangalore
My distro: RHEL, HP-UX, Solaris, FreeBSD, Ubuntu
Posts: 581
Rep Power: 7
rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough
Default

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
if command syntax was incorrect as well as you used “$ST” == “0” which is for strict POSIX compliance and returns ture if the strings are equal. Since exit status is always numeric non negative value, it is better to use arithmetic binary operators such as -eq (equal to).

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
Reply With Quote
  #5 (permalink)  
Old 08-31-2005, 07:39 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

Opps you are right rocky, I made mistake while writing if..else code. However you can use “$ST” == “0” string based comparison and it is valid.
Reply With Quote
  #6 (permalink)  
Old 09-01-2005, 12:53 PM
Member
User
 
Join Date: Jul 2005
Posts: 85
Rep Power: 0
ricc
Default

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
Now, Lets say...the user typed in a directory name, that doesn't exist. Then, the remaining of the script is obviously not valid. And also, if the directory name given is not valid.....ls gives an error on the screen.

I want that anywhere, any command encounters an error, the script should echo failed.

Thanks for any and all the help.

rc
Reply With Quote
  #7 (permalink)  
Old 09-01-2005, 02:06 PM
sweta's Avatar
Contributors
User
 
Join Date: Feb 2005
Location: New Delhi
My distro: Suse, RHEL, Vista
Posts: 154
Rep Power: 4
sweta will become famous soon enough
Default

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
Reply With Quote
  #8 (permalink)  
Old 09-01-2005, 05:01 PM
Member
User
 
Join Date: Jul 2005
Posts: 85
Rep Power: 0
ricc
Default

Thats good--- . Thank you Sweta.

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
and if any of the commands within (cat file.txt | awk ' { print $1 }'| grep -vE "( xyz|abc|^-)") fails, can we get an exit status of that.

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
Reply With Quote
  #9 (permalink)  
Old 09-02-2005, 07:34 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

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.
So you are right and you will get only exit status of the LAST command in pipeline, not a any one of the in (cat file.txt | awk ' { print $1 }'| grep -vE "( xyz|abc|^-)") .

Hope this clears some black clouds
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

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


All times are GMT +5.5. The time now is 07:52 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36