简体   繁体   English

Bash脚本从Unix获取日期

[英]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". 我正在使用将从系统捕获日期(星期几)的脚本,如果星期几是星期五,脚本将打印消息“ 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: 您需要告诉shell执行命令,然后将其输出存储在变量中:

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 [ . bash的比较应该由[ You could man [ for more details. 您可以man [更多信息。

$ 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: 如果您很幸运能够达到Bash≥4.2,则可以使用printf%(...)T修饰符:

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

will print today's week day, (1=Monday, 2=Tuesday, etc.). 将打印今天的星期几(1 =星期一,2 =星期二,依此类推)。 See man 3 strftime for a list of supported modifiers, as well as Bash's reference manual about %(...)T . 有关支持的修饰符的列表,请参见man 3 strftime ,以及有关%(...)T Bash参考手册。

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! 纯Bash,无子弹!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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