[Solved] Problem insert $1 value into a command

Hi folks

I am trying to use a call to a python command within a shell script to help validate a date. If I hard code the date to be checked in the string it works. As follows

 
cmd_result=$(/usr/bin/python -c 'import time; print time.strftime("%d",time.strptime("31/02/2008", "%d/%m/%Y"))' \
 2>/dev/null | wc -l)
if [ ${cmd_result} -eq 0 ]; then
    echo "Date Invalid "
else
    echo "Date Valid"
fi

If i use $1 instead, i.e. passing the date as an input paramater (e.g ./sysReport.sh 23/04/2011), I cant seem to get the value in $1 to be picked up by the command. I am using the following.

 
datein=$1
cmd_result=$(/usr/bin/python -c "import time; print time.strftime('%d',time.strptime('" ${datein} "', '%d/%m/%Y'))" \
 2>/dev/null | wc -l)
if [ ${cmd_result} -eq 0 ]; then
    echo "Date Invalid "
else
    echo "Date Valid"
fi

I know that datein stores the value of the input parameter $1 but the execution of the python command always returns zero for valid or invalid dates.

I kow I am not using the reference to the ${datein} or $1 varaiable correctly but have tried numerous mechanisms without any joy.

I can easily just some code from the web that validates a date but this is more about why i cant seem to execute the python command and why i cant insert the contents of a variable into the command.

Any help explaining this or helping to find a solution would be appreciated.

Remove spaces:

'"${datein}"'
1 Like

Yazu,

thanks for the spot. It helped but the overall problem manifested itself with how the shell was handling ' and " characters.

not long at shell programming but found out that i can turn on debug (set -x) and it dumped the exact command and I then spotted the confusion.

The correct syntax is as follows:

 
cmd_result=$(/usr/bin/python -c "import time; print time.strftime(\"%d\",time.strptime(""\"${datein}\""", \"%d/%m/%Y\"))" \
 2>/dev/null | wc -l)

Many thanks to Yazu for replying and anyone else for checking the post.