1

So I'm trying to write a shell script to take an input as below:

bash whichday Jan 10 2017

and it should take that input and print out what day it occurs on. Below is the script I'm trying to use:

#!/bin/bash
if [ $# -ne 3 ]
    then
    echo "Please enter the month day year (example: Jan 5 2002)"

else
    str=\"$1" "$2" "$3\"
    echo $str
    date -d $str +%A
fi 

but everytime I run this, I get the following output:

"Jan 10 2017"
date: extra operand ‘2017"’
Try 'date --help' for more information.

When I do

date -d "Jan 10 2017" +"%A"

on command line, it outputs the correct day (Tuesday), but the script, which should be executing the exact same command, doesn't work. Please help, I don't understand why this is happening.

3

Just set

str="$1 $2 $3"

and then

date -d "$str" +'%A'

By escaping the double quotes, you making them part of the value of $str, and date can't parse them.

You may also bypass the str variable completely by using $*:

echo "$*"
date -d "$*" +'%A'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.