【问题标题】:Unknown error in if statementif 语句中的未知错误
【发布时间】:2016-07-07 17:53:06
【问题描述】:

我在我的 bash 脚本的 if 语句中收到一个未知错误:

Bash 脚本:

#!/bin/bash

TIMESTAMP=$(date +"%Y-%m-%d_%H%M")
DAY=$(date +"%Y-%m-%d")
LUDAY=$(date +"%Y/%m/%d")
LUTIME=$(date +"%H:%M:%S")
mkdir /home/pi/Documents/Webcam/Shots/$DAY
fswebcam -d v4l2:/dev/video0 -r 1720x1280 --no-banner /home/pi/Documents/Webcam/Shots/$DAY/$TIMESTAMP.jpg /home/pi/Documents/Webcam/Shots/Live/Live.jpg
sudo cp /home/pi/Documents/Webcam/Shots/$DAY/$TIMESTAMP.jpg /usr/share/apache2/icons/Live.jpg
timesincelastmod=$(expr $(date +%s) - $(date +%s -r /var/www/html/index.html))
declare -i delay=10
TIMESINCELASTMOD=$(($timesincelastmod+0))
if [$TIMESINCELASTMOD \< $delay]; then
  sed -i -e "s|\(Lastupdate:\).*\(.\)|\1 $LUDAY at $LUTIME (CEST) \2|g" /var/www/html/index.html
else
  sed -i -e "s|\((CEST)\).*\(.\)|\1 - (Failed to upload last photo) \2|g" /var/www/html/index.html
fi

错误:

pi@JayRasp:/usr/lib/apache2/modules $ sudo bash /home/pi/Documents/Webcam/update_pic.sh
--- Opening v4l2:/dev/video0...
/dev/video0 opened.
No input was specified, using the first.
Adjusting resolution from 1720x1280 to 1600x1200.
--- Capturing frame...
Captured frame in 0.00 seconds.
--- Processing captured image...
Disabling banner.
Writing JPEG image to '/home/pi/Documents/Webcam/Shots/2016-07-07/2016-07-07_0659.jpg'.
Writing JPEG image to '/home/pi/Documents/Webcam/Shots/Live/Live.jpg'.
/home/pi/Documents/Webcam/update_pic.sh: line 13: [1805: command not found

【问题讨论】:

  • 在[之后和]之前加一个空格if [$TIMESINCELASTMOD...
  • shellcheck.net 是您的朋友,请检查您的脚本是否存在所有语法问题。
  • 这无疑是重复的;通过在[ 和] 前后添加空格解决了很多个问题。
  • @KeithThompson 是的,可能是这样,但我不知道要搜索什么,因为我不知道问题是什么

标签: linux bash if-statement command raspbian


【解决方案1】:

[ 和 ] 周围需要空格。

为什么?

[ 是一个命令。

tim@Hairy16:~$ ls /usr/bin
[

试试if [ $TIMESINCELASTMOD \&lt; $delay ]; then

在 bash 中,命令周围需要空格。

另外,您需要在&lt; 或&gt; 之前使用-lt、-gt、-le 和-ge,而不是\。它们的意思是小于、大于、小于或等于和大于或等于。 p>

【讨论】:

  • 谢谢你,解决了问题,你为我节省了很多时间!
  • @JoshuaWeber 如果可以,请接受此答案以表明问题已解决!
  • @JoshuaWeber 点击向下箭头下方的勾号。
  • 完成!先生,祝您有美好的一天。
  • @JoshuaWeber:实际上,如果我了解脚本在做什么,您必须使用-le、-gt 等,因为它们进行数字而不是词法比较.区别在于[ 5 -lt 100 ] 为真,但[ 5 \&lt; 100 ] 为假,因为在词汇排序顺序中“5”在“1”之前。要么,要么使用双括号表达式(例如(( TIMESINCELASTMOD &lt; delay ))),这完全是算术(和自动扩展变量,并且不需要转义大于和小于符号)。
猜你喜欢
  • 2018-09-20
  • 1970-01-01
  • 1970-01-01
  • 2017-09-07
  • 1970-01-01
  • 1970-01-01
  • 2017-04-28
  • 2014-10-31
  • 2015-09-08
相关资源
最近更新 更多