forcefully throwing error : unix script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers forcefully throwing error : unix script
# 1  
Old 10-01-2008
forcefully throwing error : unix script

how can i make one script fail if some condition is not satisfied.i m writing if ..else logic in script.i need some standard command to do that ,,
# 2  
Old 10-01-2008
Code:
if command; then
  : nothing
else
  rc=$?
  echo command failed: exit code $rc >&2
  exit $rc  # terminate script here and now
fi

# or
command || exit $?

# or
set -e
command

The inverted code in the if/then/else is required if you want to capture the exit code from the command. Otherwise you can simply write

Code:
if ! command; then
  echo command failed, exiting >&2
  exit 255
fi

The redirection to >&2 is customary; you want error messages on standard error, not standard output.

The shorthand with || is unobvious but quite logical (sic) once you get it.

The command set -e forces the script to exit immediately if any command fails.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Prustat throwing error only in zones not in global

Hi, Prustat is throwing error only in zones. But it is working fine in global. dtrace: invalid probe specifier /* ** The following is a trimmed version of two seperate DTrace scripts: ** ** socketsnoop.d - snoop TCP network socket traffic by process. ** This is intended to identify the... (3 Replies)
Discussion started by: Sumanthsv
3 Replies

2. Shell Programming and Scripting

For Loop throwing error

Hello Gurus, I am writing one script at linux. The logic is There is a find command which will find some specific files daily and store at a variable Then echo that variable . Now when I am trying to read the variable by using for loop it is throwing error as below:cat: CKDT.dat: No such... (5 Replies)
Discussion started by: pokhraj_d
5 Replies

3. Shell Programming and Scripting

Function throwing an error

Hi All I have two shell scripts where the second is getting invoked from the first. E.g. test1.sh and test2.sh Within test1, the code is something like this: #!/bin/bash . test2.sh usage() { echo "..." echo "....." } SRC=$1 DEST=$2 case "$3" in tran) doTran ;; *)... (7 Replies)
Discussion started by: swasid
7 Replies

4. Shell Programming and Scripting

awk--throwing an error

Hi all, I have below code sqlplus -s ext/exo@TIS << EOF whenever sqlerror exit failure rollback; set echo off set head off set serveroutput on set termout on set trimspool on SPOOL $SPOOL_FILE select 'ROWS '|| ' '||decode((count(Part_no)),0,'With greater values not... (3 Replies)
Discussion started by: Kiransagar
3 Replies

5. Shell Programming and Scripting

Tr--translate is throwing an error

Dear all, I would like to count the no;of word "INFORMATION" in a file called alt.lst and output to a unix variable INFORMATION.so to do this I wrote the below code INFORMATION=echo 'INFORMATION' | tr -cs 'A-Za-z' '\n' < /app/tisq005/01/home/tisq005b/scripts/alt.lst | grep -c "INFORMATION"... (2 Replies)
Discussion started by: Kiransagar
2 Replies

6. Shell Programming and Scripting

POCO install throwing error

All , I am trying to install POCO lib on Solaris server. When I do make -s,I am getting the following error. make: Fatal error in reader: Makefile, line 10: Unexpected end of line seen Few of the content: # # Makefile # # The global Makefile for POCO # #sinclude config.make ... (0 Replies)
Discussion started by: prasperl
0 Replies

7. Shell Programming and Scripting

for loop with internal unix command in statement throwing error

Hi I've gotten a plugin script that won't run. I keeps throwing an error at the following line. for BARCODE_LINE in `cat ${TSP_FILEPATH_BARCODE_TXT} | grep "^barcode"` do #something done The error reads ... (3 Replies)
Discussion started by: jdilts
3 Replies

8. UNIX for Dummies Questions & Answers

Mailx command in unix shell script, its throwing below error

How to use Mailx command in unix shell script, its throwing below error #!/bin/ksh let x=3 If ; then mailx -s “ $x is greater than 2” example@gmail.com << EOF This is the message body EOF fi its throwing error as syntax error at EOF... (10 Replies)
Discussion started by: only4satish
10 Replies

9. Shell Programming and Scripting

sendmail script throwing an error "No recipient addresses found in header"

Hi, I am using following code to send an e-mail with attachment and body. echo "To: user1@mail.com,user2@mail.com" > mail.tmp echo "Cc: user3@mail.com,user4@mail.com" >> mail.tmp echo "From: group@mail.com" >> mail.tmp echo "Subject: my report" >> mail.tmp echo "please see as attached"... (6 Replies)
Discussion started by: vivek_damodaran
6 Replies

10. Shell Programming and Scripting

for loop throwing an error

Hi Guys, I am trying a simple for loop which is throwing an error. Below is my code: #/bin/sh set -A array "are" "you" "there"; for ( i = 0 ; i < ${#array} ; i++ ) do echo ${array} done I am getting this error tci001wasd02 $ sh -vx array_for.sh #/bin/sh set -A array "are"... (6 Replies)
Discussion started by: mac4rfree
6 Replies
Login or Register to Ask a Question