这是一个关于添加记录到指定文件的小工具
首先给出正常运行的代码
#!/bin/bash
echo "Choose an action to executive!"
echo "1|append):add a line to the file
2|cat)print the content of the file
3|quit)exit the process"
read action
case $action in
1|append)
echo "the file you're going to edit is: $1"
echo "type \"cat\" for content of $1, type \"quit\" to leave"
echo "and enter what you want to append, then it will add to thelast of the file......"
for ((i=0;i<100;i=i+1))
do
echo "you've edit the $1 for $i time(s)"
read content
if [ "$content" == "cat" ]; then
less $1
else if [ "$content" == "quit" ]; then
{ echo "process suspend ...exit"
exit 0
}
else
{ echo "$content" >> $1
echo "any else?---->"
}
fi
fi
done
;;
2|cat)
cat $1
;;
3|quit)
echo "you choose to leave, thank you for using Enote"
exit 0
;;
*)
echo "choose an action to executive, if not i'll quit directly!!"
;;
esac
这里采用字符串对append功能进行测试
当24行的echo命令的引用变只要采用了$号作引用后,不论加不加“”双引号在面,变量是否加{}都没有影响输出
now, echo without "" & {}
now echo string is with {} but no ""
it works
但是对于shell中条件判断的test命令来说,并不是一回事,这里采用的字符串比较(包含多个word,所以才有这样的问题)
now, test the 'test' command contains quoted string without ""
it replys: ./Enote: line 17: [: too many arguments
now, two 'test' command are without "" in quoting variables
then,it replys: ./Enote: line 17: [: too many arguments
./Enote: line 19: [: too many arguments
now, in command 'test' {} are added to quote variables
replys: ./Enote: line 17: [: too many arguments
./Enote: line 19: [: too many argumequit
now, it changes to original
replys nothing
可见,对于 [ "string1" == "string2" ] 是不能去引号的