Sunday, 15 April 2012

bash - how to make a simple if statement is too long? -



bash - how to make a simple if statement is too long? -

this first bash script, info want time each hr of info reduction, created bash script running, think long utilize statement "if ... else", perchance loops for, while , until?

#!/bin/bash x=$(date +"%h") y=$(sed -n "/end_time/=" file.txt) if [ $x = 00 ]; s=$(($y-24)) echo "this time 00:00" elif [ $x = 23 ]; s=$(($y-1)) echo "this time 23:00" elif [ $x = 22 ]; s=$(($y-2)) echo "this time 22:00" elif [ $x = 21 ]; s=$(($y-3)) echo "this time 21:00" elif [ $x = 20 ]; s=$(($y-4)) echo "this time 20:00" . . . elif [ $x = 01 ]; s=$(($y-23)) echo "this time 01:00" else echo "this time not data" fi z=$(awk 'nr=='$s' {print $0}' file.txt) #print echo "time : " $x echo "line end_time : " $y echo "show line info : " $z

this sample info file.txt :

0 3419973 1 2302205 2 1535190 3 1045063 4 895020 5 1275980 . . . . 21 6953924 22 6423911 23 5075690 end_time

if want info in "file.txt" when @ 21:00, print:

time : 21:00 line end_time : 24 show line info : 21 6953924 *(i looking this)*

this run cron. if can help me?

i think can cut down cases to:

case $x in 24) s = $(($y-1)) echo "this time 00:00" ;; 23 | 22 | ... | 01) # or [01][0-9] | 2[0-3]) s = $(($y - 25 + $x)) echo "this time $x:00" ;; *) echo "this time not data" ;; esac

bash shell if-statement

No comments:

Post a Comment