0

I am working on the script that will capture date (the day of the week) from the system and if the day of the week is Friday the script will print the message "Any message". I think I am missing some syntax in my script

#!/bin/bash
input_source=date|awk '{print $1}'
if $input_source=Fri
then
  echo 'It is Friday !!!!'
else
  exit
fi

3 Answers 3

1

You need to tell the shell to execute the commands, then store their output in the variable:

input_source=$(date|awk '{print $1}')

You'll also need to enclose your test in square brackets:

if [ $input_source = Fri ]
Sign up to request clarification or add additional context in comments.

Comments

0

Command output should be surrounded by `` or $()

And comparisons in bash should be done by [. You could man [ for more details.

$ input=`date | awk '{print $1}'`
$ echo $input
Thu
$ if [ $input=Thu ]; then   echo 'It is Thursday !!!!'; else   exit; fi
It is Thursday !!!!

Comments

0

If you're lucky enough to have Bash≥4.2, then you can use printf's %(...)T modifier:

printf '%(%u)T\n' -1

will print today's week day, (1=Monday, 2=Tuesday, etc.). See man 3 strftime for a list of supported modifiers, as well as Bash's reference manual about %(...)T.

Hence:

#!/bin/bash

printf -v weekday '%(%u)T' -1

if ((weekday==5)); then
    echo 'It is Friday !!!!'
else
    exit
fi

Pure Bash and no subshells!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.