Creating a run script, getting pipestatus from eval


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating a run script, getting pipestatus from eval
# 1  
Old 01-29-2010
Creating a run script, getting pipestatus from eval

Hi All, I'm new to bash so I appreciate any help. Basically I want to create a script that takes 1 argument, a string from the command line. It then executes that string emailing me the std out and std err. And lastly it checks the exit status of the job and sends me an email telling me if the job was completed successful or if it failed.

My problem is getting the exit status of a piped command in an eval statement, I've searched the board and googled extensively but no luck. Here's what it looks like:

When I use the script I call it like this:
Code:
./myRunScript.sh "my Program --arg1 --arg2 < some code"
./myRunScript.sh "scp source dest"

etc.
Code:
#!/bin/bash
. ~/prod.profile
. ~/.keychain/xxx > /dev/null

execString="$1 2>&1 | mail -s \"Executing: $1\" myEmail@somewhere.com"
eval $execString

exitCode=${PIPESTATUS[0]}

if [ $exitCode -eq 0 ]; then
  echo $exitCode | mail -s "Job Success: $1" myEmail@somewhere.com
else
  echo $exitCode | mail -s "Job FAILURE: $1" myEmail@somewhere.com
fi

Here is my script, I've tried not using eval and just running the command with the following line, and it captures the exit status from pipestatus correctly but it can execute strings with redirects!! Any ideas??
Code:
$1 2>&1 | mail -s \"Executing: $1\" myEmail@somewhere.com
exitCode=${PIPESTATUS[0]}


Last edited by Scott; 01-29-2010 at 06:47 PM.. Reason: Please use code tags
# 2  
Old 01-30-2010
You can check the exit code in the exec string:

Code:
command && echo Success || echo Failure

In your script it should be something like:

Code:
execString="$1 2>&1 && mail -s \"Executing: $1 Job Success: $1\" myEmail@somewhere.com" || mail -s \"Executing: $1 Job FAILURE: $1\" myEmail@somewhere.com"

# 3  
Old 01-30-2010
Hi Franklin, thanks for your reply.

The problem with your solution is that I can't email the command output and std error. When I take the original command and pipe it into mail the last exit status is always 0. example:

dummy | mail -s testing someone@somewhere.com && echo SUCCESS || echo FAILURE

Will always echo SUCCESS, I need to be able to check the ${PIPESTATUS[0]} to see if the original command worked or not. Is there a way to do a one line if then statement to check the pipe status variable? This could work.

I like your solution but I'm trying to avoid having to write the command output and std err to a file and I don't want to have to run the command twice.
# 4  
Old 01-31-2010
Actually there's no need to use eval and an exec string, try this approach:
Code:
#!/bin/bash

. ~/prod.profile
. ~/.keychain/xxx > /dev/null

$@ > my_msg 2>&1

if [ $? -eq 0 ]; then
  mail -s "Executing: $@ Job Success" myEmail@somewhere.com < my_msg
else
  mail -s "Executing: $@ Job FAILURE" myEmail@somewhere.com < my_msg
fi

rm -f my_msg

# 5  
Old 01-31-2010
There are 2 problems with this method. First I don't want to write the output of the command to a file, I will need to be running lots of different jobs all the time and there will be lots of occasions with multiple jobs running at the same time, so they can't all be accessing the same my_msg file.

Secondly, I can't pass it any command strings that have a redirect statement. For example:

./runScript "R --no-save < myCode"

Is there a way to execute a string that has pipes or redirects in a bash script without using eval ??
# 6  
Old 01-31-2010
The problem with eval is that bash consider it a simple command (not a pipe line), so PIPESTATUS only has one value for the exit code of eval. You can do:
Code:
$ x='exit 1|exit 2|exit 3'
$ eval "$x; typeset -a a=(\${PIPESTATUS[@]})"
$ echo ${a[@]}
1 2 3

This User Gave Thanks to binlib For This Post:
# 7  
Old 02-01-2010
BinLib you are my hero!!!

Using type set appears to have worked like a charm! Thank you so much. One last question, what exactly is happening under the hood with typset, is the $a variable that is being created exist as a new environment variable?

My concern is the following: If I use this script with cron to run multiple jobs at the same exact time will the all be trying to accesses and set the value of $a?? If the answer is yes then this solution won't work. If it's the case that each time the run script is called each job has it's own individual copy of $a then this works perfectly. Do you happen to know?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash ${PIPESTATUS[@]}

I'm using this form of variable setting to use the exit status of a pipeline e.g. foo=$(date|grep -i thursday)$? echo $foo echo ${PIPESTATUS} or $foo later in a script. On days other than Thursday it results in: 1 0 For some reason the PIPESTATUS array doesn't show. I'd... (2 Replies)
Discussion started by: spacegoose
2 Replies

2. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (13 Replies)
Discussion started by: vivek d r
13 Replies

3. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. Fedora

Creating Windows 7 image to run in VirtualBox

Hey guys, Not sure if this is the best place for this question, but, after using Fedora now for about a year, I'm still getting used to it but I'm using it practically all the time now and instead of having a dual boot with Windows 7, I want to create an image of my Windows 7 installation,... (3 Replies)
Discussion started by: jimbob01
3 Replies

5. Shell Programming and Scripting

Strange result of eval, how does eval really work with ssh?

Hi all, some small script with eval turned me to crazy. my OS is linux Linux s10-1310 2.6.16.53-0.8.PTF.434477.3.TDC.0-smp #1 SMP Fri Aug 31 06:07:27 PDT 2007 x86_64 x86_64 x86_64 GNU/Linux below script works well #!/bin/bash eval ssh remotehost date eval ssh remotehost ls below... (1 Reply)
Discussion started by: summer_cherry
1 Replies

6. Shell Programming and Scripting

Creating a daemon to run in background

I am trying to create a service to always run and monitor a script that has a tendency to hang, we could not find what is causing it to hang so are in the process of completely reprogramming just about everything, however, that will take upto 6 months. So I need to create this to monitor the... (5 Replies)
Discussion started by: ukndoit
5 Replies

7. Shell Programming and Scripting

help needed with creating challenging bash script with creating directories

Hi, Can someone help me with creating a bash shell script. I need to create a script that gets a positive number n as an argument. The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So... (7 Replies)
Discussion started by: I-1
7 Replies

8. Shell Programming and Scripting

Creating a Schedular to Run the Jobs.

Hi, I have to create a sheduler to run the shell scripts one by one(dependency). Suppose i have 10 scripts. For all the 10 scripts i have a database table containing these scripts_names and status. Executing the first script.if the first status script is only 'success' then go to next... (4 Replies)
Discussion started by: laknar
4 Replies

9. UNIX for Dummies Questions & Answers

creating /var/run

I havn't installed solaris 10 (nor any solaris in a long time). I noticed that during the install the file system layout allowed me to create a swap partition, but there wern't enough partitions to create a /var/run (I am mirroring the disk config of another system). Is /var/run created manually... (2 Replies)
Discussion started by: csgonan
2 Replies
Login or Register to Ask a Question