LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-26-2008, 04:37 PM   #1
dthacker
Member
 
Registered: Nov 2003
Location: Omaha, NE
Distribution: Ubuntu, Kubuntu, Ubuntu Server
Posts: 77

Rep: Reputation: 15
Cool How can I get exit status from a called shell.


I'm working on some really old boxes with no bash, perl, or other scripting language. The task is to ftp several files to a long set of machines.

I've set this up as two shells. ftp-send.sh builds the list and calls ftpit.sh, which does the actual ftp command. Here's ftp-send:
Code:
RUNDATE=`date +%Y%m%d%H%M`
echo $RUNDATE
LOGFILE=ftp-send$RUNDATE.log
#start the logfile
echo "ftp-send-update started at `date`">>$LOGFILE

DESTLIST=ftp-destination-list.txt
for i in `cat $DESTLIST`
    do
    echo "Now sending to $i" >> $LOGFILE
    if /upgrade_work/ftpit.sh $i; then
        echo "Transfer to $i successful"
    else
        echo "Transfer to $i failed"
    fi
    done
and here's ftpit.sh

Code:
ftp -n $1 <<EOF
    user root foopass
    binary
    put foo1.txt
    bye
    EOF
print $?
The problem I'm having is that ftpit.sh is always returning zero. I've tried putting ftpit into the main shell as a function, but that doesn't work either. Can someone recommend a way to catch and pass a non-zero exit code when ftpit isn't able to connect?

TIA Dave
 
Old 06-26-2008, 04:51 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
Quote:
Originally Posted by dthacker View Post

Can someone recommend a way to catch and pass a non-zero exit code when ftpit isn't able to connect?
The exit code is passed by the exit command or equivalent. In ftp.sh you are taking the default exit which is exit 0. So you need to have exit pass the return code returned from the ftp command. Without testing this is how I would change your script:

Code:
ftprc=ftp -n $1 <<EOF
    user root foopass
    binary
    put foo1.txt
    bye
    EOF
print $?
exit ftprc
------------------
Steve Stites
 
Old 06-26-2008, 06:23 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The default exit status of a script is the status of the last cmd executed, so you don't need anything except the ftp cmd in the 2nd script.
In your case you are receiving the exit status of the 'print' cmd, which always works, so is always zero.
If you really wanted to be explicit about it you could say

exit $?

but really that's redundant, as I said.
 
Old 06-27-2008, 08:06 AM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
I think that the exit code is to be trapped by the calling program.

ie. you should replace

Code:
if /upgrade_work/ftpit.sh $i; then

with

/upgrade_work/ftpit.sh $i
if [ $? -eq 0 ]; then
... and so on
 
Old 06-27-2008, 09:51 AM   #5
dthacker
Member
 
Registered: Nov 2003
Location: Omaha, NE
Distribution: Ubuntu, Kubuntu, Ubuntu Server
Posts: 77

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jailbait View Post
The exit code is passed by the exit command or equivalent. In ftp.sh you are taking the default exit which is exit 0. So you need to have exit pass the return code returned from the ftp command. Without testing this is how I would change your script:

Code:
ftprc=ftp -n $1 <<EOF
    user root foopass
    binary
    put foo1.txt
    bye
    EOF
print $?
exit ftprc
------------------
Steve Stites
Putting the ftp command into a shell variable seems to prevent the -n argument from getting read properly.
Code:
# ./ftp-send-update.sh
200806270944
/upgrade_work/ftpit.sh: -n:  not found
Transfer to 10.27.48.9 failed
/upgrade_work/ftpit.sh: -n:  not found
Transfer to 172.16.110.39 failed
/upgrade_work/ftpit.sh: -n:  not found
Transfer to 172.16.110.37 failed
I've tried a couple of different quoting methods on that first line, but I haven't found anything that works. Thanks for the suggestion though. I'm continuing to experiment with it.

Dave
 
  


Reply

Tags
exit, function, ksh, status



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
Exit status vedanu Programming 4 06-23-2007 05:13 PM
exit status naflan Programming 2 10-22-2004 12:22 PM
error exit status 30 debian_dummy Debian 2 09-21-2004 10:26 PM
gcc exit status 1 Daddio Linux - Software 4 10-25-2003 05:37 PM
Trapping an exit status chrisk5527 Linux - General 3 09-12-2003 09:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:44 PM.

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