Bash 未正确测试的条件句

Bash 未正确测试的条件句,bash,syntax,Bash,Syntax,我有一个脚本,每次我的Ubuntu机器醒来睡觉时都会运行 #!/bin/sh if [ "${1}" == "pre" ]; then # Do the thing you want before suspend here, e.g.: echo "we are suspending at $(date)..." > /home/albin/stuff/suspend_test elif [ "${1}" == "post" ]; then # Do the thing you

我有一个脚本,每次我的Ubuntu机器醒来睡觉时都会运行

#!/bin/sh
if [ "${1}" == "pre" ]; then
  # Do the thing you want before suspend here, e.g.:
  echo "we are suspending at $(date)..." > /home/albin/stuff/suspend_test
elif [ "${1}" == "post" ]; then
  # Do the thing you want after resume here, e.g.:
  echo "...and we are back from $(date)" >> /home/albin/stuff/suspend_test
else
  echo ".2..neither pre or post . $1 .   $(date)" >> /home/albin/stuff/suspend_test
fi
这可以在输出文件中找到:

.2..neither pre or post . pre .   tis 11 sep 2018 20:44:42 CEST
.2..neither pre or post . post .   tis 11 sep 2018 20:45:06 CEST
.2..neither pre or post . pre .   tis 11 sep 2018 22:12:52 CEST
.2..neither pre or post . post .   ons 12 sep 2018 06:55:21 CEST
.2..neither pre or post . pre .   ons 12 sep 2018 06:55:22 CEST
.2..neither pre or post . post .   ons 12 sep 2018 06:55:43 CEST
.2..neither pre or post . pre .   ons 12 sep 2018 07:13:28 CEST
.2..neither pre or post . post .   ons 12 sep 2018 07:14:00 CEST


关于如何编写bash条件语句而不发现任何问题,我查阅了多个指南。$1变量是可用的,但在if语句中被忽略了。很可能,我认为第4行的重定向输出是错误的

必须是:

echo "we are suspending at $(date)..." >> /home/albin/stuff/suspend_test
而不是

echo "we are suspending at $(date)..." > /home/albin/stuff/suspend_test

此外,我认为1美元既不等于“前期”也不等于“后期”。通过查看您发布的输出,我无法理解为什么这些字符串“tis”“on”是从哪里来的。

最有可能的是,我认为您在第4行的重定向输出是错误的

必须是:

echo "we are suspending at $(date)..." >> /home/albin/stuff/suspend_test
而不是

echo "we are suspending at $(date)..." > /home/albin/stuff/suspend_test

此外,我认为1美元既不等于“前期”也不等于“后期”。通过查看您发布的输出,我无法理解为什么这些字符串“tis”“on”来自。

可能是此脚本的调用者向您传递了除post或pre之外的其他参数。
将日志写入更改为使用>>而不是>(以防止始终覆盖日志),并尝试在第一行添加echo“${1}”>>日志,然后您可以看到脚本中的参数可能是此脚本的调用方传递给您的非post或pre参数。
将日志写入更改为使用>>而不是>(以防止始终覆盖日志),并尝试在第一行添加echo“${1}”>>日志,然后您可以看到脚本中的参数是什么,您正在使用
=
而不是
[]
中的
=
,但这是一种巴什主义。Ubuntu的最新版本使用dash作为/bin/sh,它不支持
==
;它获取一个错误,该错误被解释为测试失败:

$ if [ foo == foo ]; then echo "The strings match"; else echo "The strings do *not* match"; fi
dash: 1: [: foo: unexpected operator
The strings do *not* match
解决方案:切换到
=

$ if [ foo = foo ]; then echo "The strings match"; else echo "The strings do *not* match"; fi
The strings match
如果您打算使用
/bin/sh
而不是
/bin/bash
,那么您确实需要注意bashism。在这方面有一个很好的页面。要么这样,要么切换到/bin/bash


另外,如果要使用bash扩展,我建议对条件句使用
[[]]
而不是
[]
——它修复了
[]
的大部分语法异常,并添加了一些有用的新功能,如模式和正则表达式匹配。

您在
[]
中使用的是
=
而不是
=
,但这是一种巴沙主义。Ubuntu的最新版本使用dash作为/bin/sh,它不支持
==
;它获取一个错误,该错误被解释为测试失败:

$ if [ foo == foo ]; then echo "The strings match"; else echo "The strings do *not* match"; fi
dash: 1: [: foo: unexpected operator
The strings do *not* match
解决方案:切换到
=

$ if [ foo = foo ]; then echo "The strings match"; else echo "The strings do *not* match"; fi
The strings match
如果您打算使用
/bin/sh
而不是
/bin/bash
,那么您确实需要注意bashism。在这方面有一个很好的页面。要么这样,要么切换到/bin/bash


另外,如果您打算使用bash扩展,我建议您使用
[[]]
而不是
[]]
作为条件句——它修复了
[]
的大多数语法奇怪之处,并添加了一些有用的新功能,如模式和正则表达式匹配。

什么是
echo“$1”
显示的?顺便说一句,为什么在脚本有
#的时候将其标记为bash呢/bin/sh
在shebang行中?是的,这就是问题所在,当我在使用sh时认为我在使用bash,
echo“$1”
显示了什么?顺便说一句,为什么在脚本有
#的时候将其标记为bash呢/bin/sh
在shebang行中?是的,这就是问题所在,当我使用sh“tis”和“ons”是瑞典语中“星期二”和“星期三”的缩写,“tis”和“ons”是瑞典语中“星期二”和“星期三”的缩写谢谢!解决了,谢谢!解决了。