简体   繁体   English

在Unix Bash脚本的if / fi语句中定义变量

[英]Defining Variable Within if/fi statement In A Unix Bash Script

Good Morning All, 大家早上好,

Currently, I am trying to define a variable within an if/fi statement in a bash script that checks the current system time and as a result of the current system time, defines a variable: 当前,我正在尝试在bash脚本的if / fi语句中定义一个变量,该变量检查当前系统时间,并根据当前系统时间定义一个变量:

TIME=`date +%h%m%s`

if [ ${TIME} -ge 050000 ]; then RUNHR=00 fi
if [ ${TIME} -ge 110000 ]; then RUNHR=06 fi
if [ ${TIME} -ge 170000 ]; then RUNHR=12 fi
if [ ${TIME} -ge 225900 ]; then RUNHR=18 fi

After I do this however, the RUNHR value doesn't stick and is never defined. 但是,在执行此操作之后,RUNHR值不会固定,也从未定义。

Does anyone have any ideas how to fix this? 有谁知道如何解决这个问题?

Linux Scripting is not syntax flexible. Linux脚本不是语法灵活的。

first of all you must use HM rather hm , small hm will give you date in string. 首先,您必须使用HM而不是hm,小hm会给您字符串形式的日期。

This is tested. 经过测试。

TIME=`date +%H%M%s`

if [ ${TIME} -ge 050000 ]; then 
RUNHR=00 
fi
if [ ${TIME} -ge 110000 ]; then 
RUNHR=06 
fi
if [ ${TIME} -ge 170000 ]; then 
RUNHR=12 
fi
if [ ${TIME} -ge 225900 ]; then 
RUNHR=18 
fi

echo $RUNHR

Thanks 谢谢

Suyash Jain 苏亚什·贾恩(Suyash Jain)
http://linuxhacks.in http://linuxhacks.in

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

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