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.

capturing exceptions from an scp clause

Shell Programming and Scripting

Thread Tools Search this Thread Display Modes
Old Unix and Linux 02-24-2012   -   Original Discussion by guessingo
guessingo's Unix or Linux Image
guessingo guessingo is offline
Registered User
Join Date: Sep 2009
Last Activity: 26 April 2016, 3:13 PM EDT
Posts: 57
Thanks: 1
Thanked 0 Times in 0 Posts
capturing exceptions from an scp clause


I have a script, that will scp -p a file. The server has keys set up. So I don't even pass a username.

scp -p filename server:/directory

There is a small chance that I can get an error. could be networking, etc... this is run from a job. I like to capture all exceptions when I do this.

I can do
if { $? - eq 1 ]; then Log a message.

how would I capture the exception in a scp clause?
Sponsored Links
Old Unix and Linux 02-24-2012   -   Original Discussion by guessingo
Corona688's Unix or Linux Image
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
Join Date: Aug 2005
Last Activity: 23 January 2018, 2:54 PM EST
Location: Saskatchewan
Posts: 22,574
Thanks: 1,164
Thanked 4,293 Times in 3,961 Posts
You don't need to use $? when checking a command's return value. You can check the command directly.

if ! scp source destination
        echo "something bad happened" >&2

Or even

scp source destination || echo "something bad happened" >&2

If anything goes wrong, it will likely print an error to standard error. If you redirect standard error into a logfile yourself before you run it, all error messages will go into it.

exec 2>path/to/logfile

touch / # Since you're not root, this should print 'permission denied' into logfile

Sponsored Links

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
Relationship between exceptions and signals manolo123 Programming 0 01-11-2012 04:50 PM
Diff with exceptions Question Smitty0881 UNIX for Dummies Questions & Answers 3 01-05-2012 11:51 PM
java Exceptions color ramse8pc UNIX for Advanced & Expert Users 0 07-27-2011 12:55 PM
Remove directory with exceptions zivsegal UNIX for Advanced & Expert Users 1 11-30-2009 12:44 PM
exceptions in import madmat Shell Programming and Scripting 1 07-12-2007 04:38 PM

All times are GMT -4. The time now is 04:10 PM.