Re: combining the result of processing various commands and setting it in a variable

From: Janis Papanagnou (Janis_Papanagnou_at_hotmail.com)
Date: 11/19/05


Date: Sat, 19 Nov 2005 15:54:27 +0100

onetitfemme wrote:
> All right.
>
> of the 4 examples I got from you only the first one proposed by Lars,
> didn't work.
>
> // __ grep: version: No such file or directory
> _VER="Linux version";
> _CHKVER="`dmesg | grep -i ${_VER}`";
> echo ${_CHKVER};
>
> // __ OK!
> _VER="Linux version";
> _CHKVER=$(dmesg | grep -i "${_VER}");
> echo ${_CHKVER};
>
> // __ OK!
> _VER="Linux version";
> _CHCKVER=`dmesg | grep -i "$_VER"`;
> echo ${_CHKVER};
>
> // __ OK!
> _VER="Linux version";
> _CHCKVER=$(dmesg | grep -iF -- "$_VER");
> echo ${_CHKVER};
>
>
> Obviously I need to read more about "grep" and command shell scripting
>
> Now, which of the working versions of the script is the most portable
> one?

As far as I can tell they work...
#1 - n.a.
#2 - in all but old bourne shells and with all grep's.
#3 - in all including old bourne shells and with all grep's.
#4 - in all but old bourne shells and only with newer grep's.
(read "all shells" as all bourne like shells.)

> The last one as given by Stéphane?

If your Unix systems all support the 'grep --' then go for #4.

If you can guarantee that the version string can never contain a
leading dash then you don't need the '--' of newer grep versions.

On older Unix systems there was a fgrep (instead of 'grep -F') available.

> I need for the script to hopefully work on any Unix and Linux
> versions. Right now I am testing it with Linux/Debian/Knoppix

The question rather is, will you find dmesg on "all systems"?!

Janis

> Thanks
> otf
>