Bash exec wrapper script - multiple commands passed in as arguments

Refresh

February 2019

Views

1.6k time

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 answers

4

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
$