Zerg12
Zerg12

Reputation: 317

Bash script to awk the date from Unix

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

Upvotes: 0

Views: 1198

Answers (3)

gniourf_gniourf
gniourf_gniourf

Reputation: 46823

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!

Upvotes: 0

Jiaming Lu
Jiaming Lu

Reputation: 885

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 !!!!

Upvotes: 0

Adam Liss
Adam Liss

Reputation: 48290

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 ]

Upvotes: 1

Related Questions