bash /bin/sh: 语法错误:未终止的引用字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5473876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:20:52  来源:igfitidea点击:

/bin/sh: Syntax error: Unterminated quoted string

bashubuntu

提问by oompahloompah

I am trying to get a cronjob to pipe output into a dated file in a specified (folder) location.

我正在尝试使用 cronjob 将输出通过管道传输到指定(文件夹)位置的过时文件中。

My crontab entry looks something like this:

我的 crontab 条目如下所示:

* * * * * /some/path/test.sh >> $(date "+/home/oompah/logs/%Y%m%d.test.log")

What I don't understand is that when I type this command at the console, I get the correct string:

我不明白的是,当我在控制台输入这个命令时,我得到了正确的字符串:

echo $(date "+/home/oompah/logs/%Y%m%d.test.log")
/home/oompah/logs/20110329.test.log

What's causing this error and how may I fix it?

是什么导致了这个错误,我该如何解决?

bash version info is:

bash 版本信息是:

GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)

GNU bash,版本 4.1.5(1)-release (x86_64-pc-linux-gnu)

回答by bmk

You should excape the percent signs in your crontab:

您应该在 crontab 中排除百分号:

* * * * * /some/path/test.sh >> $(date "+/home/oompah/logs/\%Y\%m\%d.test.log")

Percent (%) signs have a special meaning in crontabs. They are interpreted as newline characters.

百分比 ( %) 符号在 crontab 中具有特殊含义。它们被解释为换行符。

回答by bmargulies

Put the date command inside the script. cronisn't necessarily running the shell you think it is.

将日期命令放在脚本中。cron不一定运行您认为的 shell。

回答by kurumi

make sure you have shebang #!/bin/bashas the first line in your script. Also, as bmarguliesstated, put the datecommand inside the script if possible.

确保您将shebang#!/bin/bash作为脚本中的第一行。另外,bmargulies如上所述,date如果可能,将命令放在脚本中。