if 语句中的未知错误
Unknown error in if statement
我在 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:\).*\(.\)| $LUDAY at $LUTIME (CEST) |g" /var/www/html/index.html
else
sed -i -e "s|\((CEST)\).*\(.\)| - (Failed to upload last photo) |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
您的 [
和 ]
周围需要空格。
为什么?
[
是命令。
tim@Hairy16:~$ ls /usr/bin
[
尝试if [ $TIMESINCELASTMOD \< $delay ]; then
在 bash 中,命令周围需要空格。
此外,您需要在 <
或 -lt
、-gt
、-le
和 -ge
之前使用 \
或>
。他们的意思是小于、大于、小于或等于和大于大于或等于.
我在 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:\).*\(.\)| $LUDAY at $LUTIME (CEST) |g" /var/www/html/index.html
else
sed -i -e "s|\((CEST)\).*\(.\)| - (Failed to upload last photo) |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
您的 [
和 ]
周围需要空格。
为什么?
[
是命令。
tim@Hairy16:~$ ls /usr/bin
[
尝试if [ $TIMESINCELASTMOD \< $delay ]; then
在 bash 中,命令周围需要空格。
此外,您需要在 <
或 -lt
、-gt
、-le
和 -ge
之前使用 \
或>
。他们的意思是小于、大于、小于或等于和大于大于或等于.