LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-28-2013, 05:51 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
A shell command exit status.


Hi: suppose I run
Code:
#!/bin/bash

cp ./* another/directory/
exit(0)
,

and I want to examine cp's exit status after the script completion. How can I do it? Of course, the same question goes for anyone of bash commands.

I guess I could use, too, a variable within the script, to save the exit status and then output it to console, but do not how to do it.

Last edited by stf92; 10-28-2013 at 05:54 PM.
 
Old 10-28-2013, 06:06 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
$?
is the exit number of that last command/process


Code:
cp ./* another/directory/
cp_ExitCode=$?

.. work with ${cp_ExitCode} ..
another way, is list construct


Code:
cp ./* another/directory/ && echo "all went well" \
                          || echo "something went wrong"
advantage, easy
disadvantage, it is all or nothing
Most of the time that is perfectly fine,
but if you want to distinguish between exit codes ( instead of 0 and none-zero ) then $? gives you more options

http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
http://www.tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/BashGuide
http://www.gnu.org/software/bash/manual/bashref.html

The tldp stuff is great, however there are some nasty bad habits in it,
The mywiki.wooledge does a very good job of 'fixing' those habits
 
1 members found this post helpful.
Old 10-28-2013, 06:32 PM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Thanks a lot, and specially for your kind links. The guide by Garrels seems specially suited for me.
 
Old 10-29-2013, 02:38 AM   #4
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,149

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Cool

Quote:
Originally Posted by stf92 View Post
Hi: suppose I run
Code:
#!/bin/bash

cp ./* another/directory/
exit(0)
,

and I want to examine cp's exit status after the script completion. How can I do it? Of course, the same question goes for anyone of bash commands.

I guess I could use, too, a variable within the script, to save the exit status and then output it to console, but do not how to do it.
redirect the error to a file.

Quote:
cp ./* another/directory/ 2>>cp_error.log
 
1 members found this post helpful.
Old 10-29-2013, 11:04 AM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
That seems like an elegant solution! Thanks.
 
Old 10-29-2013, 12:58 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
What's wrong with either of:

Code:
#!/bin/bash

cp ./* another/directory/
exit $?
Or:

Code:
#!bin/bash

rc=`cp ./* another/directory/`

echo "Result of cp was $rc" >> mylogfile.txt;

exit $rc
Reason I say this is that the shell script returns the result of the cp, if that was the whole point and a calling function can further evaluate whether or not the script worked. I do either this or evaluate the outcome of system calls and shape my exit from the shell script accordingly because I call shell scripts from within a user application many times.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: How to get exit status from a piped command in Linux shell LXer Syndicated Linux News 0 02-24-2012 03:11 PM
LXer: Testing the Exit Status of a Command LXer Syndicated Linux News 0 11-03-2011 12:40 PM
How can I get exit status from a called shell. dthacker Programming 4 06-27-2008 09:51 AM
Exit status of system command?? rmvinodh123 Programming 4 05-04-2007 11:15 AM
Bash command exit status humbletech99 Programming 12 07-06-2006 12:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration