LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2007, 11:36 AM   #1
SeRGeiSarov
Member
 
Registered: Jan 2007
Location: Russia, Nizhniy Novgorod Region, Sarov town
Distribution: Mandriva 2007, Fedore Core 6
Posts: 52

Rep: Reputation: 15
How to compare environment variables


Hi all.

I have the following example:

Code:
PATH=/bin:/usr/bin
new=/tmp/wwho1.$$
old=/tmp/wwho2.$$
>$old
while :
do
  who >$new
  diff $old $new
  mv $new $old
  sleep 60
done
I want to use environment variables instead of 'new' and 'old' files. How to compare environment variables?
 
Old 07-24-2007, 06:58 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Code:
new=$(who)
if [[ $new != $old ]]
then
    do something
fi
old=$new
Your current code diffs the 2 temp files but doesn't care about the results... Why?
 
Old 07-25-2007, 05:34 AM   #3
SeRGeiSarov
Member
 
Registered: Jan 2007
Location: Russia, Nizhniy Novgorod Region, Sarov town
Distribution: Mandriva 2007, Fedore Core 6
Posts: 52

Original Poster
Rep: Reputation: 15
diff - compare files line by line

I want to use environment variables instead files. How to do this?
 
Old 07-25-2007, 04:32 PM   #4
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Quote:
Originally Posted by SeRGeiSarov
diff - compare files line by line

I want to use environment variables instead files. How to do this?
With bash/zsh and some other modern shells you could:

Code:
% a="a" b="b";diff <(echo "$a") <(echo "$b") 
1c1
< a
---
> b
 
Old 07-26-2007, 02:11 AM   #5
SeRGeiSarov
Member
 
Registered: Jan 2007
Location: Russia, Nizhniy Novgorod Region, Sarov town
Distribution: Mandriva 2007, Fedore Core 6
Posts: 52

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by radoulov
With bash/zsh and some other modern shells you could:

Code:
% a="a" b="b";diff <(echo "$a") <(echo "$b") 
1c1
< a
---
> b
I don't understand how this script work. Please explain this script.
 
Old 07-26-2007, 04:40 AM   #6
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Quote:
Originally Posted by SeRGeiSarov
I don't understand how this script work. Please explain this script.
Compares variables with diff (as you asked):

Code:
% echo "$var1"
line1
line2
line3
% echo "$var2"
line1
line2
% diff <(echo "$var1") <(echo "$var2") 
3d2
< line3
Is it clear now?
 
Old 07-26-2007, 08:40 AM   #7
SeRGeiSarov
Member
 
Registered: Jan 2007
Location: Russia, Nizhniy Novgorod Region, Sarov town
Distribution: Mandriva 2007, Fedore Core 6
Posts: 52

Original Poster
Rep: Reputation: 15
Code:
man diff

Display the differences between two files, or each corresponding file in two directories.
Each set of differences is called a "diff" or "patch". For files that are identical, `diff' normally produces no output; for binary (non-text) files, `diff' normally reports only that they are different.
...
In the simplest case, diff compares the contents of the two files from-file and to-file. A file name of - stands for text read from the standard input.

If from-file is a directory and to-file is not, diff compares the file in from-file whose file name is that of to-file, and vice versa. The non-directory file must not be -.

If both from-file and to-file are directories, diff compares corresponding files in both directories, in alphabetical order; this comparison is not recursive unless the -r or --recursive option is given.

GNU `diff' can show whether files are different without detailing the differences.
It also provides ways to suppress certain kinds of differences that are not important to you.

Most commonly, such differences are changes in the amount of white space between words or lines. `diff' also provides ways to suppress differences in alphabetic case or in lines that match a regular expression that you provide.
I don't see anything about it. Is this feature of BASH?
 
Old 07-26-2007, 08:42 AM   #8
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Quote:
Originally Posted by SeRGeiSarov
[...]
I don't see anything about it. Is this feature of BASH?
The process substitution (<(...)): yes.
 
Old 07-26-2007, 10:29 AM   #9
SeRGeiSarov
Member
 
Registered: Jan 2007
Location: Russia, Nizhniy Novgorod Region, Sarov town
Distribution: Mandriva 2007, Fedore Core 6
Posts: 52

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by radoulov
The process substitution (<(...)): yes.
Thank you
 
  


Reply

Tags
environment, variables



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting all environment variables Harlin Linux - General 2 10-06-2006 12:30 PM
when to use environment variables? mark_2811 Programming 2 02-23-2004 06:09 PM
environment variables moeminhtun Linux - General 4 01-15-2003 09:50 AM
environment variables. jISV Linux - General 0 04-05-2002 06:01 AM
environment variables aethereal Linux - Newbie 7 12-19-2001 09:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration