comparing content of 2 variables in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comparing content of 2 variables in script
# 1  
Old 04-24-2008
comparing content of 2 variables in script

hello
how can i compare the content of two variables using the if or for loops.
I have 2 variables which was formed as result of commands pass into them but i want to now compare the 2 contents and echo where their is a match
for examples
variable1=`cat file2`
variable2=`cat file3`
if [ `$variable1` = `$variable2` ] # looking for content match of strings
then
echo "variable1"
else
echo " no match found"
fi
# 2  
Old 04-24-2008
if you're comparing files, 'man comm'
# 3  
Old 04-24-2008
re

Thanks for the reply
its not realy a file but an output .
Am actually doing this
variable1=`cat /etc/passwd | cut -d ':' -f5` #getting the username
variable2=`who` # logging user
now am comparing this two variable if thier some match then it would echo those match ie where the username matches the logging then it produce the logging name
# 4  
Old 04-24-2008
Code:
variable1=`cat file2`
variable2=`cat file3`
if [ "$variable1" = "$variable2" ] # looking for content match of strings
then
   echo "variable1"
else
   echo " no match found"
fi

Jean-Pierre.
# 5  
Old 04-24-2008
@Sam

You are using who command. In a network if multiple users logs in then you will get lots of output.

x033870@agamemnon:/home/x033870>who
c028407 pts/1 Mar 27 10:31 (GDISpcp286146pcs.mis.amat.com)
c028407 pts/2 Mar 3 11:23 (GDISpcp286146pcs.mis.amat.com)
c028407 pts/3 Mar 20 22:10 (GDISpcp286146pcs.mis.amat.com)
c028407 pts/4 Mar 11 11:29 (GDISpcp286146pcs.mis.amat.com)
c028407 pts/6 Mar 28 14:22 (GDISpcp286146pcs.mis.amat.com)
x033870 pts/7 Apr 24 11:35 (KLXVF2D.mis.amat.com)
c028407 pts/8 Apr 2 10:17 (GDISpcp286146pcs.mis.amat.com)
c028407 pts/9 Apr 2 10:19 (GDISpcp286146pcs.mis.amat.com)

What you want to acheive here. Can you tell me the exact requirement.
# 6  
Old 04-24-2008
Quote:
Originally Posted by sam4now
Thanks for the reply
its not realy a file but an output .
Am actually doing this
variable1=`cat /etc/passwd | cut -d ':' -f5` #getting the username
variable2=`who` # logging user
now am comparing this two variable if thier some match then it would echo those match ie where the username matches the logging then it produce the logging name
you cannot do it like this - you need to iterate through the logged in users. Something along these lines:
Code:
#!/usr/bin/ksh

who | while read id junk
do
   awk -F: -v id="${id}" '$1 == id { print $5;exit}' /etc/passwd
done | sort -u

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

Comparing content of two variables

I have very abstract need of "comparing two variables" and take subsequent actions. please refer to image below https://lh3.googleusercontent.com/-frNk5iA3q1c/TjI3lE0sWOI/AAAAAAAAAIE/fxzB1w07gas/script_block.JPG I have a part of script which reads a file and generates variables based on... (4 Replies)
Discussion started by: animesharma
4 Replies

3. Shell Programming and Scripting

Comparing Alphanumeric Variables in Shell Script

Can someone please help me out here? I have strings similar to aafafaff45,29.34.942.45,edfdfafa that i want to compare to another similar string to check if they are the same. my script isn't working. ONE="aafafaff45,29.34.942.45,edfdfafa" TWO="ddfafagfa,87.57.942.45,afafafff" if ONE is... (5 Replies)
Discussion started by: SkySmart
5 Replies

4. Shell Programming and Scripting

Comparing 2 csv files and matching content

Hello, I have the following problem: There are two csv files csv-file #1: aaa1, aaa2, ... aaan aaa1, bbb2, ... bbbn aaa1, ccc2, ... cccn bbb1, bbb2, ... bbbn ... zzz1, zzz2, ... zzzn csv-file #2: aaa1, matchvalue1 ccc1, matchvalue2 (7 Replies)
Discussion started by: ghl10000
7 Replies

5. 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

6. Shell Programming and Scripting

comparing files content

hi i have a set of files , i need to compare one file content with other file content, i am using cmp -s abc.1 def.2 , but it is not giving theproper o/p even if the content is different.Please help thanks Satya (1 Reply)
Discussion started by: Satyak
1 Replies

7. UNIX for Dummies Questions & Answers

comparing the content of two directories

Hello I want to compare the content of two directories recursively to check if the two directories have the same files. How can I do that? (2 Replies)
Discussion started by: xyzt
2 Replies

8. 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

9. 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

10. Shell Programming and Scripting

comparing file content differences

I need to write a script to find out if there are any .c files created/removed from the last time i monitored the files available. i first created a file to contain all the .c files available on the system. (ls *.c > file1) I created another file using the same command. I used the comm file1... (4 Replies)
Discussion started by: RianTan
4 Replies
Login or Register to Ask a Question