Multiplatform SHELL script

scripting

During scripting in environment, where is used more UNIX/Linux platform we must adapt our scripts for slight differences between those platforms. Here is little example how we can do it and few examples how is script working at different platforms.

#!/bin/sh

TYPE=`uname`;
echo 'this is ' ${TYPE};

if [ ${TYPE} = HP-UX ]
   then bdf /var;
elif [ ${TYPE} = Linux ]
   then df -h /var;
elif [ ${TYPE} = FreeBSD ]
   then df -k /var;
else echo "Unsupported OS - ${TYPE}"
fi

As we can see our script can recognize 3 different platform, Linux, HP-UX and FreeBSD.

Output Linux:

# ./multiplatform
this is  Linux
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/vg00-lv_var 1008M  196M  762M  21% /var
#

Output HP-UX:

#./multiplatform
this is  HP-UX
Filesystem          kbytes    used   availĀ %used Mounted on
/dev/vg00/lvol8    2097152  792232 1301568   38% /var
#

Output FreeBSD:

$ ./multiplatform
this is  FreeBSD
Filesystem       1024-blocks     Used    Avail Capacity  Mounted on
/www/server0/var   114765308 53877848 51706236    51%    [restricted]
$

Leave a Reply