LinuxQuestions.org
Visit the LQ Articles and Editorials section
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
 
LinkBack Search this Thread
Old 08-14-2009, 09:40 AM   #1
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 119

Rep: Reputation: 17
[bash] assign boolean expression to variable


I would like to assign true/false to $RUN in one line rather than using the clumsy if statement below.

Code:
: ${DELETE:=false}
: ${FORCE:=false}
: ${SKIP:=false}
RUN=true
if $DELETE && ! $FORCE && ! $SKIP && [ -z "$RUNDAY" ] ; then
    RUN=false
fi
I tried the following but it does not work:

Code:
RUN=! $DELETE || $FORCE || $SKIP ||  [ -n "$RUNDAY" ]
 
Old 08-14-2009, 10:26 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,459

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
This should work:
Code:
! $DELETE || $FORCE || $SKIP || [ -n "$RUNDAY" ] && RUN=false
 
Old 08-14-2009, 11:28 AM   #3
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 119

Original Poster
Rep: Reputation: 17
Thank you for your reponse, jlliagre.

With the variables set as follows

Code:
DELETE=false
FORCE=false
SKIP=true
RUN=true
Code:
! $DELETE || $FORCE || $SKIP || [ -n "$RUNDAY" ] && RUN=false
sets RUN to false

I would have expected bash to stop evaluating as soon as the any of the sub-expressions is found true.
 
Old 08-14-2009, 12:07 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,514
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
bash does not use the strings "true" and "false" to denote logical values true and false. What does it use? Here's from the "Advanced Bash-Scripting Guide 6.05"
Code:
#!/bin/bash

#  Tip:
#  If you're unsure of how a certain condition would evaluate,
#+ test it in an if-test.

echo

echo "Testing \"0\""
if [ 0 ]      # zero
then
  echo "0 is true."
else          # Or else ...
  echo "0 is false."
fi            # 0 is true.

echo

echo "Testing \"1\""
if [ 1 ]      # one
then
  echo "1 is true."
else
  echo "1 is false."
fi            # 1 is true.

echo

echo "Testing \"-1\""
if [ -1 ]     # minus one
then
  echo "-1 is true."
else
  echo "-1 is false."
fi            # -1 is true.

echo

echo "Testing \"NULL\""
if [ ]        # NULL (empty condition)
then
  echo "NULL is true."
else
  echo "NULL is false."
fi            # NULL is false.

echo

echo "Testing \"xyz\""
if [ xyz ]    # string
then
  echo "Random string is true."
else
  echo "Random string is false."
fi            # Random string is true.

echo

echo "Testing \"\$xyz\""
if [ $xyz ]   # Tests if $xyz is null, but...
              # it's only an uninitialized variable.
then
  echo "Uninitialized variable is true."
else
  echo "Uninitialized variable is false."
fi            # Uninitialized variable is false.

echo

echo "Testing \"-n \$xyz\""
if [ -n "$xyz" ]            # More pedantically correct.
then
  echo "Uninitialized variable is true."
else
  echo "Uninitialized variable is false."
fi            # Uninitialized variable is false.

echo


xyz=          # Initialized, but set to null value.

echo "Testing \"-n \$xyz\""
if [ -n "$xyz" ]
then
  echo "Null variable is true."
else
  echo "Null variable is false."
fi            # Null variable is false.


echo


# When is "false" true?

echo "Testing \"false\""
if [ "false" ]              #  It seems that "false" is just a string.
then
  echo "\"false\" is true." #+ and it tests true.
else
  echo "\"false\" is false."
fi            # "false" is true.

echo

echo "Testing \"\$false\""  # Again, uninitialized variable.
if [ "$false" ]
then
  echo "\"\$false\" is true."
else
  echo "\"\$false\" is false."
fi            # "$false" is false.
              # Now, we get the expected result.

#  What would happen if we tested the uninitialized variable "$true"?

echo

exit 0

Last edited by catkin; 08-14-2009 at 12:10 PM. Reason: Typo
 
Old 08-14-2009, 03:25 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,307

Rep: Reputation: 767Reputation: 767Reputation: 767Reputation: 767Reputation: 767Reputation: 767Reputation: 767
Quote:
Originally Posted by catkin View Post
bash does not use the strings "true" and "false" to denote logical values true and false.
jlliagre's snippet executes one of the commands true or false based on the value of the variable. But I think it shoule be
Code:
$DELETE && ! $FORCE && ! $SKIP && [ -z "$RUNDAY" ] && RUN=false
or
Code:
! $DELETE || $FORCE || $SKIP || [ -n "$RUNDAY" ] || RUN=false
 
Old 08-15-2009, 06:14 AM   #6
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 119

Original Poster
Rep: Reputation: 17
ntubski, you cracked it!

However, I take catkin's point that I am not doing things the bash way. Coming from other programming languages, I find 0 = true totally counter-intuitive though.
 
Old 08-15-2009, 06:26 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,514
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
Quote:
Originally Posted by hashbang#! View Post
ntubski, you cracked it!

However, I take catkin's point that I am not doing things the bash way. Coming from other programming languages, I find 0 = true totally counter-intuitive though.
It's more (not fully!) intuitive if you consider that it's primary use is to indicate whether a command ran OK. With that perspective, it makes sense to use 0 for OK, leaving 126 other numbers (max 7 bit range less 0 and 127 reserved) to be specific about the error.

That's our perspective as problem solvers; if it worked we don't care; if it failed we are interested!
 
Old 08-15-2009, 07:23 AM   #8
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Rep: Reputation: 37
Quote:
Originally Posted by catkin View Post
That's our perspective as problem solvers; if it worked we don't care; if it failed we are interested!
at the beginning I was startled by this 0=true thing, but once you get used to know that 0 = OK and everything else is a DETAILED error code then you cant just go back to the other way. This is more useful and actually 1=true becomes more "counter-intuitive".

 
Old 08-15-2009, 10:56 PM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,307

Rep: Reputation: 767Reputation: 767Reputation: 767Reputation: 767Reputation: 767Reputation: 767Reputation: 767
Arithmetic Evaluation gives the more familiar 0=false C-like semantics:
Code:
DELETE=0
FORCE=0
SKIP=0
((RUN = !DELETE || FORCE || SKIP || ${#RUNDAY}))
 
Old 08-16-2009, 05:26 AM   #10
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 119

Original Poster
Rep: Reputation: 17
ntubski, I like that a lot, particularly the ${#var}!

However, the following now returns an error:

Code:
$DELETE && rm -f $RUNFILE
bash: 1: command not found
 
Old 08-16-2009, 08:44 AM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,307

Rep: Reputation: 767Reputation: 767Reputation: 767Reputation: 767Reputation: 767Reputation: 767Reputation: 767
Because 1 and 0 only mean true and false inside arithmetic evaluation:

Code:
((DELETE)) && rm -f $RUNFILE
 
  


Reply

Tags
bash


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you assign a variable to be a variable file name in a directory? David_Elliott Programming 4 04-14-2009 10:19 AM
bash: checking if a variable is a number (need regular expression help) anonguy9 Linux - Newbie 6 03-29-2009 02:37 AM
Complicated VI replacement with variable expression jhwilliams Linux - Software 1 08-17-2007 09:44 AM
bash script: how to assign an output (not the result) to a variable? Singing Banzo Programming 8 10-01-2006 06:29 PM
can some one explain me this java boolean expression amolgupta Programming 4 05-11-2006 04:54 AM


All times are GMT -5. The time now is 05:42 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration