3

I am trying to create an interactive bash shell script to make it easier to install and configure a postfix mail server,But have them still enter the commands and then the shell run them, but I keep getting this error and I don't know why.

These two lines are giving me the error "Too many arguments"

if test $VAR1 = sudo apt-get install postfix

and

until test $VAR1 = sudo apt-get install postfix

Thanks.

4

You need to quote the arguments to test to stop them 'breaking' on whitespace, i.e. if

$ var1="sudo apt-get install postfix"

then

$ if test $var1 = sudo apt-get install postfix; then echo "Match"; fi
bash: test: too many arguments

but

$ if test "$var1" = "sudo apt-get install postfix"; then echo "Match"; fi
Match

The same applies if you use the [ ... ] test construct

if [ "$var1" = "sudo apt-get install postfix" ]; then echo "Match"; fi

As an aside, it's recommended not to use all-caps for your variable names - all-caps are generally reserved for system variables.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.