script : output safari version to a variable

yarin
New Contributor II

Hey guys.
I'm trying to create a script that will check the version of safari.
then If its lower the 10.0 then do the rest of the script.
I'm trying to save to a variable by using "default read" command result.
but the output is blank ( echo is blank and the "if" doesnt work) the code Im using :

#!/bin/sh
`Version= defaults read /Applications/Microsoft Word.app/Contents/Info CFBundleShortVersionString
echo $Version
4 REPLIES 4

mm2270
Legendary Contributor III

First thing is, your syntax is wrong. You don't put the backtick in the front of the variable name. It goes at the start and end of the command that populates the variable.

Version=`defaults read /Applications/Microsoft Word.app/Contents/Info CFBundleShortVersionString`

However, I suggest using the newer $(some command) format for setting up variables, as they tend to be easier to read (less able to confuse the backtick with a quote for example) But it's a matter of preference really.

Version=$(defaults read /Applications/Microsoft Word.app/Contents/Info CFBundleShortVersionString)

Second thing is, I'm confused. You mention wanting to capture the Safari version, but in the above, you're capturing the version for MS Word?

Just a word of caution or a heads up. Most application version strings, including Safari and Word, won't give you a simple integer that you can easily run a greater than/less than comparison on. You usually have to manipulate the string to give you something you can do that comparison on, unless you use python, which can do it more naturally.

millersc
Valued Contributor

@yarin can you post the script your trying to work with. This does output correctly for me

defaults read /Applications/Safari.app/Contents/Info CFBundleShortVersionString

yarin
New Contributor II
including Safari and Word, won't give you a simple integer that you can easily run a greater than/less than comparison on

I must get the safari version so the script wont run on other computers with newer versions.
is there another way to do it?

@millersc @yarin can you post the script your trying to work with. This does output correctly for me

Yes but it doesn't matter, I'm guessing the output you get is what you see on the terminal when you write it down as a command.
But like what @mm2270 wrote, it wont give you a simple integer output.
To be sure : try to save your output to a variable and echo the variable to see if it works.

millersc
Valued Contributor

I'm still trying to understand what your end result is.

You can use an EA with the following to get just the primary version number:

defaults read /Applications/Safari.app/Contents/Info CFBundleShortVersionString | sed 's/[.]/ /g' | awk '{print $1}'

So your EA could look like this:

#!/bin/sh

if [ -f "/Applications/Safari.app/Contents/Info.plist" ] ; then
    VERSION=$( defaults read /Applications/Safari.app/Contents/Info CFBundleShortVersionString | sed 's/[.]/ /g' | awk '{print $1}' )
else
    VERSION="Not installed"
fi

echo "<result>$VERSION</result>"

With that EA information, you can use a Smart Group and scope with the "is not" Operator. The criteria would be your EA information. Your value would be "10" in this specific instance. This would give you anything that is not 10.x variation.

If you remove the sed and awk command and just use the actual version (10.1 is current) and do the same "is not" operator. This would give you anything that is not 10.1 specifically.