LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-17-2006, 05:41 PM   #1
zedmelon
Member
 
Registered: Jun 2004
Location: colorado, USA
Distribution: slack, oBSD
Posts: 119

Rep: Reputation: 24
Thumbs up shellscript: compound tests (FYI)


I nearly put this in the programming forum. Sorry if it should be there, and mods feel free to move it.

I had a shell script question, and searching the forum didn't directly help, except I found someone reference the ABSG (Advanced Bash Scripting Guide), where I found my answer. I'm now posting for posterity.

I wanted to run a command block if either or each of the following was true:
(variable 1 = <STRING>)
(variable 2 = <var3> or variable 3 = <var6>)

I tried rearranging brackets and parenthesis to no avail. I knew there had to be a better way than two tests and a redundant 'else' clause...lame. The answer was in the use of a logical OR test joining two more traditional-looking tests.

So here it is; hope I'm not the only syntax-deprived bonehead out there. A short script and several runs with my varied input:
Code:
(16:32:25 bin) cat doubleif

#!/bin/sh
read one two three
if [ "$one" = "1" ] || [ "$two" = "2" -a "$three" = "3" ]
then    echo yep
else    echo nope
fi

(16:32:42 bin) doubleif
5 6 7
nope
(16:32:55 bin) doubleif
2 2 3
yep
(16:33:04 bin) doubleif
1 3 4
yep
(16:33:16 bin) doubleif
1
yep
(16:33:20 bin) doubleif
1 2 3
yep
(16:33:25 bin)
Curiosity now has me thinking on how to change the above "either or both" to "either, but not both." Back to ABSG...
 
Old 07-18-2006, 10:38 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Code:
[ "$one" = 1 -o "$two" = "2" -a "$three" = "3" ]

or (same thing but reassuring)

[ "$one" = 1 -o \( "$two" = "2" -a "$three" = "3" \) ]

Last edited by bigearsbilly; 07-18-2006 at 10:41 AM.
 
Old 07-18-2006, 10:43 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
change (..) to "either, but not both."
If you have no requirements, then how about:
Code:
res=0; read a b c
[ "$a" = "1" ] && res=$[$res+1]
[ "$b" = "2" ] && res=$[$res+2]
[ "$c" = "3" ] && res=$[$res+4]
case "$res" in 1|2|4) echo \
"yep";; *) echo "nope";; esac
 
Old 07-19-2006, 11:47 AM   #4
zedmelon
Member
 
Registered: Jun 2004
Location: colorado, USA
Distribution: slack, oBSD
Posts: 119

Original Poster
Rep: Reputation: 24
I originally meant to post a quick tip, but then I yammered at the end--Dave Barry calls this "blitherer's disease."

Note to those who just found this thread: I was only curious about the superfluous problem. Feel free to solve it, but there are others who actually need help. My script is working and in place. That said, I love seeing my superiors' smarter code.

bigearsbilly, thanks for posting that. I'm evidently too impaired to realize escaping also works inside a test. I started by attempting to group the args, because I once read about problems controlling precedence with "-a" and "-o"--guess that guy was wrong.

unSpawn, thanks as well. Your "binary" code is a great idea, and tweaking it to do what I meant (not what I said ;-) was a snap.

I wasn't clear; by "both," I meant both tests from my first post. I just wondered if it's possible to do all this in a single test:
Code:
( ([a=1] -o [b=2 -a c=3]) but not ([a=1] -a [b=2 -a c=3]) )
This worked:
Code:
#!/bin/sh
res=0; read a b c
[ "$a" = "1" ] && res=$[$res+1]
[ "$b" = "2" -a "$c" = "3" ] && res=$[$res+2]
case "$res" in 1|2) echo \
"yep";; *) echo "nope";; esac

(10:37:07 bin) ./werks 
5 5 5                      <- neither
nope
(10:37:19 bin) ./werks 
5 2 3                      <- 2nd
yep
(10:37:23 bin) ./werks 
1 4 4                      <- 1st
yep
(10:37:30 bin) ./werks 
1 2 3                      <- both
nope

Last edited by zedmelon; 07-19-2006 at 11:51 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c compound conditional steve_96 Programming 9 06-04-2005 07:17 PM
Help Shellscript paraiso Linux - Newbie 7 05-12-2005 07:25 AM
warning: deprecated use of label at end of compound statement abk4523 Programming 1 03-06-2005 09:50 AM
compound grep j-me Linux - Newbie 3 08-04-2003 02:04 PM
Compound Problems bselzler Linux - Networking 0 09-09-2001 09:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration