the problem is, that you are for f in *; do echo $f; done
is not a command, but rather it is bash-code, which needs a bash-interpreter to be executed.
you can start an interpreter by using eval
:
#!/bin/sh
PID="$1"
shift
JOB="$@"
echo "Waiting job ${PID} to be finished..."
while ps -p ${PID} >/dev/null; do sleep 1; done ;
echo "Job ${PID} finished."
echo "Now running job '${JOB}'"
eval ${JOB}
echo "Job '${JOB}' finished."
then use single quotes for commands including variables that should be evaluated by After
:
./After.sh 123 'for f in *; do echo $f; done'