Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

bash operators in if-statement

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..)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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&section=all

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading