My bash script is supposed to get user input to deal with a particular file exiting as in:
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
This logic checks the user's input:
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
The issue is that if you just press enter the script executes as though a "y" or a "Y" have been input, and I don't know why. My understanding is that the way this is written, it should exit if the user puts in anything other than a y,Y,n,or N.
It prints this when you have no input:
master_script.sh: line 14: [: =: unary operator expected
master_script.sh: line 14: [: =: unary operator expected
master_script.sh: line 19: [: !=: unary operator expected
But doesn't exit – how can I make it exit?
[[ ]]form of conditional expressions will probably get you there, from a quick look. Also make sure every[]&&||and the like has spaces around it. shellcheck can help you.norN).test -fis not checking for existence - it's much more picky than that. You need either to write-eor change your description.