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

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #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?
 

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:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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