In the last few blog entries we have been focusing quite a bit on displaying information from a Unix system via a BASH script. One question that’s come up by quite a few people is, do these commands work on all Unix system? That’s a very valid question.
It turns out that one of my favorite sayings about Unix is that “Unix systems are always the same, they’re just different.” In other words – most Unix flavors share all sorts of similarities. The problem is that there are often very subtle differences between flavors of systems that make it difficult to write one script that will work on all systems. Basically when writing an audit script you have to decide, do you write one script for all flavors and sacrifice on which commands you run to make it consistent, write multiple scripts (one per flavor of Unix), or do you try to do OS detection within your script.
If you decide to do OS detection, I know there are multiple ways to do it. But here is some basic code that we have used in the past in a BASH script to detect which operating system or flavor of Unix is running on a system:
if [ -f /etc/debian_version ]; then OS="Debian" VER=$(cat /etc/debian_version) elif [ -f /etc/redhat-release ]; then OS="Red Hat" VER=$(cat /etc/redhat-release) elif [ -f /etc/SuSE-release ]; then OS="SuSE" VER=$(cat /etc/SuSE-release) else OS=$(uname -s) VER=$(uname -r) fi echo "Operating System Name: $OS" echo "Operating System Version: $VER"
Like I said, this likely won’t work on all flavors, you’ll need to test it out on your favorite to make sure it works. In fact if you have suggestions to improve it, please submit them to firstname.lastname@example.org and we’d be happy to update ours too. Happy scripting!