Issue
Im new to bash and can't understand how the operator quite work here. The goal is to creating a script which ask user to input "y" to run the command or "n" to not run.
by using ==
:
All inputs get passed such as (y, n, a, abc, etc..)
if (($TFENGINE_VAR==y))
by using =
:
All inputs get rejected such as (y, n, a, abc, etc..)
if (($TFENGINE_VAR=y))
echo "run tfengine? (y/n)"
read TFENGINE_VAR
if (($TFENGINE_VAR==y))
then
echo "running tfengine command.."
sleep 1
tfengine --config_path=main.hcl --output_path=terraform/ -delete_unmanaged_files
else
echo "continue.."
continue
fi
Solution
Expressions surrounded with (( ))
are evaluated as numeric expressions.
You need to use the test
command, which returns 0
if test is successful, and a number different from 0
if not.
There are several options to the test command. In your case, you want to compare strings, so use each parameter surrounded with double quotes.
Also, when comparing strings you don't use ==
as the comparison operator.
if test "$TFENGINE_VAR" = "y"
then
...
Take a look at test man page: http://man.he.net/?topic=test§ion=all
Answered By - Niloct Answer Checked By - Mildred Charles (WPSolving Admin)