multiple condition in if


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting multiple condition in if
# 1  
multiple condition in if

All,

My environment is Red Hat Enterprise Linux 5.

I am using the following condition --

if [[ $z -gt 5 -a $z -lt 7 ] -0 [ $y == 'VALID' ]]

above command is not working. It is telling that -a unexpected.

Please help me
# 2  
Quote:
Originally Posted by user7509
All,

My environment is Red Hat Enterprise Linux 5.

I am using the following condition --

if [[ $z -gt 5 -a $z -lt 7 ] -0 [ $y == 'VALID' ]]

above command is not working. It is telling that -a unexpected.

Please help me
You're getting your ]'s and ]]'s mixed up, if you start with [[ always end with ]], they're not nesting brackets.
# 3  
Hi

I tried with following combinations,

if [[ $z -gt 5 -a $z -lt 7 ] -o [ $y == 'VALID' ]]

if [ $z -gt 5 -a $z -lt 7 -o $y == 'VALID' ]

if [ $z -gt 5 ]-a [ $z -lt 7 ] -0 [ $y == 'VALID' ]

nothing is working fine.

Can you please provide the correct one
# 4  
How about ...

Code:
if ([ $z -gt 5 ] && [ $z -lt 7 ]) || [ $y == 'VALID' ]

# 5  
still not working..
# 6  
Try this. The condition is the same each time, we just change the data for each test.

Code:
z=6
y="VALID"
if [ \( ${z} -gt 5 -a ${z} -lt 7 \) -o \( "${y}" = "VALID" \) ]
then
      echo "TRUE: ${z} ${y}"
else
      echo "FALSE: ${z} ${y}"
fi


z=5
y="VALID"
if [ \( ${z} -gt 5 -a ${z} -lt 7 \) -o \( "${y}" = "VALID" \) ]
then
      echo "TRUE: ${z} ${y}"
else
      echo "FALSE: ${z} ${y}"
fi

z=6
y="ORANGE"
if [ \( ${z} -gt 5 -a ${z} -lt 7 \) -o \( "${y}" = "VALID" \) ]
then
      echo "TRUE: ${z} ${y}"
else
      echo "FALSE: ${z} ${y}"
fi

z=4
y="ORANGE"
if [ \( ${z} -gt 5 -a ${z} -lt 7 \) -o \( "${y}" = "VALID" \) ]
then
      echo "TRUE: ${z} ${y}"
else
      echo "FALSE: ${z} ${y}"
fi



./scriptname 
TRUE: 6 VALID
TRUE: 5 VALID
TRUE: 6 ORANGE
FALSE: 4 ORANGE

# 7  
Quote:
Originally Posted by user7509
still not working..
Smilie

Code:
[house@leonov] more test.bash
#! /bin/bash

if ([ $1 -gt 5 ] && [ $1 -lt 7 ]) || [ $2 == 'VALID' ]
then
  echo "true"
else
  echo "false"
fi

exit 0

Code:
[house@leonov] sh test.bash 5 VALID
true
[house@leonov] sh test.bash 6 VALID
true
[house@leonov] sh test.bash 6 ORANGE
true
[house@leonov] sh test.bash 4 ORANGE
false

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #368
Difficulty: Medium
The Open Group released the Single UNIX Specification Version 2 in 1987.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need help search multiple condition from a file.

OS: window 7 shell : korn shell I have 2 file , i'm need grep data according File_1 from file 2. File_1 CAL_ENAB_N_4_ $2N12743_29 +12V File_2 NODE CAL_ENAB_N_4_ PINS 21548; PROBES P1465 3651, 46900 tn2700.1 LWT; WIRES (6 Replies)
Discussion started by: kttan
6 Replies

2. Shell Programming and Scripting

Executing multiple scripts using if condition

I have an if condition. If that condition is true then one script will be run and after that I need to check another condition based on the output value of first script. i tried like below : cd lock if ; then rm exitup if ; then kb_shutdown kb_startup if ; then rm exitup if ;... (3 Replies)
Discussion started by: charanarjun
3 Replies

3. Shell Programming and Scripting

Reading multiple lines with condition

Hi guys, I need to read following lines and put them in same row …. text: Abcd5437_XYA0_B1_WXYZ_BE 99:00:14:42:55:01:d4:22 99:00:14:42:70:01:d4:22 99:00:14:42:55:03:a0:22 99:00:14:42:70:03:a0:22 ... (4 Replies)
Discussion started by: dc@bos
4 Replies

4. Shell Programming and Scripting

Multiple if condition

Hi, I wanted to satisfy two requirements to proceed to do a task One of them is to calculate between two metrics and the other to check one of the file not empty if the condition matches the above two, it should proceed with the task below is the snippet of it, however when i run the script... (3 Replies)
Discussion started by: ajothi
3 Replies

5. UNIX for Dummies Questions & Answers

LINUX Multiple condition in IF Statement - Pls help

Hi All, I am trying to put multiple conditions in an IF Statement (using $$). the Linux script somehow doesnt like it. The logic I am trying to implement is as follows, 1. I will first search for DateFile.txt 2. If it exists & there is a P_BUS_DATE value in it, then assign the date value... (5 Replies)
Discussion started by: dsfreddie
5 Replies

6. Shell Programming and Scripting

multiple condition matching and doing some predefined work

Hi everybody, I had 10 files in in one folder(/home/sai/) namely sai. 1.gz,2.gz,3.gz ..,10.gz. I want to delete the files which are there home based on the following conditions fliecount in sai folder==10 && grep -cv ".gz"==0 How to check this using awk? Otherwise please... (2 Replies)
Discussion started by: p_sai_ias
2 Replies

7. Shell Programming and Scripting

Multiple condition checking in bash

Hi All, I am trying to check if two variables have value assigned to it. i am doing it like if ] then echo "Please specify either single hostname or host file for the report" usage exit fi But its not working for it.Even i specify values for both variables it dont go... (6 Replies)
Discussion started by: kailash19
6 Replies

8. Shell Programming and Scripting

multiple echo statements in if condition

Hi , I have a peculiar problem. i have an if block like this if ; then echo " todays date is " ${date} >> log_file echo " file count is " $ count >> log_file mv filename1 filename 2 else echo "no files available ">> log_file fi the echo statement "no files available " is not... (2 Replies)
Discussion started by: wizardofoz
2 Replies

9. Shell Programming and Scripting

awk with multiple condition

Hi Guys, I just wanted to print all the lines execpt 1st and 3rd line. For that i wrote a awk command, awk 'NR != 1 || NR != 3 {print $0}' c.out the command is working if i give an equal to instead of not equal to. In the case of not equal to, it gives me the entire file. Can you... (18 Replies)
Discussion started by: mac4rfree
18 Replies

10. UNIX for Dummies Questions & Answers

Multiple Condition If statement

Hi, I would like to create an IF statement where if a variable is equal to at least one of 2 (or more) values then the script proceeds. For example: TEST_VAR=2 if ; then echo success! else echo failure fi I understand that the above syntax is wrong but I feel it must be close. Any... (1 Reply)
Discussion started by: msb65
1 Replies

Featured Tech Videos