nixCraft Linux Forum

nixCraft

Linux / UNIX 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

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 30-08-2005, 06: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
  #2 (permalink)  
Old 30-08-2005, 11:25 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
OS: Debian GNU/Linux
Posts: 506
Thanks: 0
Thanked 8 Times in 6 Posts
Rep Power: 7
monk has a spectacular aura about monk has a spectacular aura about
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 31-08-2005, 11:27 AM
Member
User
 
Join Date: Jul 2005
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
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 31-08-2005, 01:32 PM
rockdalinux's Avatar
Is that all you got?
User
 
Join Date: May 2005
Location: Planet Vegeta
OS: Redhat
Posts: 708
Thanks: 15
Thanked 19 Times in 18 Posts
Rep Power: 10
rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light
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.
What's wrong? I hope I am not making you uncomfortable...

Never send a boy to do a mans job.
Reply With Quote
  #5 (permalink)  
Old 31-08-2005, 06:39 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
OS: Debian GNU/Linux
Posts: 506
Thanks: 0
Thanked 8 Times in 6 Posts
Rep Power: 7
monk has a spectacular aura about monk has a spectacular aura about
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 01-09-2005, 11:53 AM
Member
User
 
Join Date: Jul 2005
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
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 01-09-2005, 01:06 PM
sweta's Avatar
Contributors
User
 
Join Date: Feb 2005
Location: New Delhi
OS: Suse, RHEL, Vista
Posts: 199
Thanks: 12
Thanked 9 Times in 9 Posts
Rep Power: 7
sweta has a spectacular aura about sweta has a spectacular aura about
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 01-09-2005, 04:01 PM
Member
User
 
Join Date: Jul 2005
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
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 02-09-2005, 06:34 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
OS: Debian GNU/Linux
Posts: 506
Thanks: 0
Thanked 8 Times in 6 Posts
Rep Power: 7
monk has a spectacular aura about monk has a spectacular aura about
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


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 Off


Similar Threads

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


All times are GMT +5.5. The time now is 01:52 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
2005-2010 nixCraft. All rights reserved

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 37 38