[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: How to match a substring?



On Wed, Jan 14, 2004 at 11:59:12AM -0600, Kent West wrote:
> I need to run a test in a .bashrc startup script to see whether the 
> machine the user is logging onto is a Solaris or a Linux box (the /home 
> directory is shared between the two, and paths need to be modified 
> according to which OS is being logged into).
> 
> Something like this:
> 
> if {the first word of "uname -a" is Linux}

Why not just use 'uname', which prints Linux on Linux and SunOS on
Solaris?

In general, try either:

  if [ "`uname -a | sed -e 's/ .*//'`" = Linux ]

(fairly straightforward) or:

  UNAME_A="`uname -a`"
  if [ "${UNAME_A%% *}" = Linux ]

(match trailing portion of variable against shell pattern ' *').

> My problem is that I don't know how to match for the first word of 
> "uname -a". I've tried things like:
> 
> if [ `uname -a`:0:5} = "Linux" ]

That doesn't work because I don't believe there's a syntax anything like
that in shell.

> if {awk '{ print $1 }' < `uname -a` = "Linux" ]

That doesn't work because < redirects from a file, not from the output
of a command. This would work:

  if [ "`uname -a` | awk '{ print $1 }'" = Linux ]

Some would think that's clearer than my sed example above, too. :)

Note that I haven't quoted "Linux" in any of my examples, since it
doesn't contain any metacharacters, but there's no harm in doing so.

Cheers,

-- 
Colin Watson                                  [cjwatson@flatline.org.uk]



Reply to: