unix compare two variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers unix compare two variable
# 1  
Old 11-19-2009
unix compare two variable

how can i unix compare two variable??

var1 = 6499 7328 6351 7583 7573
var2 = 6499 7328 6351 7583 7573
i did:
diff $var1 $var2
and i got the output:
diff: extra operand `6351'
diff: Try `diff --help' for more information.


whats wrong??
# 2  
Old 11-19-2009
Hi.

'Diff' command is used to compare files.

To compare two variables you must use:
Code:
if [ "$var1" = "$var2" ]
.....

# 3  
Old 11-19-2009
You use test to compare variables.

Code:
if test "var1" = "$var2"; then
   ....
fi

or 

if [ "$var1" = "$var2" ]; then
  ...
fi

Code:
man test

# 4  
Old 11-19-2009
ok, but i wanna know what is the different between the 2 variable
like if:
var1 = a b c d
var2 = a b c f
so i need to know that the different is that var2 have f insted of d
or somthing like that...

??
# 5  
Old 11-19-2009
diff is a command that compares 2 files not 2 strings. You probably mean something like this.

Code:
 #!/bin/bash
  s_one='string'
   s_two='String'
  if [ $s_one=$s_two ];
   then
    echo "s_one('$s_one') is not equal to s_two('$s_two')"
   fi
    if [ $s_one=$s_one ];
    then
         echo "s_one('$s_one') is equal to s_one'$s_one')"
      fi

Hope this helps
# 6  
Old 11-19-2009
Checking to see if the variables are different, and checking if one variable contains a certain character are not the same thing.

Code:
if [ "$var1" != "$var2" ]; then
  echo var1 and var2 are different
  [ "${var2%%f*}" != "$var2" ] &&  echo var2 contains an f
fi

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare pattern in variable

How to check if pattern is matching in variable or not. I want to check file name in variable with the pattern. Eg: file_name="AB1000.csv" Check for below patterns pattern1 = AB??? pattern2 = abc??* if file_name == <patten1> then ... elif file_name == <pattern2> ---... (4 Replies)
Discussion started by: vegasluxor
4 Replies

2. Shell Programming and Scripting

Compare the two variable with if condition

Please help me with this: I need to compare two values in if condition in shell script but its goes always to else condition: TIME_CHECK=PM TIME-CLOCK=PM if ; then echo "You have access!" else echo "ACCESS DENIED!" fi (5 Replies)
Discussion started by: aroragaurav.84
5 Replies

3. UNIX for Dummies Questions & Answers

awk unable to compare the shell variable

Hi Could anyone please help with Awk. The below code prints the PID of the matching process with condition with $8 and $9 ps -ef |awk '($8~/proc/) && ($9~/rPROC2/) {print $2}' Now i want to change the Constant PROC2 from Shell variable PROC2 is already declared in shell variable SRVNAME... (9 Replies)
Discussion started by: rakeshkumar
9 Replies

4. Red Hat

Compare Empty Variable

Hello All, I am running the below code in my script. I want if jk is empty nothing should be appened to the file total_usage. but apparently its not happening.Kindy let me know how to do it. ################################### jk=`ps auxf |grep -w $inputline|tr -s " "|cut -d... (0 Replies)
Discussion started by: ajaincv
0 Replies

5. Shell Programming and Scripting

KSH: Compare variable to $1 in an input file

Hello, I am working with KSH on AIX and I have 2 files generated from different sources... as seen below: FILE1 FILE2 AAA AAA@ABS0001C BBB BBB@ABS0003D CCC CCC@ABS0023A DDD DDD@ABC0145D EEE EEE@ABS0090A FFF FFF@ABS0002A GGG GGG@ABC0150D HHH FILE1 is main main data source,... (4 Replies)
Discussion started by: right_coaster
4 Replies

6. Shell Programming and Scripting

AWK help: how to compare array elements against a variable

i have an array call ignore. it is set up ignore=34th56 ignore=re45ty ignore=rt45yu . . ignore=rthg34 n is a variable. I have another variable that i read from a different file. It is $2 and it is working the way i expect. array ignore read and print correct values. in the below if... (2 Replies)
Discussion started by: usustarr
2 Replies

7. Shell Programming and Scripting

compare two variable value in unix

dear all i had two variable in my script. var1 and var2 var1= 10 ( it will part of date op ) Fri Aug 8 10:05:09 IST 2008 var2=9 ( it will op of ls -ltr and time part of it.) now i want to compare of them and want to do some task if both are are not same. kindly let me know possible... (0 Replies)
Discussion started by: jaydeep_sadaria
0 Replies

8. Shell Programming and Scripting

compare a variable against array elements

hi everyone - i have a bash script that does, in broad terms, the following: given a file containing a list of email accounts, for every account, do , then move on to the next account. pretty simple, and all that stuff works fine. thing is, there's a very good change that any account... (2 Replies)
Discussion started by: fearboy
2 Replies

9. Shell Programming and Scripting

compare variable against regular expression?

is it possible? if so, how? i want to check a variable whether is it a number or letter in an if-else statement (6 Replies)
Discussion started by: finalight
6 Replies

10. UNIX for Dummies Questions & Answers

Compare the content of a variable with a string

Hello all: I'm new in Unix and here and I'am spanish so my english isn't so good to explain my doubt. Here it is. Very urgent: I need to compare the value of a variable with a string. Example is this. Imagine that the variable x1 contains the path and a file text and I need to compare... (2 Replies)
Discussion started by: robdai
2 Replies
Login or Register to Ask a Question