problems with changing date through shell script
Hello Friends,
I am trying to change the system date through a shell script. The
scrpt is very simple and works through the plain command. But it
doesnot work through the shell script.
It gives the following output:-
joshi [at] home-comp> sh changeDate.sh '$(/bin/date +0812)$(/bin/date +%H
%M)2007'
Changing the system date to $(/bin/date +0812)$(/bin/date +%H%M)2007
/usr/bin/sudo /bin/date $(/bin/date +0812)$(/bin/date +%H%M)2007
/bin/date: invalid date `$(/bin/date +0812)$(/bin/date +%H%M)2007'
I can't know what is going wrong. If I hardcode the command in the
script, it works:-
/usr/bin/sudo /bin/date $(/bin/date +0712)$(/bin/date +1530)2007
Can you please provide some insight on this thing?
Thank you all for reading this post
The actual script is:-
#!/bin/sh
# Make the sudo wrapper for /bin/date command to change date and time.
# Actual Command:-
# /usr/bin/sudo /bin/date $(/bin/date +0712)$(/bin/date +1530)2007
dateToBeChanged=$1
if [ -n "$dateToBeChanged" ]
then
echo "Changing the system date to $dateToBeChanged"
#echo "/bin/date $dateToBeChanged"
#/bin/date "$dateToBeChanged"
echo "/usr/bin/sudo /bin/date $dateToBeChanged"
/usr/bin/sudo /bin/date "$dateToBeChanged"
else
echo "Error: non empty argument."
echo "Usage: changeSystemDate [date]"
exit 1
fi
exit 0
Re: problems with changing date through shell script
wizard wrote:
> Hello Friends,
> I am trying to change the system date through a shell script. The
> scrpt is very simple and works through the plain command. But it
> doesnot work through the shell script.
>
> It gives the following output:-
> joshi [at] home-comp> sh changeDate.sh '$(/bin/date +0812)$(/bin/date +%H
> %M)2007'
Single quotes will take and pass that string literally to your script
and will not expand the $(...) expression. Use double quotes instead.
Janis
> Changing the system date to $(/bin/date +0812)$(/bin/date +%H%M)2007
> /usr/bin/sudo /bin/date $(/bin/date +0812)$(/bin/date +%H%M)2007
> /bin/date: invalid date `$(/bin/date +0812)$(/bin/date +%H%M)2007'
>
> I can't know what is going wrong. If I hardcode the command in the
> script, it works:-
> /usr/bin/sudo /bin/date $(/bin/date +0712)$(/bin/date +1530)2007
>
> Can you please provide some insight on this thing?
>
> Thank you all for reading this post
>
>
> The actual script is:-
>
> #!/bin/sh
>
> # Make the sudo wrapper for /bin/date command to change date and time.
>
> # Actual Command:-
> # /usr/bin/sudo /bin/date $(/bin/date +0712)$(/bin/date +1530)2007
>
> dateToBeChanged=$1
>
> if [ -n "$dateToBeChanged" ]
> then
> echo "Changing the system date to $dateToBeChanged"
> #echo "/bin/date $dateToBeChanged"
> #/bin/date "$dateToBeChanged"
>
> echo "/usr/bin/sudo /bin/date $dateToBeChanged"
> /usr/bin/sudo /bin/date "$dateToBeChanged"
> else
> echo "Error: non empty argument."
> echo "Usage: changeSystemDate [date]"
> exit 1
> fi
>
>
> exit 0
>