开发者

Use a different command variation on Solaris

Since Solaris grep by default doesn't have the -E option, I would have to update my bash to work with a specific grep. This is what I do.

It works on the command line, but when I put it in the bash file, it looks like the script doesn't pick it up and still uses the normal grep. (I do not want to change the whole $PATH.)

Please开发者_JAVA百科 advise:

export isSolaris=`uname -a | grep -i "sunos"`
if  [ -n "$isSolaris" ]; then
        alias grep="/usr/xpg4/bin/grep -E";
fi


bash and ksh don't process aliases defined in a file until after the file is read... which means you can't define it in the same script that will use it. You can put it in another file and . (source) that into your script, though.

Alternately, use a shell function.

mygrep() {
  if test -n "$isSolaris"; then
    /usr/xpg4/bin/grep -E ${1+"$@"}
  else
    grep ${1+"$@"}
  fi
}


Consider using egrep instead; it probably works everywhere, even though POSIX/SUS doesn't list it as a command any more. (SUS v2 from 1997 listed egrep as a 'legacy' utility; POSIX 1003.1:2004 omitted egrep).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜