operators in if loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting operators in if loop
# 1  
Old 06-30-2012
operators in if loop

Hi all,

I have a variable which is having the value like below,

Code:
$ echo ${p}
8 15 22 30
$

My requirement is that the variable should return true when it contains only one number like below,

Code:
$ echo ${p}
15
$

Otherwise, it should return false if it contains more than one number.
I tried to find out the if loop operator which helps in this, but could not find anything.
Could anyone help me in this please?

Regards,
Raghu
# 2  
Old 07-01-2012
Which shell and O/S are you using?

Generally this should work.

Code:
if echo $p | grep -E '[ ]+'; then
 echo return true 
else 
 echo return false 
fi

Here, I am matching whether the variable contains "spaces".

If you have modern bash, You could directly use regular expressions inside an "if" statement.
# 3  
Old 07-01-2012
Only test for whole positive numbers
Code:
case $p in
  *[^0-9]*) echo false;;
  *)        echo true;;
esac

--
General test for number of fields:

This should work for spaces tabs and newlines:
Code:
if ( set -- $p; [ $# -eq 1 ] ); then
  echo true 
else 
  echo false 
fi

or

Code:
singlefield(){
  [ $# -eq 1 ]
}

if singlefield $p; then
  echo true
else
  echo false
fi



These should work for spaces only as a field separator
Code:
if [ "$p" = "${p% *}" ]; then 
 echo true 
else 
 echo false 
fi

Code:
case $p in
  *\ *) echo false;;
  *)    echo true;;
esac


Last edited by Scrutinizer; 07-01-2012 at 02:57 AM..
# 4  
Old 07-01-2012
If you'd like a reasonably bullet-proof solution, specify the exact forms that the numeric representation is allowed to assume.

Because we have no context, it's possible that the best solution lies in an earlier step, when or before the variable is assigned.

In my opinion, Scrutinizer's first suggestion is best. It doesn't require any external utilities, so it's efficient. It is also trivially extensible, for handling multiple representations.

I wouldn't recommend the following for production use. It's more an exercise in utility abuse than anything else Smilie:
Code:
expr "$p" >/dev/null 2>&1

Regards,
Alister
# 5  
Old 07-01-2012
Thanks everybody...

All are very helpful examples.... But i'm gonna use Scrutinizer's second example as it is bit extendable for me in my requirement.

Thanks again for all your valuable inputs.

Regards,
Raghu
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash -o -v -R operators

I do not know the use of the -o -v -R operators. This is what the info says and I am confused of what optname and varname mean, are they just normal variable? -o optname True if the shell option optname is enabled. See the list of options under the ... (6 Replies)
Discussion started by: kristinu
6 Replies

2. Programming

Combining Operators

Hey everyone, I'm really getting into learning C, but as I look at more advanced example code, I see things like if (!*variable1) blah blah blah... and variable2 ^= *(variable1++); my question is, when you have a combination of two operators, like !*. The ! means 'not' and the *... (2 Replies)
Discussion started by: Lost in Cyberia
2 Replies

3. Shell Programming and Scripting

Array operators

Hi Lets say I have two arrays: VAR_1 = "File_A" "File_B" "File_C" "File_D" VAR_2 = "File_A" "File_D" Is there a simple command to get the difference of these list, i.e. VAR_1_2 = "File_B" "File_C" or do I have to write a script and loop through all elements and compare them one by one? ... (1 Reply)
Discussion started by: Wernfried
1 Replies

4. Homework & Coursework Questions

Operators

I really don't know the meaning of these operators. Could someone explain the meanings? <, <=, ==, !=, >=, >, ||, &&, ! ~ , !~ Thanks! (1 Reply)
Discussion started by: Erjen
1 Replies

5. Shell Programming and Scripting

Operators

I really don't know the meaning of these operators. Could someone explain the meanings so I can make my test for today? <, <=, ==, !=, >=, >, ||, &&, ! ~ , !~ Thanks! (1 Reply)
Discussion started by: Erjen
1 Replies

6. Shell Programming and Scripting

And and OR Operators with If Statement.

Hi All, I have 2 variables. Result1 and Result2. I want to put a condition that if Both are True then echo "All True" Else Show Error. Right now i am doing this and getting error. if ; then echo "All True" else echo "Failed" fi; Error. line 8: ' Solution: Looking for (2 Replies)
Discussion started by: mkashif
2 Replies

7. UNIX for Dummies Questions & Answers

Operators

I am trying to understand Does the following: {tmp+=$10} Mean take $10 and add them all up and call it tmp thanks! (2 Replies)
Discussion started by: llsmr777
2 Replies

8. UNIX for Dummies Questions & Answers

Arithmetic Operators

Hello, I have a list of 'inputs' and i want to convert those on the second list named 'Desired Outputs', but i don't know how to do it? Inputs Desired Outputs 1 2 94 4 276 8 369 10 464 12 ... (0 Replies)
Discussion started by: filda
0 Replies

9. UNIX for Advanced & Expert Users

If with set operators

Hi guys, I'm trying to run more than one "if" condition at once. What I want is something like if ] or ] or ]; then ... I can't remember the syntax for using this or/and set operators. Can someone please assist/ jog my memory? thanks Khoom (2 Replies)
Discussion started by: Khoomfire
2 Replies
Login or Register to Ask a Question