Unix Shell - Shortening

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2006 > Shortening





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Shortening
sil

2006-11-20, 1:18 pm

OS=$( uname|sed -n '1p')

if [ $OS = Linux ]
then
something
else
if [ $OS = FreeBSD ]
then
something
else
if [ $OS = NetBSD ]
then
something
if [ $OS = OpenBSD ]
then
something
fi
fi
fi

I know this can be shorter.. It's a brainfarting Monday

if [ $OS = *BSD ] or so

Radoulov, Dimitre

2006-11-20, 1:18 pm


"sil" <dsphunxion@gmail.com> wrote in message
news:1164039564.144862.140350@j44g2000cwa.googlegroups.com...
> OS=$( uname|sed -n '1p')
>
> if [ $OS = Linux ]
> then
> something
> else
> if [ $OS = FreeBSD ]
> then
> something
> else
> if [ $OS = NetBSD ]
> then
> something
> if [ $OS = OpenBSD ]
> then
> something
> fi
> fi
> fi
>
> I know this can be shorter.. It's a brainfarting Monday
>
> if [ $OS = *BSD ] or so


Use case:

case "$OS" in
*BSD) ...


Regards
Dimitre


Chris F.A. Johnson

2006-11-20, 1:18 pm

On 2006-11-20, sil wrote:
> OS=$( uname|sed -n '1p')
>
> if [ $OS = Linux ]
> then
> something
> else
> if [ $OS = FreeBSD ]
> then
> something
> else
> if [ $OS = NetBSD ]
> then
> something
> if [ $OS = OpenBSD ]
> then
> something
> fi
> fi
> fi
>
> I know this can be shorter.. It's a brainfarting Monday
>
> if [ $OS = *BSD ] or so


case `uname` in
Linux*) # do something Linuxy ;;
FreeBSD*) # do something Freeby ;;
NetBSD*) # do something Netty ;;
OpenBSD*) # do something Openly ;;
*) # do something else ;;
esac

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
bsh

2006-11-22, 1:39 am

sil wrote:
> I know this can be shorter.. It's a brainfarting Monday


The shortest it can be made is to use someone else's code.

"config.guess.sh; config.sub.sh"
http://cvs.savannah.gnu.org/viewcvs...{guess,sub}

"archit.sh"
http://www.cs.indiana.edu/~kinzler/home/binp/archit

"opsys.sh"
http://www.cs.indiana.edu/~kinzler/home/binp/opsys

"platform.sh"
in GNU shtools

"whatami.sh"
component of http://www-unix.mcs.anl.gov/systems/software/msys/

"archguess.sh"
http://sourceforge.net/projects/archguess/

and, of limited applicability,

"shtype"
http://shelldorado.com/scripts/cmds/shtype.txt

and my own "whichshell"
http://groups.google.com/group/comp...1513a857080ed64

P.S. You may wish to investigate the "canonical" solution: the
autoconf app which "automatically configures shell scripts ... to
adapt ... to many kinds of UNIX-like systems without manual
user intervention."

http://www.gnu.org/software/autoconf/

=Brian

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com