test with two conditions (OR)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting test with two conditions (OR)
# 1  
Old 11-29-2008
Question test with two conditions (OR)

Hi there,
I'm very surprised that I can't find this myself and I'm sorry to bother you with such a stupid question. I just want to write a test with one condition or another one. I want either the first argument to be equal to 'this' or the second argument to be equal to 'that'.
Code:
~$ cat test
(( $1='this' || $2='that' )) && echo test1 says ok
(( $1=='this' || $2=='that' )) && echo test2 says ok
[[ $1='this' || $2='that' ]] && echo test3 says ok
[[ $1=='this' || $2=='that' ]] && echo test4 says ok
~$ test wrong wrong
test: line 1: ((: wrong=this || wrong=that : attempted assignment to non-variable (error token is "=that ")
test2 says ok
test3 says ok
test4 says ok
~$ test this wrong
test: line 1: ((: this=this || wrong=that : attempted assignment to non-variable (error token is "=that ")
test2 says ok
test3 says ok
test4 says ok
~$ test wrong that
test: line 1: ((: wrong=this || that=that : attempted assignment to non-variable (error token is "=that ")
test2 says ok
test3 says ok
test4 says ok
~$ test this that
test: line 1: ((: this=this || that=that : attempted assignment to non-variable (error token is "=that ")
test2 says ok
test3 says ok
test4 says ok

To sum up those tests, first construct will never work, and other constructs will always return true.
Can you help me with that, I can't believe this can be hard!
Thanks in advance
Santiago
# 2  
Old 11-29-2008
Hi,

try it this way:

Code:
( [[ $1 = this ]] || [[ $2 = that ]] ) && echo true || echo false

HTH Chris
# 3  
Old 11-29-2008
Use square brackets instead of parenthesis and place a space around the equal signs:
Code:
[[ $1 == 'this' || $2 == 'that' ]] && echo test1 says ok
[[ $1 == 'this' || $2 == 'that' ]] && echo test2 says ok
[[ $1 = 'this' || $2 = 'that' ]] && echo test3 says ok
[[ $1 == 'this' || $2 == 'that' ]] && echo test4 says ok

# 4  
Old 11-29-2008
Thanks Christoph Spohr and Franklin52,
Both your proposals work fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell test conditions

Are these shell test conditions in any man pages? I do not see them in bash or ksh man pages. The tests below are test conditions provided by the shell: -b file = True if the file exists and is block special file. -c file = True if the file exists and is character special file. -d file... (3 Replies)
Discussion started by: cokedude
3 Replies

2. Shell Programming and Scripting

Shc : trying to test functionality "test" compiling but can not execute

I am testing shc to see if it would help with my need. Im at a point where Im trying to compile and test the "test.ksh" file that comes in the tar ball : shc-3.8.9> shc -v -r -f test.ksh shc shll=ksh shc =-c shc =exec '%s' "$@" shc = shc opts= shc: cc test.ksh.x.c -o test.ksh.x... (7 Replies)
Discussion started by: popeye
7 Replies

3. Shell Programming and Scripting

Errors in if conditions with to many OR conditions

Hi ALL I have a script where in i need to check for several values in if conditons but when i execute the script it throws error such as "TOO MANY ARGUMENTS" if then msg="BM VAR Issue :: bmaRequestVAR=$bmaRequestVAR , nltBMVAR=$nltBMVAR , bmaResponseVAR=$bmaResponseVAR ,... (10 Replies)
Discussion started by: nikhil jain
10 Replies

4. Shell Programming and Scripting

Prefixing test case methods with letter 'test'

Hi, I have a Python unit test cases source code file which contains more than a hundred test case methods. In that, some of the test case methods already have prefix 'test' where as some of them do not have. Now, I need to add the string 'test' (case-sensitive) as a prefix to those of the... (5 Replies)
Discussion started by: royalibrahim
5 Replies

5. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

6. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies
Login or Register to Ask a Question