LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-13-2008, 05:59 PM   #1
AncientPC
LQ Newbie
 
Registered: Apr 2007
Posts: 25

Rep: Reputation: 15
Bash: Problems testing multiple statements with logical and (-a or &&)


I know the first and second statements evaluate correctly by themselves, but I would like to combine the two into one line (using -a or &&) as opposed to having nested if statements.

Code:
#!/bin/bash

if [[ ! -z $1 ] && [[ $1 =~ [0-9] ]]]; then
	echo "$1 is a number"
else
	echo "invalid arguments"
	exit 1
fi
exit 0
On a side note, what are the boolean values for true and false in bash?

Code:
if [ 0 -a 1 ]
returns true
Code:
if [ 0 -a 0 ]
returns true
Code:
if [ false -a false ]
returns true
Code:
if [ 0 && 1 ]
gives me a syntax error
 
Old 04-13-2008, 08:40 PM   #2
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Code:
[[ -n $1 && $1 =~ [0-9] ]]
bash variables for true and false are "true" and "false". Just don't use them in [] or [[]] since then it's just defaulting to the equivalent of [ -n "true" ].

Also, if you're doing bash, always use the double brackets '[['

Here's some more info [[]] vs []
 
Old 04-13-2008, 09:58 PM   #3
AncientPC
LQ Newbie
 
Registered: Apr 2007
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by angrybanana View Post
Code:
[[ -n $1 && $1 =~ [0-9] ]]
bash variables for true and false are "true" and "false". Just don't use them in [] or [[]] since then it's just defaulting to the equivalent of [ -n "true" ].

Also, if you're doing bash, always use the double brackets '[['

Here's some more info [[]] vs []
Thanks, that helped a lot. I remember going over [] vs [[]] before but it slipped my mind. I have to relearn bash every few months.

On a side note, while I was looking up -n I ran into this (http://tldp.org/LDP/abs/html/compari...s.html#STRTEST):
Quote:
As S.C. points out, in a compound test, even quoting the string variable might not suffice. [ -n "$string" -o "$a" = "$b" ] may cause an error with some versions of Bash if $string is empty. The safe way is to append an extra character to possibly empty variables, [ "x$string" != x -o "x$a" = "x$b" ] (the "x's" cancel out).
The final code:
Code:
#!/bin/bash

max=1000
if [[ -n "$1" && $1 =~ [0-9] ]]; then
	max=$1
else
	echo "Invalid arguments"
	exit 1
fi

ssh-add ~/.ssh/csservers
machines=`cat ~/bin/cshosts.txt`
count=1
for m in $machines; do
	if [[ $count -le $max && `ping -s 1 -c 1 $m>/dev/null; echo $?` -eq 0 ]]; then
		if [ "$(ssh $m who)" == "" ]; then
			echo "$m"
			let "count+=1"
		fi
	fi
done

exit 0

Last edited by AncientPC; 04-13-2008 at 10:35 PM.
 
Old 04-13-2008, 11:32 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 2,920

Rep: Reputation: 1135Reputation: 1135Reputation: 1135Reputation: 1135Reputation: 1135Reputation: 1135Reputation: 1135Reputation: 1135Reputation: 1135
I think this

Code:
if [[ $count -le $max && `ping -s 1 -c 1 $m>/dev/null; echo $?` -eq 0 ]]; then
could be simplified to

Code:
 if [[ $count -le $max ]] && ping -s1 -c1 $m >/dev/null ; then
 
Old 04-14-2008, 12:16 AM   #5
AncientPC
LQ Newbie
 
Registered: Apr 2007
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ntubski View Post
I think this

Code:
if [[ $count -le $max && `ping -s 1 -c 1 $m>/dev/null; echo $?` -eq 0 ]]; then
could be simplified to

Code:
 if [[ $count -le $max ]] && ping -s1 -c1 $m >/dev/null ; then
Yes that works, thank you very much.
 
  


Reply

Tags
bash, scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
mixed system, etch && testing || unstable: building ia32-libs-gtk HowDoIProgramIt Debian 2 08-15-2007 10:18 AM
bash script: run multiple jobs in bg, & kill them separately? mattengland Programming 1 03-26-2006 05:07 PM
AOL UK && BT Voyager 100 && Slackware 10.2 && RP-PPPoE pitt0071 Linux - Networking 3 01-17-2006 06:10 AM
Japanese canna won't work : Warning: かな漢字変&am OrganicOrange84 Debian 3 06-30-2005 02:28 PM
Phục hồi dữ liệu bị mất???, cứ pollsite General 1 06-27-2005 12:39 PM


All times are GMT -5. The time now is 11:44 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration