简体   繁体   中英

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

Your script, reprise

#!awk -f
BEGIN {
  if (strftime() ~ /Fri/)
    print "It is Friday!"
}

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 ]

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM