Sponsored Content
Full Discussion: If else then exit
Top Forums Shell Programming and Scripting If else then exit Post 302344513 by kshji on Monday 17th of August 2009 01:06:22 AM
Old 08-17-2009
[ is test command, not bracket. Shell script syntax look like some programming language, but it's only group of command lines. if test next commands exit status. So we don't need to test exit code using [, because if do it.

if syntax is:
Code:
if commandlist
then
     # do this commandlines if exit status is 0 in the last command in command list
else
     # exit status is not 0, do this commandlines
fi

More if and testing.

Solution after those data:
Code:
# no need to echo grep output
# if grep exit code is 0 then mailx, else exit
if grep notice $file >/dev/null 2>&1
then
    mailx -s " MESSAGE " abc@xyz.com < $file
else
    exit 1   # only example, if not ok, exit with some not 0 value
fi

Or if not grep then exit
Code:
if ! grep notice $file >/dev/null 2>&1
then
    exit 1
fi
   
mailx -s " MESSAGE " abc@xyz.com < $file

We can write it shortly without if-then-else like shantanuo has done.
Code:
grep some >/dev/null 2>&1 && mailx
#or
grep some >/dev/null 2>&1 || exit 1
mailx

&& = if previous command exit code is 0, then do next
|| = if previous command exit code is not 0, then do next
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies

2. Shell Programming and Scripting

exit

Hi all, I am confused about When and where to use exit 0 and exit 1 ... Thanks (2 Replies)
Discussion started by: dhananjaysk
2 Replies

3. Programming

exit(0) versus exit(1)

What is the difference between using exit(0) and exit(1) to exit a program? Which should I use? (9 Replies)
Discussion started by: enuenu
9 Replies

4. Shell Programming and Scripting

exit 2

Hi even though I use exit in my first scripts, I am not sure exactly about exit codes.I know there is a relation between return and exit codes, exit 0 means it returned a 0 to indicate there is no error at the end of this point. But what does it mean: exit 1 , probably there was an error, OK... (2 Replies)
Discussion started by: xramm
2 Replies

5. UNIX for Dummies Questions & Answers

what is meaning of exit(0) and exit(1)

can u tell me what is the meaning of exit(0),exit(1),exit(2) what is diff amonng these. Amit (1 Reply)
Discussion started by: amitpansuria
1 Replies

6. UNIX for Dummies Questions & Answers

Exit or logout

When I want to close my session I can use exit or logout, and a lot of manuals use both of them indistinctly. So, Is there any difference between exit or logout?? Or are both of them exactly the same command?? I forgot to mention that I am using a RedHat Unix. Thanks for your help. Kinai. (1 Reply)
Discussion started by: kinai
1 Replies

7. Shell Programming and Scripting

RE: exit value

I am running HP-UX & ksh I have several validation programs that scan log files for error messages. One of these files scan 3 diff files, thus I have the exit value in a variable and depending on which log-file I am scanning the value changes. I am not getting the value I expect but a... (1 Reply)
Discussion started by: vslewis
1 Replies

8. UNIX for Dummies Questions & Answers

What does it mean and how do I do it: exit 0 fi ??

I use a Mac and need to 'echo' a code in >> /etc/hosts Where is: /etc/hosts? And how do I do : exit 0 Does : 'fi' mean something too? :confused:Thanks for any help, Jacqrav:confused: (1 Reply)
Discussion started by: jacqrav
1 Replies

9. Shell Programming and Scripting

If Error then Exit

HI I am just using cd Command and i want exit if error. Ex. cd /hp/pp/0720 If above folder in not available then stop the script Folder is change every day Thanks (3 Replies)
Discussion started by: asavaliya
3 Replies

10. Shell Programming and Scripting

Need the difference between exit 1 & exit 7

Hi In one of the script I am seeing some thing like exit 7,exit 1,exit 2,exit 3,exit 9,exit6.What is the difference between all of this exit.Can anyone help here please (3 Replies)
Discussion started by: ginrkf
3 Replies
ZGREP(1)						      General Commands Manual							  ZGREP(1)

NAME
zgrep - search possibly compressed files for a regular expression SYNOPSIS
zgrep [ grep_options ] [ -e ] pattern filename... DESCRIPTION
Zgrep invokes grep on compressed or gzipped files. These grep options will cause zgrep to terminate with an error code: (-[drRzZ]|--di*|--exc*|--inc*|--rec*|--nu*). All other options specified are passed directly to grep. If no file is specified, then the standard input is decompressed if necessary and fed to grep. Otherwise the given files are uncompressed if necessary and fed to grep. If the GREP environment variable is set, zgrep uses it as the grep program to be invoked. EXIT CODE
2 - An option that is not supported was specified. AUTHOR
Charles Levert (charles@comm.polymtl.ca) SEE ALSO
grep(1), gzexe(1), gzip(1), zdiff(1), zforce(1), zmore(1), znew(1) ZGREP(1)
All times are GMT -4. The time now is 06:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy