Comparing String with Number variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing String with Number variables
# 1  
Old 04-29-2009
Comparing String with Number variables

I have two variables and want to perform some functions after comparison

Code:
$cat file1
14.abcde
 
a=`cut -f 1 -d "." file1
b=15
 
if [ $a -lt $b ] 
then
   ....
fi

but i got an error message says that unary operator expected
and i think its because of $a is a string and trying to compare with integer variable.

Can someone help me to fix this ??
# 2  
Old 04-29-2009
Code:
#!/bin/ksh

b=15

while IFS=. read num rest
do
   if [ "${num}" -lt "${b}" ]; then
      printf "%d < %d\n" "${num}" "${b}"
   fi
done < file1

# 3  
Old 04-29-2009
Never Mind.
I solved it Thanks
# 4  
Old 04-29-2009
Code:
a=`echo "14.abcde" | cut -f 1 -d "."`
b=15
if [ $a -lt $b ]
then
echo "less"
fi

cheers,
Devaraj Takhellambam
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing two variables

I have a script like this. Just couldn't get the comparison part work. Any thought? thanks, #!/usr/bin/ksh -x STEP=`echo $(basename $0 .ksh) | tr "" ""` log=/skip.log while read LINE do if then echo `date`: STEP $STEP skipped by user >> $log exit 0 fi done < $1 echo... (0 Replies)
Discussion started by: ghostmic
0 Replies

2. Shell Programming and Scripting

awk comparing variables

Is there a way to compare variables in a 'awk'? I've been trying for a while and can't figure it out. I'm guessing its not possible :/ VAR=Bob awk '$3 == $VAR { print $1 }' file.txt Regards Jikuu (4 Replies)
Discussion started by: Jikuu
4 Replies

3. UNIX for Dummies Questions & Answers

Comparing a String variable with a string literal in a Debian shell script

Hi All, I am trying to to compare a string variable with a string literal inside a loop but keep getting the ./testifstructure.sh: line 6: #!/bin/sh BOOK_LIST="BOOK1 BOOK2" for BOOK in ${BOOK_LIST} do if then echo '1' else echo '2' fi done Please use next... (1 Reply)
Discussion started by: daveu7
1 Replies

4. Shell Programming and Scripting

comparing variables to date as string

I have a file $ cat myfile A 02/16/2012 B 02/19/2012 C 02/20/2012 D 02/17/2012 E 02/16/2012 My simple script > cat myscript.sh mydate="02/16/2012" awk ' ($2~/$mydate/) {print $1}' < myfile but I got no output! and when I try $2~/'$mydate'/ I got: The error context is (2 Replies)
Discussion started by: Sara_84
2 Replies

5. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

6. UNIX for Dummies Questions & Answers

comparing variables

im trying to compare ipaddresses. i loop through an array to see if the ip is already is in the array and if it is it should set a flag and then i wont add it to the array. but its just adding all the ipaddresses to the array if ] then ... (3 Replies)
Discussion started by: magnia
3 Replies

7. Shell Programming and Scripting

Comparing Variables in Perl

Hi. I have three arrays. @a=('AB','CD','EF'); @b=('AB,'DG',HK'); @c=('DD','TT','MM'); I want to compare the elements of the first two array and if they match then so some substition. I tried using the if statement using the scalar value of the array but its not giving me any output. ... (7 Replies)
Discussion started by: kamitsin
7 Replies

8. UNIX for Dummies Questions & Answers

comparing 2 string variables in unix

search=Jul 22 date=Jul 22 if then echo They match >>this piece of code does not detect that search and date are equivalent strings.. what's wrong with it? (17 Replies)
Discussion started by: katdaniel16
17 Replies

9. Shell Programming and Scripting

Comparing two variables

Script #!/bin/sh hardware=PC os=WindowsNET for i in `cat newservers` do x=`sudo /opt/openv/netbackup/bin/admincmd/bpplclients |grep $i |head -40 |grep $i|awk '{print $3;exit}'` if then echo "$i is already added" else echo "Need to add" fi done O/p in debug mode bash-2.05$... (3 Replies)
Discussion started by: rajip23
3 Replies

10. UNIX for Dummies Questions & Answers

comparing variables

I have searched and found a few threads that have dealt with this, but the examples I've tried haven't seemed to help. I am monitoring our database log for high checkpoints. I can parse out the checkpoint value which can be anywhere from zero into a 3 digit number. I set a variable to be the... (3 Replies)
Discussion started by: MizzGail
3 Replies
Login or Register to Ask a Question