大于-小于break bash脚本的倍数-a

问题描述:

我写了一个bash脚本,仅在工作时间执行一次curl调用.由于某种原因,当我添加"-a"运算符时(由于某种原因,我的bash无法识别&&"),每小时比较失败.

I wrote a bash script that performs a curl call only during business hours. For some reason, the hourly comparison fails when I add an "-a" operator (and for some reason my bash does not recognize "&&").

尽管脚本要大得多,但这是相关的部分:

Though the script is much larger, here is the relevant piece:

HOUR=`date +%k`

if [ $HOUR > 7 -a $HOUR < 17 ];
then
  //do sync
fi

脚本给我错误:

./tracksync: (last line): Cannot open (line number): No such file

但是,这种比较不会失败:

However, this comparison does not fail:

if [ $DAY != "SUNDAY" -a $HOUR > 7 ];
then
  //do sync
fi

我的语法错误还是bash的问题?

Is my syntax wrong or is this a problem with my bash?

您不能在bash脚本中使用<>.为此使用-lt-gt

You cannot use < and > in bash scripts as such. Use -lt and -gt for that:

if [ $HOUR -gt 7 -a $HOUR -lt 17 ]

Shell使用

<>来执行stdin或stdout的重定向.

< and > are used by the shell to perform redirection of stdin or stdout.

您所说的比较实际上是在当前目录中创建一个名为7的文件.

The comparison that you say is working is actually creating a file named 7 in the current directory.

&&一样,它对于shell也具有特殊的含义,并用于创建命令的"AND列表".

As for &&, that also has a special meaning for the shell and is used for creating an "AND list" of commands.

所有这些的最佳文档:man bash(和man test有关比较运算符的详细信息)

The best documentation for all these: man bash (and man test for details on comparison operators)