Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

boolean question 2

Status
Not open for further replies.

prosper

Programmer
Sep 4, 2001
631
HK
This is a bash script
Code:
vmFound=false
if [ $vmFound ]; then
   echo "vmFound is true"
else
   echo "vmFound is false"
fi

Why "vmFound is true" is printed?
How could I modify the script to make it run with correct result?
I cannot find a tutorial with similar example
Thanks for any help
 
The default check performed by [ somestring ] is whether a string is equivalent to [ -n somestring ], which returns true when the string is of non-zero length. Refer to the "CONDITIONAL EXPRESSIONS" secion of man bash.

So you could either do a string comparison, i.e.

Code:
if [ "$vmFound" == true ]; then

Or you could rely on the /usr/bin/true and /usr/bin/false binaries to return the correct value and simply use:

Code:
 if $vmFound; then

Annihilannic.
 
Ignore "whether a string is" in my previous post... I meant to delete that.

Annihilannic.
 
Thanks for the answer and the detailed explanation
 
Hi

As an alternative, while you are using [tt]bash[/tt]. You can use C-like conditions, so 0 is false and any other integer is true :
Bash:
[navy]vmFound[/navy][teal]=[/teal][purple]0[/purple]
[b]if[/b] [teal](([/teal] vmFound [teal]));[/teal] [b]then[/b]
  echo [green][i]"vmFound is true"[/i][/green]
[b]else[/b]
  echo [green][i]"vmFound is false"[/i][/green]
fi
The same works in [tt]ksh[/tt] too. ( Not sure if in all versions. )

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top