Discussion:
Appreciate advice on %if %foo || %bar syntax
Patrick
2007-08-29 18:28:19 UTC
Permalink
Hi all,

I have a spec file which tests for various distro's:

%define is_fc45 %(test -f /etc/fedora-release && cat /etc/fedora-release | egrep -q '4|5' && echo 1 || echo 0)
%define is_fc6 %(test -f /etc/fedora-release && grep -c Zod /etc/fedora-release || echo 0)
%define is_f7 %(test -f /etc/fedora-release && grep -c Moonshine /etc/fedora-release || echo 0)
%define is_rhel4 %(test -f /etc/redhat-release && cat /etc/redhat-release | egrep -q 'release 4' && echo 1 || echo 0)
%define is_rhel5 %(test -f /etc/redhat-release && cat /etc/redhat-release | egrep -q 'release 5' && echo 1 || echo 0)

Now I would like to use something like this:

%if %is_fc6 || %is_f7 || %is_rhel5
..do something
%endif

I tried this on a laptop with Fedora 7 but it did not work. Does anyone
know if it is possible to use an "OR" here and how?

Thanks and regards,
Patrick
Michael Jennings
2007-08-29 21:48:33 UTC
Permalink
On Wednesday, 29 August 2007, at 20:28:19 (+0200),
Post by Patrick
%define is_fc45 %(test -f /etc/fedora-release && cat /etc/fedora-release | egrep -q '4|5' && echo 1 || echo 0)
%define is_fc6 %(test -f /etc/fedora-release && grep -c Zod /etc/fedora-release || echo 0)
%define is_f7 %(test -f /etc/fedora-release && grep -c Moonshine /etc/fedora-release || echo 0)
%define is_rhel4 %(test -f /etc/redhat-release && cat /etc/redhat-release | egrep -q 'release 4' && echo 1 || echo 0)
%define is_rhel5 %(test -f /etc/redhat-release && cat /etc/redhat-release | egrep -q 'release 5' && echo 1 || echo 0)
%if %is_fc6 || %is_f7 || %is_rhel5
..do something
%endif
%if %{is_fc6}%{is_f7}%{is_rhel5}

will expand to

%if 010

and will do exactly what you want.

Michael
--
Michael Jennings (a.k.a. KainX) http://www.kainx.org/ <***@kainx.org>
Linux Server/Cluster Admin, LBL.gov Author, Eterm (www.eterm.org)
-----------------------------------------------------------------------
"You did not tell the truth, and so you will have to pay the
consequences." -- Bob Barker, "Truth or Consequences"
Tony Earnshaw
2007-08-30 06:12:53 UTC
Permalink
Post by Michael Jennings
Post by Patrick
%define is_fc45 %(test -f /etc/fedora-release && cat /etc/fedora-release | egrep -q '4|5' && echo 1 || echo 0)
%define is_fc6 %(test -f /etc/fedora-release && grep -c Zod /etc/fedora-release || echo 0)
%define is_f7 %(test -f /etc/fedora-release && grep -c Moonshine /etc/fedora-release || echo 0)
%define is_rhel4 %(test -f /etc/redhat-release && cat /etc/redhat-release | egrep -q 'release 4' && echo 1 || echo 0)
%define is_rhel5 %(test -f /etc/redhat-release && cat /etc/redhat-release | egrep -q 'release 5' && echo 1 || echo 0)
%if %is_fc6 || %is_f7 || %is_rhel5
..do something
%endif
%if %{is_fc6}%{is_f7}%{is_rhel5}
will expand to
%if 010
and will do exactly what you want.
Thanks from somebody else :)

Quite another thing is, that a more rational test would be something like:

"test -f /etc/redhat-release && grep 'release 5' /etc/redhat-release &&
echo 1 || echo 0"

Redundant use of "cat|grep" and egrep ...

--Tonni
--
Tony Earnshaw
Email: tonni at hetnet dot nl
Patrick
2007-08-30 10:28:02 UTC
Permalink
Post by Tony Earnshaw
Post by Michael Jennings
Post by Patrick
%define is_fc45 %(test -f /etc/fedora-release && cat /etc/fedora-release | egrep -q '4|5' && echo 1 || echo 0)
%define is_fc6 %(test -f /etc/fedora-release && grep -c Zod /etc/fedora-release || echo 0)
%define is_f7 %(test -f /etc/fedora-release && grep -c Moonshine /etc/fedora-release || echo 0)
%define is_rhel4 %(test -f /etc/redhat-release && cat /etc/redhat-release | egrep -q 'release 4' && echo 1 || echo 0)
%define is_rhel5 %(test -f /etc/redhat-release && cat /etc/redhat-release | egrep -q 'release 5' && echo 1 || echo 0)
%if %is_fc6 || %is_f7 || %is_rhel5
..do something
%endif
%if %{is_fc6}%{is_f7}%{is_rhel5}
will expand to
%if 010
and will do exactly what you want.
Thanks from somebody else :)
"test -f /etc/redhat-release && grep 'release 5' /etc/redhat-release &&
echo 1 || echo 0"
Redundant use of "cat|grep" and egrep ...
Thanks Tony, I see what you mean. Iirc I copied it a long time ago from
another spec file. It just works so I never bothered to look at it in
more detail.

Regards,
Patrick
Tim Mooney
2007-08-30 21:11:24 UTC
Permalink
Post by Tony Earnshaw
Post by Patrick
%define is_rhel5 %(test -f /etc/redhat-release && cat /etc/redhat-release |
egrep -q 'release 5' && echo 1 || echo 0)
Thanks from somebody else :)
"test -f /etc/redhat-release && grep 'release 5' /etc/redhat-release &&
echo 1 || echo 0"
Except you probably want the -q option to grep, otherwise your macro might
end up resulting in

release 51

Getting rid of the useless cat was a good move, but I don't see any reason
to prefer grep over egrep here.

Tim
--
Tim Mooney ***@dogbert.cc.ndsu.NoDak.edu
Information Technology Services (701) 231-1076 (Voice)
Room 242-J6, IACC Building (701) 231-8541 (Fax)
North Dakota State University, Fargo, ND 58105-5164
Tony Earnshaw
2007-08-31 06:51:18 UTC
Permalink
Post by Tim Mooney
Post by Tony Earnshaw
Post by Patrick
%define is_rhel5 %(test -f /etc/redhat-release && cat
/etc/redhat-release | egrep -q 'release 5' && echo 1 || echo 0)
Thanks from somebody else :)
"test -f /etc/redhat-release && grep 'release 5' /etc/redhat-release &&
echo 1 || echo 0"
Except you probably want the -q option to grep, otherwise your macro might
end up resulting in
release 51
Might, only tried it at the CLI and it works as expected there. The '-q'
is only for "silent", equivalent to " > /dev/null 2>&1". It won't affect
output.
Post by Tim Mooney
Getting rid of the useless cat was a good move, but I don't see any reason
to prefer grep over egrep here.
People who use egrep should understand what it's good for. People who
use egrep should understand and be able to use Posix regexps. Using
egrep where grep will do is redundant.

Best,

--Tonni
--
Tony Earnshaw
Email: tonni at hetnet dot nl
Tim Mooney
2007-08-31 18:05:29 UTC
Permalink
Post by Tony Earnshaw
Post by Tim Mooney
Post by Tony Earnshaw
Post by Patrick
%define is_rhel5 %(test -f /etc/redhat-release && cat /etc/redhat-release
| egrep -q 'release 5' && echo 1 || echo 0)
Thanks from somebody else :)
"test -f /etc/redhat-release && grep 'release 5' /etc/redhat-release &&
echo 1 || echo 0"
Except you probably want the -q option to grep, otherwise your macro might
end up resulting in
release 51
Might, only tried it at the CLI and it works as expected there.
I just tried it in a spec file on RHEL5, and the problem I expected is
present. I added your define:

%define is_rhel5 %(test -f /etc/redhat-release && grep 'release 5' /etc/redhat-release && echo 1 || echo 0)

to the top of a random spec file, and then I added

echo is_rhel5='%{is_rhel5}'

into the prep, and ran

rpmbuild -bp foo.spec

I was rewarded with the output:

is_rhel5=Red Hat Enterprise Linux Client release 5 (Tikanga)
1

As I indicated, because -q wasn't used with grep, the is_rhel5 ended up
containing both the output from grep and the 1 from echo.
Post by Tony Earnshaw
The '-q' is
only for "silent", equivalent to " > /dev/null 2>&1". It won't affect output.
I agree with the first sentence (though I would have said q == quiet), but
I don't understand how you can arrive at the second sentence after the
first. -q absolutely affects the output of grep and egrep. That's it's
point.
Post by Tony Earnshaw
Post by Tim Mooney
Getting rid of the useless cat was a good move, but I don't see any reason
to prefer grep over egrep here.
People who use egrep should understand what it's good for. People who use
egrep should understand and be able to use Posix regexps. Using egrep where
grep will do is redundant.
You're right, using extended grep where grep will do is "exceeding what is
necessary", so it meets a definition of redundant.

I've found, however, that it's easier for most people to use egrep as a
superset of grep (if you don't use EREs, it behaves as grep does) than it
is for people to remember the circumstances under which they should use
basic grep vs. the circumstances under which they need to switch to egrep.

Tim
--
Tim Mooney ***@dogbert.cc.ndsu.NoDak.edu
Information Technology Services (701) 231-1076 (Voice)
Room 242-J6, IACC Building (701) 231-8541 (Fax)
North Dakota State University, Fargo, ND 58105-5164
Patrick
2007-08-30 10:26:15 UTC
Permalink
Post by Michael Jennings
On Wednesday, 29 August 2007, at 20:28:19 (+0200),
Post by Patrick
%define is_fc45 %(test -f /etc/fedora-release && cat /etc/fedora-release | egrep -q '4|5' && echo 1 || echo 0)
%define is_fc6 %(test -f /etc/fedora-release && grep -c Zod /etc/fedora-release || echo 0)
%define is_f7 %(test -f /etc/fedora-release && grep -c Moonshine /etc/fedora-release || echo 0)
%define is_rhel4 %(test -f /etc/redhat-release && cat /etc/redhat-release | egrep -q 'release 4' && echo 1 || echo 0)
%define is_rhel5 %(test -f /etc/redhat-release && cat /etc/redhat-release | egrep -q 'release 5' && echo 1 || echo 0)
%if %is_fc6 || %is_f7 || %is_rhel5
..do something
%endif
%if %{is_fc6}%{is_f7}%{is_rhel5}
will expand to
%if 010
and will do exactly what you want.
Thanks Michael. I will give it a try.

Regards,
Patrick
Patrick
2007-08-30 12:45:53 UTC
Permalink
Post by Michael Jennings
On Wednesday, 29 August 2007, at 20:28:19 (+0200),
Post by Patrick
%define is_fc45 %(test -f /etc/fedora-release && cat /etc/fedora-release | egrep -q '4|5' && echo 1 || echo 0)
%define is_fc6 %(test -f /etc/fedora-release && grep -c Zod /etc/fedora-release || echo 0)
%define is_f7 %(test -f /etc/fedora-release && grep -c Moonshine /etc/fedora-release || echo 0)
%define is_rhel4 %(test -f /etc/redhat-release && cat /etc/redhat-release | egrep -q 'release 4' && echo 1 || echo 0)
%define is_rhel5 %(test -f /etc/redhat-release && cat /etc/redhat-release | egrep -q 'release 5' && echo 1 || echo 0)
%if %is_fc6 || %is_f7 || %is_rhel5
..do something
%endif
%if %{is_fc6}%{is_f7}%{is_rhel5}
will expand to
%if 010
and will do exactly what you want.
One last question. You used %{is_fc6} while I use %is_fc6. Is your
syntax the one I should be using?

Thanks and regards,
Patrick
Michael Jennings
2007-08-30 17:05:03 UTC
Permalink
On Thursday, 30 August 2007, at 14:45:53 (+0200),
Post by Patrick
One last question. You used %{is_fc6} while I use %is_fc6. Is your
syntax the one I should be using?
It's a matter of taste, but I personally find the former more
readable. As long as there is no ambiguity on where the macro name
ends, either will work. %{...} in RPM is not unlike ${...} in shell.

Michael
--
Michael Jennings (a.k.a. KainX) http://www.kainx.org/ <***@kainx.org>
Linux Server/Cluster Admin, LBL.gov Author, Eterm (www.eterm.org)
-----------------------------------------------------------------------
"I wish you'd look at me that way, your beautiful eyes looking deep
into mine, telling me more than any words could say. But you don't
even know I'm alive. Baby, to you all I am is the invisible man."
-- 98 Degrees
Patrick
2007-08-30 17:29:05 UTC
Permalink
Post by Michael Jennings
On Thursday, 30 August 2007, at 14:45:53 (+0200),
Post by Patrick
One last question. You used %{is_fc6} while I use %is_fc6. Is your
syntax the one I should be using?
It's a matter of taste, but I personally find the former more
readable. As long as there is no ambiguity on where the macro name
ends, either will work. %{...} in RPM is not unlike ${...} in shell.
Thanks. The ending of the macro name in relation to the example you gave
totally makes sense.

Regards,
Patrick
Loading...