Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

FTP exit and error codes

Shell Programming and Scripting


Tags
bash, exit code, ftp, ftp issues, linux and unix

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-23-2014   -   Original Discussion by dhruuv369
dhruuv369's Unix or Linux Image
dhruuv369 dhruuv369 is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 21 August 2015, 6:35 AM EDT
Location: New York
Posts: 55
Thanks: 29
Thanked 1 Time in 1 Post
FTP exit and error codes

I have a script which connects to a remote server via FTP and gets some file from there.



Code:
#!/bin/bash
/usr/bin/ftp -n remote.hostname.com <<EOF
quote USER user123
quote PASS password123
lcd /dir1/dir2/dir3
cd /
mget file_pattern*
close
bye
EOF

rc=$?
            if [[ $rc != 0 ]]
            then
            echo "Error occured getting the files...$rc" `date "+%Y-%m-%d-%H.%M.%S"`
            exit 1
else
  echo "Successful transfer of the files...$rc" `date "+%Y-%m-%d-%H.%M.%S"`
fi

When the files with the mentioned patterns are not available on the server, the script should echo the failure message and exit the script with error code 1, else it should download the files and echo the successful transfer message. However, in my case, it is always returning the successful transfer message even if there are no files on the server with that name. Any suggestions on what needs to be changed?
Sponsored Links
    #2  
Old Unix and Linux 06-23-2014   -   Original Discussion by dhruuv369
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 20 April 2018, 2:49 PM EDT
Location: Saskatchewan
Posts: 22,636
Thanks: 1,172
Thanked 4,306 Times in 3,972 Posts
You may have to check the text printed by FTP.
Sponsored Links
    #3  
Old Unix and Linux 06-24-2014   -   Original Discussion by dhruuv369
dhruuv369's Unix or Linux Image
dhruuv369 dhruuv369 is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 21 August 2015, 6:35 AM EDT
Location: New York
Posts: 55
Thanks: 29
Thanked 1 Time in 1 Post
I did echo $rcand it was returning '0'. I believe it is returning this after successfully completing the closeand byecommands. But, how can it return an error code or exit code as '1' in case there are no files to download or any issues with downloading? Any suggestions?Linux

Last edited by dhruuv369; 06-24-2014 at 09:13 AM.. Reason: Improved reply
    #4  
Old Unix and Linux 06-24-2014   -   Original Discussion by dhruuv369
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 20 April 2018, 2:49 PM EDT
Location: Saskatchewan
Posts: 22,636
Thanks: 1,172
Thanked 4,306 Times in 3,972 Posts
You might have to check the text printed by ftp (which is not the same thing as the return code).
Sponsored Links
    #5  
Old Unix and Linux 06-24-2014   -   Original Discussion by dhruuv369
dhruuv369's Unix or Linux Image
dhruuv369 dhruuv369 is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 21 August 2015, 6:35 AM EDT
Location: New York
Posts: 55
Thanks: 29
Thanked 1 Time in 1 Post
Okay, I checked what FTP command from the script gives the output. It is as below


Code:
Local directory now /dir1/dir2/dir3
No files found.
0
Successful transfer of the files...0 2014-06-24-12.19.19

If this is not what you were referring to as text, please let me know how can I find it.
Sponsored Links
    #6  
Old Unix and Linux 06-24-2014   -   Original Discussion by dhruuv369
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 20 April 2018, 2:49 PM EDT
Location: Saskatchewan
Posts: 22,636
Thanks: 1,172
Thanked 4,306 Times in 3,972 Posts
OK, something like:



Code:
ftp <<EOF 2>&1 > /tmp/$$
...
EOF

if [ "$?" -ne 0 ] || grep "No files" /tmp/$$ >/dev/null
then
        echo "Error in file transfer"
fi

rm -f /tmp/$$

I suspect that FTP will still return nonzero error codes for things like failure to connect, so it checks both.
The Following User Says Thank You to Corona688 For This Useful Post:
dhruuv369 (06-25-2014)
Sponsored Links
    #7  
Old Unix and Linux 06-24-2014   -   Original Discussion by dhruuv369
jim mcnamara's Unix or Linux Image
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 April 2018, 10:08 PM EDT
Location: NM
Posts: 11,353
Thanks: 602
Thanked 1,153 Times in 1,062 Posts
FTP almost always returns 0. The return code is not normally useful.
You have to parse the responses from an ftp server to figure out what is going on. A 3 digit number is first followed by text. Every transaction gets one of the things.

FTP return codes are displayed like: 550 Failed to connect
List of FTP server return codes - Wikipedia, the free encyclopedia

See also: FTP
The Following User Says Thank You to jim mcnamara For This Useful Post:
dhruuv369 (06-25-2014)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Exit codes rlmadhav Linux 4 05-05-2010 01:28 PM
Exit Codes cmschube Shell Programming and Scripting 1 04-04-2008 11:34 AM
Catching all Exit Codes Sivaswami J Shell Programming and Scripting 3 02-16-2006 06:13 PM
Where can I find a list of exit codes? (Exit code 64) jkuchar747 UNIX for Dummies Questions & Answers 3 12-07-2004 05:08 PM
exit codes donna carter Programming 3 05-31-2001 09:35 PM



All times are GMT -4. The time now is 07:53 AM.