LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to compare environment variables (https://www.linuxquestions.org/questions/programming-9/how-to-compare-environment-variables-571919/)

SeRGeiSarov 07-24-2007 11:36 AM

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?

chrism01 07-24-2007 06:58 PM

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?

SeRGeiSarov 07-25-2007 05:34 AM

diff - compare files line by line

I want to use environment variables instead files. How to do this?

radoulov 07-25-2007 04:32 PM

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


SeRGeiSarov 07-26-2007 02:11 AM

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.

radoulov 07-26-2007 04:40 AM

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?

SeRGeiSarov 07-26-2007 08:40 AM

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?

radoulov 07-26-2007 08:42 AM

Quote:

Originally Posted by SeRGeiSarov
[...]
I don't see anything about it. Is this feature of BASH?

The process substitution (<(...)): yes.

SeRGeiSarov 07-26-2007 10:29 AM

Quote:

Originally Posted by radoulov
The process substitution (<(...)): yes.

Thank you :)


All times are GMT -5. The time now is 03:46 PM.