gpt4 book ai didi

bash - 参数太多 Shell 脚本如果检查

转载 作者:行者123 更新时间:2023-12-02 09:03:14 32 4
gpt4 key购买 nike

我正在尝试检查这一天是星期日还是星期五,以及时间是否晚于 20 小时。
如果是这样,那我想做点什么;如果没有,我想做点别的。
我正在使用 /bin/sh

这给了我以下错误:

Too many arguments.

为什么?

   day_of_week=$(date -d $given_date +%u )
hour_of_day=${ST_job:0:2}
if [ [ $day_of_week -eq 7 ] || [ [ $day_of_week -eq 5 ] && [ $hour_of_day -gt 20 ] ] ] ; then
something
else
something else
fi

谢谢

最佳答案

您可能认为“[”只是一个分组,您可以有不同的任意嵌套。

不是。

["是 shell "test"命令的别名……你需要正确匹配它们:

https://www.shellscript.sh/test.html

Test is used by virtually every shell script written. It may not seem that way, because test is not often called directly. test is more frequently called as [. [ is a symbolic link to test, just to make shell programs more readable. It is also normally a shell builtin (which means that the shell itself will interpret [ as meaning test, even if your Unix environment is set up differently):

$ type [
[ is a shell builtin
$ which [
/usr/bin/[
$ ls -l /usr/bin/[
lrwxrwxrwx 1 root root 4 Mar 27 2000 /usr/bin/[ -> test
$ ls -l /usr/bin/test
-rwxr-xr-x 1 root root 35368 Mar 27 2000 /usr/bin/test

在此处查看更多详细信息(以及一些有用的示例):

Advanced Bash-Scripting Guide: 7.4. Nested if/then Condition Tests

关于bash - 参数太多 Shell 脚本如果检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61515485/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com