Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如果语句太长,如何制作简单的if语句?_Bash_Shell_If Statement - Fatal编程技术网

Bash 如果语句太长,如何制作简单的if语句?

Bash 如果语句太长,如何制作简单的if语句?,bash,shell,if-statement,Bash,Shell,If Statement,这是我的第一个bash脚本,为了获得每小时数据缩减所需的时间,我创建了一个已经在运行的bash脚本,但我认为使用“if…else”语句太长,可能会包含for、while和until的循环 #!/bin/bash x=$(date +"%H") y=$(sed -n "/END_TIME/=" file.txt) if [ $x = 00 ]; then s=$(($y-24)) echo "this time 00:00" elif [ $x = 23 ]; th

这是我的第一个bash脚本,为了获得每小时数据缩减所需的时间,我创建了一个已经在运行的bash脚本,但我认为使用“if…else”语句太长,可能会包含for、while和until的循环

#!/bin/bash
x=$(date +"%H")
y=$(sed -n "/END_TIME/=" file.txt)
if [ $x = 00 ]; then
        s=$(($y-24))
        echo "this time 00:00"
elif [ $x = 23 ]; then
        s=$(($y-1))
        echo "this time 23:00"
elif [ $x = 22 ]; then
        s=$(($y-2))
        echo "this time 22:00"
elif [ $x = 21 ]; then
        s=$(($y-3))
        echo "this time 21:00"
elif [ $x = 20 ]; then
        s=$(($y-4))
        echo "this time 20:00"
        .
        .
        .
elif [ $x = 01 ]; then
        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 Data : " $z
此示例数据文件为.txt:

0 3419973
1 2302205
2 1535190
3 1045063
4 895020
5 1275980
.
.
.
.
21 6953924
22 6423911
23 5075690
END_TIME
如果我想在21:00时获取“file.txt”中的数据,它将打印:

Time           : 21:00
Line END_time  : 24 
Show Line Data : 21 6953924 *(I was looking for this)*

这是用cron运行的。如果你能帮助我的话?

我想你可以把案例减少到:

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

使用switch语句而不是if语句。我希望他在没有case语句的情况下自动运行。感谢您的建议@AndersonGreen,除了switch语句之外,还有其他人吗?
date'+%H'
不会在午夜返回24。从手册页:“
%H hour(00..23)
”感谢您的更正@try catch finally..如果这真的是您想要做的:
date--date=“1hour ago”'+%H'
对于GNU date最方便(在大多数Linux版本上)。
-z
如果字符串为空则为真,否则为假谢谢@Sergey Fedorov,还有一个让我困惑的问题是,“如果[-z”$time“]”,那么“-z”是什么意思?谢谢你的建议@hjpotter92,如果我喜欢我刚刚更新的问题,是否也可以使用这个?为什么不使用
((s=y-1))
((s=y-25+x))
#!/bin/bash

x=$(date +"%H")
y=24

unset time
for h in `seq 0 23`
do
    if [ $x -eq $h ]
    then
        time="${x}:00"
        s=$(( $y - ( 24 - $x ) ))
        break
    fi
done

if [ -z "$time" ]
then
    echo "this time not data"
else
    echo "this time $time"

    #proceed with awk
fi