当我使用 alarm.sh 5 “喝茶” 运行此命令时,我得到了
/home/andy/bin/alarm.sh: line 27: [: Drink: unary operator expected
报警器
soundfile="/usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3"
#amixer -D pulse sset Master 50% > /dev/null 2>&1
originalVolume=$(amixer -D pulse get Master | grep -m 1 -o -E [[:digit:]]+%)
clear
amixer -D pulse sset Master 50% > /dev/null 2>&1
if [ -f "$soundfile" ];
then
echo "Soundfile is present."
else
echo "File $soundfile does NOT exist."
echo
echo "Program will now exit."
exit
fi
[ ! $2 ] && {
echo
echo -e " Error!! No time value given for sleep and/or message !!"
echo
echo -e " Alarm Program 2018"
echo
echo -e " alarm.sh [time value in seconds] Message in double Quotes";
echo -e " alarm 5m = 5 minute alarm"
echo -e " alarm 5h = 5 hour alarm"
echo -e " alarm 5d = 5 day alarm"
echo -e " alarm 1.5m = 1 minute 30 seconds alarm"
echo
echo -e " alarm.sh 1m "\"Take bread out of oven."\""
echo
exit 1; }
echo -e "\033[32;5mTIMER COUNTING DOWN to $1 \033[0m"
sleep $1
{
for ((volume = 35; volume <= 50; volume += 2)); do
amixer -D pulse sset Master ${volume}% > /dev/null
sleep .5
done
} &
cvlc --play-and-exit "$soundfile" > /dev/null 2>&1
echo $2
#set back to original volume
amixer -D pulse sset Master %40
gxmessage -fg blue -font 'sans 20' -timeout 2 ' TIME IS UP !!'
答案1
要检查位置参数是否$2
为空,请使用-z
测试运算符,并且不要忘记引用变量:
[ -z "$2" ]
看help test
:
String operators: -z STRING True if string is empty. -n STRING STRING True if string is not empty.
另外,不要忘记#!/bin/bash
在文件顶部添加适当的shebang(特别是如果您使用echo -e
)。