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

From: Stephane CHAZELAS (this.address_at_is.invalid)
Date: 11/19/05


Date: Sat, 19 Nov 2005 14:45:03 +0000

2005-11-19, 05:08(-08), onetitfemme:
> _VER="Linux version";
[...]
> _CHKVER=$(dmesg | grep -i "${_VER}");
[...]
> _CHCKVER=`dmesg | grep -i "$_VER"`;
[...]
> _CHCKVER=$(dmesg | grep -iF -- "$_VER");
> echo ${_CHKVER};
[...]
> Now, which of the working versions of the script is the most portable
> one?
>
> The last one as given by Stéphane?
>
> I need for the script to hopefully work on any Unix and Linux
> versions. Right now I am testing it with Linux/Debian/Knoppix
[...]

For that particular value of $_VER all the 3 ones are
equivalent. For any value of $_VER, only the 3rd one is correct.

All those are POSIX conformant. Only the 2nd will work with the
Bourne shell (which is an old shell you sometimes find on very
old systems or on Solaris as /bin/sh). You may find some old
greps that don't recognise -F.

As long as $_VER doesn't contain any ., *, [, ^, or $
characters and don't start with a "-", you can stick with the
second solution.

Otherwise, instead of:

_VER='- $Version: 1.2$'

for instance, you'll have to write it:

_VER='[-] \$Version: 1\.2\$'
_CHCKVER=`dmesg | grep -i "$_VER"`;

You're more likely to have compatibility issues with dmesg, than
with the syntax of the 3rd line, though.

If you want the OS version, note: uname -rs

-- 
Stéphane


Relevant Pages

  • Re: Bourne Shell Port Available
    ... >> I guess I'm happy for you, but I spend most of my time on Solaris ... >> boxes trying to avoid the Bourne shell. ... shell script that couldn't be run by ksh anyway. ... > obligation to the POSIX standard requires them to fix non-conforming ...
    (comp.unix.shell)
  • exit call in script causes unit test to not run (newby)
    ... I currently have a ruby script which I'm trying to create some ... If I comment out the exit then my tests actually run. ... something I commonly do in bourne shell scripts and it made sense to ... # Set PROTO constant based on command line arg ...
    (comp.lang.ruby)
  • Re: exec script in all subdirs; pathnames contain spaces
    ... unset CDPATH ... that was not a Bourne shell script. ... Inline is a "dummy" parameter. ...
    (comp.unix.shell)
  • Re: Bourne shell script affecting current shell with arguments
    ... >We've got a script that is run by users running the Bourne shell to set ... >arguments to this script for the variables to be set. ... Have the script define a function that does the real work. ...
    (comp.unix.shell)
  • Re: Help with very basic bourne shell
    ... simple script does not work in bourne shell. ... There is no let command in the Bourne shell; ... This script will be called by a cron job so I don't want any output. ...
    (comp.unix.shell)