Hola, estoy tratando de escribir un script para verificar la versión del software. Quiero pasar mis propias variables, que son el nombre de la aplicación y la versión.
Aquí hay una copia. Actualmente no me funciona... soy un novato y estoy bastante seguro de que tiene que ver con la sintaxis.
Cualquier entrada es apreciada.
gracias, pablo
#/bin/sh
#Enter the Name of the Application here
ApplicationName=/Applications/FakeApp.app
ApplicationVersionNumber=1.0
echo $ApplicationName
#Check if Directory Exist
if [ ! -d $ApplicationName ]; then
echo $ApplicationName "is not installed"
exit 123456
fi
echo $ApplicationName " is installed"
# Check Version
VersionCheck=`cat $ApplicationName/version.plist | grep $ApplicationVersionNumber`
echo $VersionCheck
if [ ${#VersionCheck} != 0 ]; then
echo $VersionCheck
echo $ApplicationName $ApplicationVersion Number "is Installed"
exit 0
fi
echo $ApplicationName $ApplicationVersion Number "is NOT Installed"
exit 1
En realidad, solo hay un error de sintaxis que cometiste. Pones un espacio antes del Número en $ApplicationVersionNumber
. La mayoría de las aplicaciones no tienen un archivo version.plist, pero siempre tienen la versión en su Info.plist.
Aquí hay una versión corregida del script con algunas mejoras:
#!/bin/sh
ApplicationName=/Applications/FakeApp.app
ApplicationVersionNumber=1.0
echo $ApplicationName
#Check if Directory Exist
if [ ! -d $ApplicationName ]; then
echo "$ApplicationName is not installed"
exit 123456
fi
echo "$ApplicationName is installed"
# Check Version
VersionCheck=`plutil -p "${ApplicationName}/Contents/Info.plist" | grep "CFBundleShortVersionString.*$ApplicationVersionNumber"`
echo $VersionCheck
if [ ${#VersionCheck} != 0 ]; then
echo "$ApplicationName $ApplicationVersionNumber is Installed"
exit 0
fi
echo "$ApplicationName $ApplicationVersionNumber is NOT Installed"
exit 1
plutil -p
en lugar de cat
, ya que plutil puede imprimir una versión agradable y legible del plist incluso si no está en formato XML..*
y luego el valor. Esto es mejor porque no quieres que se active en cosas como LSMinimumSystemVersion
.yo lo hubiera escrito asi:
#!/bin/bash
app_path="$1"
desired_version="$2"
#Get the line, regardless of whether it is correct
version_line=$(plutil -p "${app_path}/Contents/Info.plist" | grep "CFBundleShortVersionString")
if [[ $? -ne 0 ]]; then
version_line=$(plutil -p "${app_path}/Contents/Info.plist" | grep "CFBundleVersion")
if [[ $? -ne 0 ]]; then #if it failed again, the app is not installed at that path
echo "$app_path not installed at all"
exit 123456
fi
fi
#Some text editing to get the real version number
real_version=$(echo $version_line | grep -o '"[[:digit:].]*"' | sed 's/"//g')
if [ "$real_version" = "$desired_version" ]; then
echo "$app_path $desired_version is installed"
exit 0
fi
echo "${app_path}'s version is $real_version, not $desired_version"
exit 1
La ventaja de esto es que verifica la cadena de versión real, por lo que si coloca 1.3 y es 1.3.1, informa que es una versión diferente. $1
y $2
son los argumentos de la línea de comandos que se pasan como./script.sh '/Applications/FakeApp.app' '1.3'
Además, el otro contaría como correcto en 1.1 para 121 porque grep cuenta .
como un comodín.
También puede usar AppleScript o defaults
:
$ osascript -e 'version of app "TextEdit"'
1.9
$ defaults read /Applications/TextEdit.app/Contents/Info.plist CFBundleShortVersionString
1.9
Puede usar AppleScript o mdfind
para encontrar la ruta de una aplicación:
$ osascript -e 'posix path of (path to app "textedit")'
/Applications/TextEdit.app/
$ mdfind 'kMDItemContentType=com.apple.application-bundle&&kMDItemFSName=TextEdit.app'
/Applications/TextEdit.app
Pablo Mung
TJ Luoma
0942v8653
TJ Luoma
0942v8653