The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-23-2009
lavascript lavascript is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 47
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
Below tests don't work correctly. I'm guessing its spawning something.

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
Any ideas or alternatives?
  #2 (permalink)  
Old 04-23-2009
EagleFlyFree EagleFlyFree is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 13
Specifically about reporting and exiting, this is a slightly more condensed idiom:

Code:
test $condition || { print "Crap blew up; exiting"; exit 2; }
You could also directly test for the result of your command:

Code:
command || { print "Crap blew up; exiting"; exit 2; }
I enjoy reading that out loud to myself as "either you do this or DIE!", as if threathening the script.

Last edited by EagleFlyFree; 04-23-2009 at 02:17 PM..
  #3 (permalink)  
Old 04-23-2009
lavascript lavascript is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 47
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"
  #4 (permalink)  
Old 04-23-2009
EagleFlyFree EagleFlyFree is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 13
() executes the statements in a new subshell, with separate state. {} executes stuff in the current shell.

Example:

Code:
(aVariable="hello"); echo $aVariable
this doesn't print "hello" because the variable was assigned inside a new shell, whose state was discarded when the () expression ended. Think of variable scoping in C; variables live and die inside the block where they're declared.

Code:
{aVariable="hello"; }; echo $aVariable
this prints "hello" because the variable was assigned in the same shell as the next statement.

It's the same difference as:
Code:
sh myScript.sh
and
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
or this, using newlines instead of semicolons to separate the syntax parts:
Code:
if $condition
then
    $statements
fi
According to the bash man page, it's different from () because { and } are reserved words instead of metacharacters, which means they don't automatically cause word breaks. Presumably the same applies in the rest of the shells.

Last edited by EagleFlyFree; 04-23-2009 at 02:14 PM..
  #5 (permalink)  
Old 04-24-2009
lavascript lavascript is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 47
Dude thats real great and an informative reply. Thanks a lot. Its certainly giving me a better understanding of the shell rather than just trying things until they work
  #6 (permalink)  
Old 04-24-2009
EagleFlyFree EagleFlyFree is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 13
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.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0