简体   繁体   中英

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:

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.

Does anyone have any ideas how to fix this?

Linux Scripting is not syntax flexible.

first of all you must use HM rather hm , small hm will give you date in string.

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
http://linuxhacks.in

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