我有一个Django项目。这是一个函数,聚合博客文章,这个功能附加到网址。我需要在约定的时间运行此功能(url)。为此,我使用cron我编写了shell脚本,但我在shell脚本中使用了noob并且它不起作用。 请告诉我它有什么问题?
OUT=$( curl -qSfsw '\n%{http_code}' url ) 2>/dev/null
RET=$?
if [[ $RET -ne 0 ]]; then
echo "Adding of blogs was failed: $(echo "$OUT" | tail -n1 )" | mail -s "Adding of blogs was failed" baartt11@gmail.com
else
echo "Blogs were added successfully" | head -n-1 | mail -s "Blogs were added successfully" baartt11@gmail.com
fi
错误文字:
./update_blogs.sh: line 7: syntax error near unexpected token `fi'
./update_blogs.sh: line 7: `fi'
答案 0 :(得分:0)
你正在使用管道“|”,因此,它认为这是comande的结束。要解决这个问题,你必须使用“(”和“)”:
对commandes进行分组OUT=$( curl -qSfsw '\n%{http_code}' http://appbrainiac.com/api/daily_blogs_update/ ) 2>/dev/null
RET=$?
if [[ $RET -ne 0 ]]; then
( echo "Adding of blogs was failed: $(echo "$OUT" | tail -n1 )" | mail -s "Adding of blogs was failed" baartt11@gmail.com )
else
( echo "Blogs were added successfully" | head -n-1 | mail -s "Blogs were added successfully" baartt11@gmail.com )
fi
答案 1 :(得分:0)
当我使用你的脚本(剪切并粘贴它)并通过sh -n scriptname
(测试sh语法)运行它时,我无法重现这个问题。
但是,当使用DOS行结尾(\r\n
)时,我可能会导致类似的错误。这个脚本是否以某种方式从Windows机器转移?是用Windows编辑器编辑的吗?
要验证这是否是问题,请运行
od -bc scriptname
并查看脚本中是否有回车 \r
个字符。如果是这样,您可以使用
dos2unix scriptname