How do I compare mixed variables?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I compare mixed variables?
# 1  
Old 03-24-2007
How do I compare mixed variables?

Searched all over can't figure out how to compare variables of alpha and numeric characters.

Example a script that ask user to enter a password and then ask to repeat the password.
# 2  
Old 03-24-2007
Password script

Is this is of any help ? . Please let us know

HTML Code:
#!/bin/ksh
stty -echo -icanon -iuclc
print "Enter you password:\c"
read passwd;
while read  passwd2?"Re enter you password:"
do
 if [[ passwd -ne passwd2 ]] ; then
   print "Password mismatch.Please try again"
 else
   print "You have set ur password as $passwd"
   exit
 fi
done
Thanks,
Nagarajan Ganesan
# 3  
Old 03-24-2007
if [[ passwd -ne passwd2 ]] ; then

Couple of problems there. First you need $ in front of the variables. As written you are comparing "passwd" to "passwd2". Second -ne is a numeric compare.
Code:
$ x1="    123"
$ x2="123"
$
$ [[ $x1 -ne $x2 ]] && echo they are not equal
$ [[ $x1 != $x2 ]] && echo they are not equal
they are not equal
$

# 4  
Old 03-25-2007
I mean if I have a password like fjiK55FF.

If the variable has both alpha and numeric characters. Like a good password should have. Ex. tyyhe84949kjk

Thanks
# 5  
Old 03-25-2007
Quote:
Originally Posted by monx
If the variable has both alpha and numeric characters. Like a good password should have. Ex. tyyhe84949kjk

Thanks
Then treat it just as you would a pure alpha string.
# 6  
Old 03-25-2007
Thanks Perderabo, for spotting out the error

The corrected code would be,

HTML Code:
#!/bin/ksh
stty -echo -icanon -iuclc
print "Enter you password:\c"
read passwd;
while read  passwd2?"Re enter you password:"
do
 if [[ $passwd != $passwd2 ]] ; then
   print "Password mismatch.Please try again"
 else
   print "You have set ur password as $passwd"
   exit
 fi
done
Thanks
Nagarajan Ganesan.
# 7  
Old 03-26-2007
thanks for your help.

Got it working perfect. It was a lesson from the book I'm reading.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare two variables and print the difference

compare two variables and print the difference i have two variables X1=rac1,rac2 Y1=rac2,rac3 output=rac1,rac3 Use code tags to wrap code fragments or data samples. (1 Reply)
Discussion started by: jhonnyrip
1 Replies

2. Shell Programming and Scripting

Compare two variables and print the difference

Hi PRIM_SEQ=`some sql code` and output of PRIM_SEQ is like below 120 130 STB_SEQ=`some sql code` and output of STB_SEQ is like below 115 110 i need to compare this two variables output ( decimal numbers) 1) What I want to do is to compare every number in the PRIM_SEQ with... (8 Replies)
Discussion started by: amar1208
8 Replies

3. Shell Programming and Scripting

Compare and calculate two variables

Hi, I have two variables with some values with 5 decimal digits and separated with space, the values of the variables are different, I want to make operations with this two variables. The way was I find to do it itīs with files and paste file1 file2 | awk '{print $1 - $2}' I want to make it... (8 Replies)
Discussion started by: faka
8 Replies

4. Shell Programming and Scripting

How to use FS for mixed file?

Hi! All I am just wondering solution to use FS if file fields are separated by whitespace (one or more spaces ), tab and comma, How to use FS ? finally I want to print all columns as tab separated look at my file here tagged 130, US 121337 30.530 -58.900 1941 1 25 19.50 ... (5 Replies)
Discussion started by: Akshay Hegde
5 Replies

5. Shell Programming and Scripting

How to compare floating variables , integer value expected?

I am running some commands and I am trying to get an output into a variable. I am having problem when I try to put that value in while loop, it says integer value expected. What's the best way to accomplish this remaining=$(symclone -sid XXX -f Clone_test query | grep MB | awk '{print... (1 Reply)
Discussion started by: rajsan
1 Replies

6. Shell Programming and Scripting

using awk compare two variables in bash

ok this is probably going to turn out to be something really stupid but i've tried to use the following command in a script but the output is just a blank screen and i have to use Ctrl c to exit it. awk 'BEGIN {printf "%.2f\n", '${bashArray}'>='$Variable' {print '${bashArray}'}}' the command... (2 Replies)
Discussion started by: zagreus360
2 Replies

7. Shell Programming and Scripting

HELP: compare floating point variables??

Hi All, I got this script that pulls the Amps value from our RPC's , I basiclly want to compare the valued with a "limit" value -- if the numbers match or are greater than the definded value ...do something. My problem is I cant seem to figure out how to compare floating points... here is... (1 Reply)
Discussion started by: zeekblack
1 Replies

8. Shell Programming and Scripting

Awk - Compare fields and increment variables

Hi, My first post to this group... I have a need to to parse a source file which is a capture from a network analyser. I have two fields that need to be checked: - Field 7 represents the packet length (an integer), and Field 4 represents a network address (e.g. 192.168.25.3) - The... (10 Replies)
Discussion started by: mv652
10 Replies

9. Shell Programming and Scripting

Compare String Variables for Greater or Less Than?

Is there any way to compare two strings for a greater or less than condition? For example, StringA is "apple" and StringB is "bonnet" and StringC is "captain". Can I only test for equal/not-equal, or is there a way to find out whether StringA is less than StringB, and StringC is greater than... (6 Replies)
Discussion started by: OPTIMUS_prime
6 Replies

10. Shell Programming and Scripting

Compare two variables

Hi guys, How do I compare two variables using diff? The way I'm thinking: #!/bin/sh a=cat asdf.x | wc -l b=cat asdf.y |cut -d',' -f5 |grep -v '^$' |wc -l diff $a $b How to rewrite the above sciprt using only one line in command prompt? (2 Replies)
Discussion started by: onthetopo
2 Replies
Login or Register to Ask a Question