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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With