while [ $done = 0 ]
do
echo -n "Would you like to create one? [y/n]: "
read answer
if [ "$(answer)" == "y" ] || [ "$(answer)" == "Y" ]; then
mkdir ./fsm_$newVersion/trace
echo "Created trace folder in build $newVersion"
$done=1
elif [ "$(answer)" == "n" ] || [ "$(answer)" == "N" ]; then
$done=2
else
echo "Not a valid answer"
fi
done
Ok so I have this simple bashscript above that simply just tries to get input from a user and validate it. However I keep getting this error
./test.sh: line 1: answer: command not found
./test.sh: line 1: answer: command not found
./test.sh: line 1: answer: command not found
./test.sh: line 1: answer: command not found
Which I have no idea why because “answer” is nowhere near line 1. So I ran into this article
Which makes sense since it’s referring to line 1 and can’t find answer. So it seems to be starting a new subshell. However I didn’t really understand the solution and can’t see how I would apply it to my case. I just wanna get this to work.
$(answer)doesn’t substitute the value of the variableanswer. It executesansweras a command, and substitutes the output of that command. You want${answer}everywhere you have$(answer). In this case you can get away with bare$answertoo, but overuse of${...}is good paranoia.(Are you perhaps used to writing Makefiles?
$(...)and${...}are the same in Makefiles, but the shell is different.)By the way, you have some other bugs:
$done=1to justdone=1and similarly for$done=2.mkdircommand and the condition on thewhileloop.test(aka[). You need to prefix both sides of an equality test withxso that they cannot be misinterpreted as switches.==is not portable shell, use=instead (there is no difference in bash, but many non-bash shells do not support==at all).Put it all together and this is what your script should look like: