Execution Problems with ASU Shell Script

Hello. I have been trying to create a shell script that checks to see if there are software updates and if not, then exit the script. If so, then check to see if a user is logged in. If a user is logged in, it will only install the updates. If a user is not logged in, then it will display a message for the user not to login until that pop-up is gone, then it will install all software updates and reboot the machine.

The script is executed via a Cron Job. It seems to not execute after the variables are declared. Here is the code I have. Any help in this matter is greatly appreciated! :slight_smile:

#!/bin/sh

set applesw="/usr/sbin/softwareupdate -l | grep 'Software Update' | awk '{print $5}'"

set userlogin="who | grep 'console' | awk '{print $2}'"

if [ $applesw != "following" ] then
exit 0
else

if [ $userlogin = "console" ] then
/usr/sbin/softwareupdate -i -a
exit 0
else
/usr/bin/osascript -e 'tell app "System Events" to display dialog "Software Updates are currently running. Please do not login to this machine until this message is gone. Thank you."'
/usr/sbin/softwareupdate -i -a
/sbin/shutdown -r now
fi
fi

It probably won't make a huge difference, but you can skip the "set" command, I believe.

varname=`blah-blah...`

is a valid variable declaration.

Typically, I will have a development machine that I test on, and when confronted by issues running a specific script, I use the system log to determine why a script is failing.

The "logger" command can be used effectively:
logger "Software update status: $applesw"
...

loggedin=`who | grep 'console' | awk '{print $1}'`
logger "Users logged in: $loggedin"

Actually, you appear to be using command line substitution in setting up your variables, but you are using double quotes instead of the backtick `

Thank you very much for your input. I have taken what you have suggested and implemented it. When Crontab runs and executes my script, this is what it logs:

Mar 15 21:57:33 ecsxloan1 root: Software update status: '\nfollowing'
Mar 15 21:57:33 ecsxloan1 root: Users logged in: ''

Also, when I sent the scripts as a Unix command thru Apple Remote Desktop, this is what I received:

/Library/Management/initswupdater2.sh: line 16: syntax error near unexpected token `else'
/Library/Management/initswupdater2.sh: line 16: `else'

It seems to get hung-up at this and then not perform the rest of the script.

Any suggestions?

This is how my code appears now:

#!/bin/sh
applesw=`/usr/sbin/softwareupdate -l | grep 'Software Update' | awk '{print $5}'`
logger "Software update status: '$applesw'"

loggedin=`who | grep 'console' | awk '{print $1}'`
logger "Users logged in: '$loggedin'"

if [ $applesw != "following" ] then
exit 0
else
if [ $loggedin = "console" ] then
softwareupdate -i -a
exit 0
else
/usr/bin/osascript -e 'tell app "System Events" to display dialog "Software Updates are currently running. Please do not login to this machine until this message is gone. Thank you."'
softwareupdate -i -a
shutdown -r now
fi
fi