![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Print Error with set command | Shribigb | Shell Programming and Scripting | 0 | 03-06-2009 06:08 PM |
| tar error exit delayed form pervious error | chayato | Linux | 1 | 02-06-2009 12:07 AM |
| In ksh shell command - Print "-ABC" is giving error | sagarjani | Shell Programming and Scripting | 2 | 10-08-2008 04:32 PM |
| Custom error page when tomcat authentication fails | sebagra | UNIX and Linux Applications | 0 | 05-06-2008 05:10 PM |
| at command fails | a329743 | UNIX for Advanced & Expert Users | 1 | 10-05-2006 10:08 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
How to print error and exit if command fails?
Guys any tips on printing a certain error message to stderr and exiting should a command fail within a ksh script? I'm trying to null some output files.
Touch isn't suitable as i need to null them. print "" > file isn't suitable as i need to check elsehere for if they are 0bytes or not. I've tried these below examples and none work correctly. I don't want to have to put a check after each command as :- Code:
if [[ $? -ne 0 ]];then print "error blah blah" >&2 exit 2 fi Code:
OUTFILE=/tmp/out
# Null outfiles. Security already checked
> ${OUTFILE} || print "ERROR: blah blah \n" >&2 ; exit 2 #doesnt work
> ${OUTFILE} || (print "ERROR: blah blah \n" >&2 ; exit 2 ) #doesn't work
> ${OUTFILE} || (print "ERROR: blah blah \n" >&2 && exit 2) #doesnt work
if [[ -n "$(> ${OUTFILE} 2>&1)" ]];then
print "ERROR: blah blah \n" >&2
exit 2
fi # doesnt work
|
|
||||
|
Specifically about reporting and exiting, this is a slightly more condensed idiom:
Code:
test $condition || { print "Crap blew up; exiting"; exit 2; }
Code:
command || { print "Crap blew up; exiting"; exit 2; }
Last edited by EagleFlyFree; 04-23-2009 at 02:17 PM.. |
|
||||
|
Thanks dude thats worked like a charm.
Seems i was almost there but didn't use the correct { } Could you explain the difference between { } and ( ) in the command grouping? Also i notied the ; at the end before } is vitally important otherwise the next command doesn't work. e.g Code:
This works and exits if cant null but echos got to here if can.
> ${OUTFILE} || { print "ERROR: cannot null output file. Exiting\n" >&2; exit 2; }
> ${TMPFILE} || { print "ERROR: cannot null tmp file. Exiting\n" >&2; exit 2; }
echo "got to here"
This doesnt work and never gets to echo even if null is successful
> ${OUTFILE} || { print "ERROR: cannot null output file. Exiting\n" >&2; exit 2 }
> ${TMPFILE} || { print "ERROR: cannot null tmp file. Exiting\n" >&2; exit 2 }
echo "got to here"
|
|
||||
|
() executes the statements in a new subshell, with separate state. {} executes stuff in the current shell.
Example: Code:
(aVariable="hello"); echo $aVariable Code:
{aVariable="hello"; }; echo $aVariable
It's the same difference as: Code:
sh myScript.sh Code:
source myScript.sh Also, yes, you need a semicolon to end the last statement inside {}; that's how the shell's grammar is defined. Kind of how you can either do this: Code:
if $condition; then $statements; fi Code:
if $condition
then
$statements
fi
Last edited by EagleFlyFree; 04-23-2009 at 02:14 PM.. |
|
||||
|
Don't mention it; I love yapping about bash.
Its man page is long and daunting, but flick through it every once in a while; you're bound to learn great tidbits every time. The parts about history and readline are particularly cool, and they're handy and fun to use. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|