1

Essentially I have a script which acts as a task wrapper and emails a user if the task fails.

The task is passed in as an argument to the script. The problem comes when we need to run multiple commands say the following line is passed to the script as the task arg "echo this; echo that" would output this; echo that.

So the question is what is the easiest way to run multiple commands without having to loop through the input command string and split on the ';' char?

Simple example:

FIRST=$1
TASK=$*
echo run
echo "emailing $FIRST"
$TASK
echo done

and to run this script we would use ./wrapper.sh "[email protected]" "echo this; echo that"

Suggestions?

1 Answer 1

7

If you execute the string $TASK via sh -c, you might not need to do any more work:

$ sh -c "echo this ; echo that"
this
that
$ 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - is there anyway to get the return value if any command in the command string fails. Say "echo this; dsddsdsd; echo that", the middle command will fail (not found) yet $? will still be 0 as echo succeeds. So the question is using sh I can halt on failure with the -e switch but can I run through each command to return any non-zero errors (Im guessing no but worth a shot).
Hrm, I cannot think of any easy mechanism to do that; can you instead change your script to accept multiple commands as multiple arguments? e.g. wrapper.sh alert@hostname "echo this" "echo that"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.