我的bash脚本应该接受用户的输入,处理与特定文件退出,如:使得bash脚本退出如果在标准输入
if [ -e "formatted_log.csv" ]
then
printf "\nPresence of formatted_log.csv file detected: It appears this script has already been run is this dir.\n"
printf "Appending can cause errors.\n"
read -e -p "Would you like to continue?[y/n]" stdin
这种逻辑检查用户输入:
if [ $stdin = "n" ] || [ $stdin = "N" ];
then
printf "\nOkay, exiting.\n"
exit;
fi
if [ $stdin != "y" ]&&[ $stdin != "Y" ]&&[ $stdin != "n" ]&&[ $stdin != "N" ];
then
printf "\nPlease use a valid input (y/n)...exiting.\n"
exit;
fi
fi
问题是,如果你只是按下输入脚本执行,就好像一个“Y”或“Y”已被输入,我不知道为什么。我的理解是,这是书面的方式,它应该如果用户将在比唉,Y以外的任何时候你有没有输入退出,n或N.
它打印此:
master_script.sh: line 14: [: =: unary operator expected
master_script.sh: line 14: [: =: unary operator expected
master_script.sh: line 19: [: !=: unary operator expected
但不退出–我该如何退出?
切换到[[]]'形式的[条件表达式](http://wiki.bash-hackers.org/syntax/ccmd/conditional_expression)可能会让你快速浏览。还要确保每个'''''''&&''||'等都有空格。 [shellcheck](http://shellcheck.net)可以帮助你。 – cxw
如果你只是要退出,就告诉用户他们输入了一个无效的输入信息是没有意义的(就好像他们输入'n'或'N')。 – chepner
'test -f'不*检查存在 - 它比这更挑剔。你需要写'-e'或者改变你的描述。 –