Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check version of OS then issue a command if the correct version

i am writing a bash script for Mac OS X Lion 10.7 and i would like to know how i can check the version of the OS in bash and if the version is lets say 10.7.1 then it does a command and continues with the script and do the same thing for a different version lets say 10.7.3 then it does a different command then the command that used for 10.7.1?

like image 740
Bryan Larson Avatar asked Mar 28 '12 18:03

Bryan Larson


2 Answers

You want the sw_vers command on OS X. It prints some human-readable strings, including the 10.X.X system version (sw_vers -productVersion). You can also use uname to check the kernel version; if your script is ever ported to other Unix variants uname will work there.

like image 116
zmccord Avatar answered Sep 28 '22 11:09

zmccord


OS_Version (full… example 10.7.3)

system_profiler SPSoftwareDataType | grep "System Version" | awk '{print $6}'

OR

sw_vers -productVersion

OS (short… example 10.7)

system_profiler SPSoftwareDataType | grep "System Version" | awk '{print $6}' | sed "s:.[[:digit:]]*.$::g"

OR

OS_Version=$(OS (short… example 10.7) | sed "s:.[[:digit:]]*)

bash:

#!/bin/bash

# Use one of the examples given above to create the OS_Version variable

if [[ ${OS_Version} == 10.7.3 ]]; then
   echo "Operating System is a match... will continue on."
else
   echo "Operating System is NOT a match... will NOT continue."
fi
like image 45
E1Suave Avatar answered Sep 28 '22 09:09

E1Suave