with a basic shell script such as
USER=$1
CMD=$2
/usr/bin/sudo -u $USER $CMD
I can run
# ./script.sh root "/bin/echo 'apple pie'"
'apple pie'
However trying instead:
#/script.sh root "/bin/date -s '2011-08-24 15:24:30'"
date: the argument `15:24:30\'' lacks a leading `+';
when using an option to specify date(s), any non-option
argument must be a format string beginning with `+'
Try `date --help' for more information.
I can replicate the date error by manually typing
/bin/date -s \'2011-08-24 15:24:30\'
date: the argument `15:24:30\'' lacks a leading `+';
when using an option to specify date(s), any non-option
argument must be a format string beginning with `+'
Try `date --help' for more information.
So my problem then with using date in this script is that the single-quotes are getting automatically escaped which date does not like. I can change the script to be based on $1 $2 $3 “$4” but that removes the general purpose nature of this script (which has been simplified for this post).
Any ways to make a script of this nature support the date case without special casing it?
Try this:
The “$@” preserves the quotes and you should run your script like this instead:
Note the lack of double quotes.