Hi guys,
I am pretty new with scripting and really don't have an idea how to solve my problem. And as I learned from JNUC '13 - never script alone :) Hope you can help!
Unfortunately I have some Apps running with the wrong permissions, meaning kids can write into the package content to run other apps like Terminal or so. Now I wrote a script to see if the application is existent, if yes the script should check if the process is running, if yes kill process, mv the corrupt app to Utilities Folder (totally blocked for my students) and re-install a fixed version via policy trigger. So far everything works fine if I declare my variables in the script, but I would like to use it a little bit advanced by using the JAMF script parameters, so I could use the one script for similar problems.
May you guys could check my code and let me know what I am doing wrong. The Script always stops working before it mv the app, equal if App is running or not :(
Thanks in advance!
Fabian
#!/bin/bash
## Bavarian International School e.V. - Fabian Ulmrich
## Check for specified Application Folder - if exits move Folder
#####################################################################
# HARDCODED VALUES ARE SET HERE
#specified AppFolder
appdir=""
#Process to look for
process=""
#specified Policy Trigger
trigger=""
## Variables
if [ "$4" != "" ] && [ "$appdir" == "" ]; then
appdir=$4
fi
if [ "$5" != "" ] && [ "$process" == "" ]; then
process=$5
fi
if [ "$6" != "" ] && [ "$trigger" == "" ]; then
trigger=$6
fi
apputil="/Applications/Utilities/"
logfile="/private/var/log/AppMove.log"
logdate=`date "+Log: %d.%m.%Y - %H:%M:%S"`
jamf="/usr/sbin/jamf"
## Creating Log
/usr/bin/touch "/private/var/log/AppMove.log" >> "${logfile}"
/bin/echo "${logdate}" >> "${logfile}"
/bin/echo "Logfile created..." >> "${logfile}"
## Check if values are set for Script in Policy Script Details
if [ "$appdir" == "" ]; then
/bin/echo "Error: No appdir value is specified."
exit 1
fi
if [ "$process" == "" ]; then
/bin/echo "Error: No process value is specified."
exit 1
fi
if [ "$trigger" == "" ]; then
/bin/echo "Error: No trigger value is specified."
exit 1
fi
## Script
if [ -d "${appdir}" ]; then
/bin/echo "Check if specific App is running" >> "${logfile}"
ps aux | grep "${process}" | grep -v grep > /dev/null
if [ $? -eq 0 ]; then
/bin/echo "Stopping process: $process" >> "${logfile}"
/bin/ps aux | grep "${process}" | grep -v grep | awk '{print$2}' | xargs kill -9
else
/bin/echo "No process ${process} running - continue script" >> "${logfile}"
fi
sleep 5
/bin/echo "Moving Folder to /Applications/Utilities" >> "${logfile}"
/bin/mv "${appdir}" "${apputil}"
/bin/echo "Installing fixed version of corrupt application via JAMF policy" >> "${logfile}"
"${jamf}" policy -trigger "${trigger}" >> "${logfile}"
else
/bin/echo "$appdir is not an installed Application on this Computer" >> "${logfile}"
fi
exit 0