If statement with two boolean conditions


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers If statement with two boolean conditions
# 1  
Old 07-29-2013
If statement with two boolean conditions

Code:
#!/bin/bash

if [ true ] && [ ! false ]
then
        echo "True"
else
        echo "False"
fi

Hi everyone, I am new to UNIX, here I have a if statement elevating two boolean conditions. I thought the output should be True because there are [true] + [true] in the statement. But it turns out to be False.

Can anyone explain to me how to properly use the boolean condition here? Thanks a lot!

Last edited by Scott; 07-30-2013 at 03:22 AM.. Reason: Code tags
# 2  
Old 07-29-2013
There are utilities named true and false on UNIX and Linux systems, but you aren't using them in your if statement. If you wanted to use them, the if statement would be:
Code:
if true && ! false
then    echo true
else    echo false
fi

The if statement you had was using the test utility to evaluate whether or not "true" was <not an empty string> which evaluates to true and the second test utility invocation to evaluate whether or not NOT ("false" was <not an empty string>) which evaluates to false. Since both did not evaluate to true, the AND of the two tests evaluates to false and you executed the "else" branch of your if statement.
# 3  
Old 07-29-2013
Thank you, Don!

I just changed my code and it is giving out error

Code:
line 46: no: command not found
line 46: -d: command not found

but my code are different from what I typed above.

Code:
 elif ! $recursive && -d $originalPath/$filename
 then
          echo ".................."

for the first condition, recursive takes on the boolean value, and the second condition is to check if the file exists in that directory.

Would you suggest a better approach?

Last edited by Scott; 07-30-2013 at 03:23 AM.. Reason: Code tags
# 4  
Old 07-29-2013
Quote:
Originally Posted by mryuyu1111
Thank you, Don!

I just changed my code and it is giving out error

line 46: no: command not found
line 46: -d: command not found


but my code are different from what I typed above.



elif ! $recursive && -d $originalPath/$filename
then
echo ".................."

for the first condition, recursive takes on the boolean value, and the second condition is to check if the file exists in that directory.

Would you suggest a better approach?
First look at the test man page on your system (or the man page for the shell you're using and look at its description of its built-in test utility). With what you're testing here, you do want to use the test utility; with what you were doing in the first posting in this thread, you didn't want to use the test utility.

After looking at the man page, it should be obvious that the test utility takes two forms:
Code:
test expression

and
Code:
[ expression ]

and some shell also provide other forms. But, these two ways of writing a test command produce exactly the same results; the 2nd form is much more common in shell scripts. Note that the expression must be separated from the test or the [ and ] by one or more spaces or tabs.

From the error messages you're getting, I will make the assumption that the shell variable recursive is expected to either have the value "no" or "yes". Assuming that is correct, you probably want an if statement similar to:
Code:
if false
then    echo "1st <if> true"
elif [ "no" != "$recursive" ] && [ -d "$originalPath/$filename" ]
then    echo "2nd <if> recursive AND $originalPath/$filename is a directory"
else    echo "2nd <if> not recursive OR $originalPath/$filename is not a directory"
fi

I picked up the value "no" for recursive from the errors you got. If my guess that "yes" is the other valid value, I would rewrite the elif clause to be:
Code:
elif [ "yes" = "$recursive" ] && [ -d "$originalPath/$filename" ]

to get rid of the double negative logic ("no" is not the value of recursive) because it is easier for humans to parse "yes" is the value of recursive.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Check Multiple conditions in IF statement?

I wish to check two conditions inside the if statement Condition 1: The two file contents should be identical // using cmp command for this. Condition 2: The two filenames should NOT be the same. This is what i did in vain. if ]; then where entry1 and entry2 are ls *.txt | while... (7 Replies)
Discussion started by: mohtashims
7 Replies

2. Shell Programming and Scripting

Multiple conditions inside if statement

I was trying to write multiple conditions inside the if statement but its not working. export VAR_NM=abc.txt export CURR_DT=20131011 export PREV_DT=20131012 if && then echo "Yes" else echo "NO" fi It should return Yes but returning NO always.Appreciate any help. (3 Replies)
Discussion started by: dr46014
3 Replies

3. Shell Programming and Scripting

How to Evaluate two conditions in single if statement

I am trying to test two conditions in a single if and getting syntax error on -a and && if ] ; then echo "variable a equals to variable b" else echo "variable a not equal to variable b" fi in second attempt I used -a instead of &&, referring to other website, but not sure that... (1 Reply)
Discussion started by: praxis1
1 Replies

4. Shell Programming and Scripting

Multiple Conditions Perl if Statement

Hello, I'm trying to put together a script that involves pulling data from a config file. I'm attempting to write an if statement to validate one of the pieces of data from the config file, but I think I'm fat fingering it somehow. $config{VALUE} is being pulled from a config file but can only... (4 Replies)
Discussion started by: Picch
4 Replies

5. Shell Programming and Scripting

Multiple conditions in a CASE statement

is it possible to use multiple conditions in a CASE statement? And if so, what is the syntax? I'm trying to use one but can't seem to get it right. I want the statement to be CASE $vendor OR $alias condition 1) statements; condition 2) statements; etc. esac but I keep... (25 Replies)
Discussion started by: Straitsfan
25 Replies

6. Shell Programming and Scripting

Multipe conditions in if statement

I have a script that runs every 15 minutes in cron that builds a web page. It runs at 15, 28, 45 and 58 minutes past the hour. (pretty much evry 15 mins). Every 2 hours from 6:28 to 18:28 it sends out emails if it finds errors. I do not want it sending email every single time it runs, every 15... (5 Replies)
Discussion started by: spacemancw
5 Replies

7. Shell Programming and Scripting

If statement with multiple conditions

I have a script that runs on multiple servers. What I want to do is have the script do the following: if $(hostname) is equal to server or server2 then TO_DIR=go else TO_DIR=stop fi I have tried: if if ] Server is hpux. any ideas? (1 Reply)
Discussion started by: cpolikowsky
1 Replies

8. UNIX for Dummies Questions & Answers

Two conditions in one if statement

I'm totally new with bash programming and I don't get it how to put two conditions in one if statement. My code looks like this: h=`date +%k` if && ]; then$h is 10 but I don't get into my if statement. What's wrong here? (5 Replies)
Discussion started by: borobudur
5 Replies

9. Shell Programming and Scripting

How to giv two conditions in IF statement..??

Actually i need to satisfy both the condition.. lik i'm lookin for two different files.. and if BOTH the files r not present then it should run the followin script.. For example inputfile1=data/in/inputfile1.txt inputfile2=data/in/inputfile2.txt if then echo " " echo... (6 Replies)
Discussion started by: RRVARMA
6 Replies

10. Shell Programming and Scripting

if statement with two conditions

Hi, I am trying to use && set up to match two conditions within ksh: if && then '''Do something if somehow, I keep getting error message telling me that ] is missing. What's wrong with my code? Thanks a lot for your help! (1 Reply)
Discussion started by: cin2000
1 Replies
Login or Register to Ask a Question