Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

When developing a shell script that should run on various unix/linux derivates, i sometimes have the problem that some tools have to be called in a different way compared to other systems. For example if the arguments are different. I wonder what's the best way to solve this.

Should i use uname to check for the operating system name and rely on this to execute the tool in different ways or are there any "better" ways, some kind of "ability" check for shell commands and tools?

The systems in question are for example Linux, Mac OS X, Solaris and Irix -- all quite different when it comes to abilities of tools and shell commands.

share|improve this question

4 Answers 4

up vote 1 down vote accepted

Ahh, shell script portability, fun!

Yeah, uname and checking $SHELL are the easiest way (and only portable way I can think of) to do this.

share|improve this answer
    
ok, thanks ... i just was not sure, if there are more "nifty" solutions for this :(. –  harald Feb 7 '13 at 13:16

the easiest way to see what distro you're on is to check /etc/issue

~$ cat /etc/issue
Debian GNU/Linux 6.0 \n \l

~$ cat /etc/issue
CentOS release 5.8 (Final)
Kernel \r on an \m

~$ cat /etc/issue
Ubuntu 12.04.2 LTS \n \l
share|improve this answer
    
As i could remember /etc/issue is an LSB standard, it will work with some linux disto only, not with other *nix –  DukeLion Feb 10 '13 at 8:49

You could use the facter tool which is part of Puppet (but can be used independently, if you don't have Puppet in your environment) to get basic facts about the target system.

For instance:

$ facter operatingsystem
CentOS

$ facter kernel kernelversion osfamily
kernel => Linux
kernelversion => 2.6.32
osfamily => RedHat

gem install facter should be sufficient to install it on any system with Ruby available, if it's not in your system packages already.

share|improve this answer

You are much better off writing portable code. Almost anything is achievable using plain POSIX, and then you won't need to worry about which utilities take which options.

Shell functions are always helpful when something useful isn't available, for example ls -A:

ls-A()
{
    ls -f ${1+"$@"} | sort | sed '1,2d'
}

Of course, this is very contrived, but much better than maintaining multiple sets of code for each operating system, and also supports OSes that you hadn't thought of;

case $OS in
GNU)
    # some bashisms
    ;;
FreeBSD)
    # some shisms
    ;;
*)
    # Oops, don't know what to do!
    ;;
esac
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.