How to automatically detect command failure


 
Thread Tools Search this Thread
Special Forums UNIX and Linux Applications How to automatically detect command failure
# 1  
Old 04-24-2008
How to automatically detect command failure

I have a shell script. In this script I executes various command and my requirement is such that if any command fails I've to terminate the shell script.

To achieve this objective I'm checking the value of $? after each command and if its value is greater thaen I 'exit' the script.

Is there any way through which I can avoid the if condition on $? after each and every command as It a pain to put if condition after every command.
# 2  
Old 04-24-2008
You can write a function, say "handle_error" where you can define the if condition, and execute this function whenever you need it.
# 3  
Old 04-24-2008
Code:
set -e

This will make the script terminate on any unhandled error.

It's more picky than you think, so a script which was not written to cope with this probably has unchecked commands which might bite you in the back. (It's good for the self-discipline, of course.)

For example, anything like hello && echo success will terminate the script if hello fails. You need to rewrite that as an if ... then, or artificially add || true at the end.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Ubuntu

Detect and run command upon mouse movement

I am trying to find a way to run a command upon any movement of a mouse. The 1st statement shows the mouse co-ordinates. So it can detect mouse movement. xinput test 9 First list input devices: $ xinput list If possible, I would like to use it in a bash script. (2 Replies)
Discussion started by: drew77
2 Replies

2. Shell Programming and Scripting

Htop - How to detect exact command running at background

Hello, I am running a python file from terminal and I wish to see which code is running at background. When I use htop, I see just a few commands, unable to see entire command. htop > report nano report Output: ^ Following parts of ffmpeg line is not shown by htop. Just showing... (7 Replies)
Discussion started by: baris35
7 Replies

3. Shell Programming and Scripting

Tomcat failure detect via shell

Team, I am able to get ERROR message from my shell script, but sometime few severe error happen that tomcat server not started or not responding. How do we can detect it via shell script ? here is my snippet I used for my case tail -n0 -F $catalinaPath | while read line; do if echo... (0 Replies)
Discussion started by: Ghanshyam Ratho
0 Replies

4. Shell Programming and Scripting

Using grep command to detect presence of script run

i have this line of code on a korn shell script to detect the presence of script run: ISRUNNING=`ps -eaf -o args | grep -i sfs_load_file.ksh | grep -v grep | wc -l` sometimes this returns either 1, 2, or 3. when it returns 2 or 3 that tells us that there are more than 1 script of... (8 Replies)
Discussion started by: wtolentino
8 Replies

5. Red Hat

How to detect kind of command for root only?

Dear all, Please help me clarify why i cannot run command in /sbin directory (ex: /sbin/fdisk -l )! I've checked permission on files which belong /sbin directory with execute permission. However, i still cannot run with normal user. Sorry for my English. thanks all, (5 Replies)
Discussion started by: all4cfa
5 Replies

6. AIX

how to detect removable disks or volume in AIX using command

Hi I am new to AIX and any help regarding the same would be really appriciated, thanks In advance. My priority issue is how to detect from command line that the volume / disk on AIX machine is a USB or removable disk /volume and if possible can we list out details for that disk / volume (1 Reply)
Discussion started by: mak_mailbox
1 Replies

7. Solaris

make command failure

Hi After downloading and compiling new ntp source for Solaris 10 I used the make command on the ntp directory. I received the following output: bash-3.00# make (bk version) >/dev/null 2>&1 && \ cd . && \ x=`bk -R prs -hr+ -nd:I: ChangeSet` && \ y=`cat version... (2 Replies)
Discussion started by: shaife720
2 Replies

8. Shell Programming and Scripting

copy command failure

A cp command failure occured on our production system. A shell script copies a source file to a temporary directory - cp <source file on mount point 1> <dest path on mount point 2> The same script ran successfully for another file, about 3mins after the failure. The script did not have a... (1 Reply)
Discussion started by: mynix
1 Replies

9. UNIX for Dummies Questions & Answers

find command to detect installations

Hi I'm wondering how I can find all the files which were installed on certain date? For example: I'm looking for alle the files which were installed on the 11.09.06 on the system. Does somebody know how to do this? Thanks Reto (2 Replies)
Discussion started by: goldenglobe
2 Replies

10. UNIX for Dummies Questions & Answers

failure of at command

This is my first posting. I am not a very sophisticated UNIX user. I am trying to have an existing control string trigger another process at 4:00 p.m. My command looks like this on System V.4 UNIX. at -f /scriptname 16:00 I have added the users to /etc/cron.d/at.allow and at.deny is... (5 Replies)
Discussion started by: DKuester
5 Replies
Login or Register to Ask a Question