If condition getting bypassed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If condition getting bypassed
# 1  
Old 04-17-2005
If condition getting bypassed

Hi,
I am trying something like:

c=`awk -F"|" '{ print NF }' test.txt`
echo "c is $c"
if [ c > 2 ]
then
echo "More than One field"
else
echo "One field"
fi

The test.txt file has something like:
lkadaldj|
fdsakfdsafd|
ddddddd|

The value of "c" is 2 in above case. If any rows in the file has more than 2 columns(something like: lkadaldj999|2345| ) then it should get inside "if" condition.

But the above code is bypassing the "if condition for all the rows" and getting inside the "if" statement even if the value of "c" is 2 for all the rows. I am getting "More Than One Field" as an answer even if all the rows are having two columns. I am using ksh.

I also tried something like:
if test $c -gt 2 ---> I get error :test: 2: unknown operand"

I will really appreciate any help in correcting this code.

Thanks
Raj
rkumar28
# 2  
Old 04-17-2005
You may have to try some thing like this:

Code:
 awk -F"|" '{ if (NF >2 ) { print "More than One field" }else { print "One field }
}' test.txt

# 3  
Old 04-17-2005
When comparing integers, use the notation: -gt (greather than), -lt (less than), -ge (greater than or equal), etc. Also, to access the value of a variable, you must prefix the variable with a $ sign. The quotes around the code example below are there in the event that c is null (not set). Since the shell will expect a value, the quotes will basically say, no value. Otherwise you will get an error back from the shell when it attempts to make the comparison.

Change this line:
Code:
if [ c > 2 ]

To:
Code:
if [ "$c" -gt "2" ]

# 4  
Old 04-17-2005
This isnt the problem in your shell but using back ticks is old school and is less readable than the new form. New form also allows nesting of variables which is pretty powerful.

old
c=`awk -F"|" '{ print NF }' test.txt`
new
c=$(awk -F"|" '{ print NF }' test.txt)
# 5  
Old 04-17-2005
If condition getting bypassed

Thanks for lot for replying so soon on this.
I changed the awk statement and the "if" statement as suggested:

#c=`awk -F"|" '{ print NF }' test`
c=$(awk -F"|" '{ print NF }' test)

echo "c is $c"

#if [ c > 2 ]

if [ "$c" -gt "2" ]
then
echo "Inside if"
else
echo "else part"
fi

I get the error below:
c is 2
2
2
2
2
[: 2
2
2
2
2: bad number
else part


But the statement below did work:

awk -F"|" '{ if (NF >2 ) { print "More than One field" }else { print "One field" }
}' test

Is there a way to provide the exit status of 2(exit 2) in the first part.Something like below. This script is called from other script and expect an exit status of 2 if there is "More than One field"

awk -F"|" '{ if (NF >2 ) { print "More than One field" exit 2}else { print "One field" }
}' test


Thanks again for the help and time in this regards.
Raj
rkumar28
# 6  
Old 04-17-2005
try
Code:
awk -F"|" '{ if (NF >2 ) {print "More than a 2 fields" ; exit }else { print "one field" ; print FNR  } }' test.txt

# 7  
Old 04-18-2005
Quote:
Originally Posted by bhargav
try
Code:
awk -F"|" '{ if (NF >2 ) {print "More than a 2 fields" ; exit }else { print "one field" ; print FNR  } }' test.txt

Hi,
The statement above worked. Is there a way to make the above statement send an email. I am trying something below:

email1=`"$message3" | /usr/bin/mailx -s "$SUBJECT_EMAIL" $toMail1`
# Check for the columns in the file
awk -F"," '{ if (NF >2 ) {print "$email1" ; exit 2 }else { print "one field" ; print FNR } }' $file1


Thanks
Raj
rkumar28
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

IF [ condition ] help

Hi all Unix newbie - please be gentle Am modifying an existing script to error trap a variable with a length of 0 #!/bin/bash ipfile='/var/data/bin/ipaddress' ] && ipold="$(< "$ipfile" )" ipnew="$( wget -q -O - checkip.dyndns.org | sed -e 's/.*Current IP Address: //;s/<.*$//' )" #... (6 Replies)
Discussion started by: CRChamberlain
6 Replies

2. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

3. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

4. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

5. Shell Programming and Scripting

or in a IF condition

Hi I have to evaluate multiple conditions with an 'or'. Here is an example: if when i use the above i get a error message ' Please help me to know if i am missing something in the syntax. how do i achieve multiple "or" in the same if condition. Thanks. (11 Replies)
Discussion started by: sunrexstar
11 Replies

6. Shell Programming and Scripting

need help in IF condition!!

Hi All ,, This is my code .. i am checcking a file temp66.txt , when i execute this shell it checks whether the file is 0 byte file or not .. if it is a 0 byte file , then Program exists out else it Prints the Echo Statement .. if then RET_CODE=$? if then echo 'File... (18 Replies)
Discussion started by: raghav1982
18 Replies

7. Shell Programming and Scripting

if condition

Hi how to write this: if then usage fi thx (3 Replies)
Discussion started by: melanie_pfefer
3 Replies

8. Shell Programming and Scripting

if condition

Hi friends, :) In a shell script i found the following if condition. echo -n "Which version of $1 do you want to restore ('0' to quit)? : " read desired if ${desired:=1} -ge $index ] ; then echo "$0: Restore canceled by user: index value too big." >&2 exit 1 fi Can... (1 Reply)
Discussion started by: ravi raj kumar
1 Replies

9. UNIX for Dummies Questions & Answers

if condition

suppose a name is read how to check whether the name contains only alphabets and space not non-alphabetic characters and special characters (5 Replies)
Discussion started by: manisha_agrawal
5 Replies
Login or Register to Ask a Question