You are not logged in.

#1 2010-10-09 20:12:37

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,671
Website

bash conditionals - if two variables are equal to a string... [solved]

I have two variables in a script: $a and $b

They are set to either "y" or "n" by the user - I need a conditional to break the script if the users sets them both to "y"

This doesn't work but you get the idea:

if [ $a = "y" && $b = "y" ]; then
exit
fi

What is the right syntax to accomplish this?  The other three combinations are fine (nn, ny, and yn).

Last edited by graysky (2010-10-09 21:33:05)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Online

#2 2010-10-09 20:19:33

olvar
Member
Registered: 2009-11-13
Posts: 97

Re: bash conditionals - if two variables are equal to a string... [solved]

man test:

 if [ "x$a" = "xy" -a "x$b" = "xy" ]; then
   exit
fi

Edit: the "x" before the variables is to ensure nothing funny happens if the variables haven't been asigned.

Last edited by olvar (2010-10-09 20:21:55)

Offline

#3 2010-10-09 20:26:15

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: bash conditionals - if two variables are equal to a string... [solved]

Offline

#4 2010-10-09 21:32:53

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,671
Website

Re: bash conditionals - if two variables are equal to a string... [solved]

Thanks olvar and karol - very informative link in that wiki.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Online

#5 2010-10-10 00:17:17

olvar
Member
Registered: 2009-11-13
Posts: 97

Re: bash conditionals - if two variables are equal to a string... [solved]

Oops... sorry for promoting my bad habits sad

Offline

Board footer

Powered by FluxBB