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
cron Executed Script Can't Find Command Chivalry Shell Programming and Scripting 3 03-12-2009 03:01 PM
Trouble with tee command to capture script outputs muthubharadwaj Shell Programming and Scripting 5 02-16-2009 08:29 PM
What command or script to capture a system snapshot? SecureMe Security 1 12-29-2008 06:40 PM
Host command NOT getting executed in Hot backup script. user__user3110 High Level Programming 4 07-17-2008 04:45 AM
perl - why is the shell script executed before the print command? mjays Shell Programming and Scripting 3 09-21-2007 05:49 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 09-30-2009
deepaksinbox deepaksinbox is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 6
Question Capture Last Command Executed inside Script

Hi,

I have a few old software setup scripts written as shell scripts which contains 100s of lines of shell commands for setup & configuration of our software (name of software not relevant for this topic). The script in general does lot of mkdir, chmod, chgrp, awk, cat kind of command executions.

These setup scripts lack one basic feature: if an error occurs, the scripts don't exit and keep continuing. I am now trying to introduce this feature in an existing script.

I wish to add at the end of (probably) each statement in the shell script a function like "stop_on_error", which basically checks $? and exits if non-zero, but I also wish to capture what was the command that failed.

How can I do this ?

An example would be:

Code:
 
setup.sh
------------
#/bin/sh
echo "setup"
mkdir "sas"
chgrp -R sasgrp sas 
..
..
userlist=`awk ..`
..
..
echo $userlist
I intend to add "; stop_on_error;" on each line. Here is the modified script:

Code:
 
 
setup.sh
-----------
#/bin/sh
stop_on_error () {
if [ "$?" = "0" ]; then exit 1
}
echo "setup" ; stop_on_error;
mkdir "sas" ; stop_on_error;
chgrp -R sasgrp sas ; stop_on_error;
..
..
userlist=`awk ..` ; stop_on_error;
..
..
echo $userlist ; stop_on_error;
When a command above actually fails (e.g say chgrp), then I want to print that command as well, e.g: Ideal output would be:

Code:
 
$ ./setup.sh
setup
<user message> command chgrp -R sasgrp sas failed
Any ideas ? Any ideas of how I can approach the problem itself in a better way ?

I am using AIX 5.3

Last edited by deepaksinbox; 09-30-2009 at 09:34 AM.. Reason: Added Unix Version
 

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 09:13 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