Hoping some one can point out what I am doing wrong. After looking through several threads, plus lots of experimentation, it feels like something simple is wrong. The threads I read include How can I trigger a Notification Center notification from an AppleScript or shell script? and How to get a notification when my commands are done
This script is intended for minimally technical users to perform mounting and un-mounting shares. Right now it only supports SMB, but will handle NFS and other remote mounts later. We have the constraint that the user's system will be the base OS X installation, therefore we cannot install other tools or libraries.
The problem is popping a notification window when an event occurs, with text that includes a space in the argument passed to the function. If the argument is only alpha/numeric, it works fine.
As a starting point, I am using a piece of code from randomor on How to get a notification when my commands are done. The say command makes it much easier to tell when the script works, plus will make for nice voice prompts.
send_notify(){
cmd=$@
local $@ && say 'Completed!' && osascript -e "display notification \"Completed: $2\" with title \"$1\""
}
As long as there isn't whitespace in the argument string being passed from the calling function, both the say and the notification work. When there is white space in the argument string such as "BAR 3" or in a variable being passed, the send_notify() function throws an error.
When called without whitespace, send_notify() works
send_notify "FOO" "BAR2" <<< This works
This fails because of the whitespace
send_notify "FOO" "BAR 3" <<< This fails
./sharemount.sh: line 138: local: `37': not a valid identifier
Interestingly, the say command also fails to execute.
Part of the problem is my lack of understanding what role “$@" plays in this command. If anyone has an explanation, it would be very welcome.
Below is one of several functions in the script tha call the send_notify() function.
Pre_Mount_Check() {
Current_Share="$(df -t smbfs --output=source 2>/dev/null | sed '1d' | awk -F/ '{print tolower($NF)}')"
Current_MountPoint="$(df -t smbfs --output=target 2>/dev/null | sed '1d')"
logger -p Info "$DebugLog" "Sharemount:Pre_Mount_Check Checking to see if $Share_Path already exists"
if [[ (-z "$Current_Share") && (-z "$Current_MountPoint") ]]; then
logger -p Info "$DebugLog" "Sharemount:Pre_Mount_Check $Share_Path on $Mount_Point is not mounted, we will try to mount it"
return 0
elif [[ ( "$Current_Share" = "$Share_Name") && ( "$Current_MountPoint" = *"$Mount_Point"*) ]]; then
logger -p Info "$DebugLog" "Sharemount:Pre_Mount_Check $Share_Path already mounted, nothing to do, exiting"
echo "$Share_Path already mounted, nothing to do, exiting 1"
exit 0
elif
[[ ( "$Current_Share" = "$Share_Name") ]]; then
logger -p Warning "$DebugLog" "Sharemount:Pre_Mount_Check $Share_Path already mounted, on $Current_MountPoint, exiting"
echo "$Share_Path already mounted, on $Current_MountPoint, exiting 2"
send_notify "FOO" "BAR2" <<< This works
exit 36
elif [[ ( "$Current_MountPoint" = *"$Mount_Point"*) ]]; then
logger -p Warning "$DebugLog" "Sharemount:Pre_Mount_Check $Share_Name is already mounted on $Mount_Point exiting"
echo "$Share_Name is already mounted on $Mount_Point exiting 3"
send_notify "FOO" "BAR 3" <<< This fails
exit 0
fi
}
send_notify(){
cmd=$@
local $@ && say 'Completed!' && osascript -e "display notification \"Completed: $2\" with title \"$1\""
}
$@
expands to the positional parameters. One way to visualize this is if you wanted all the elements in an array,$@
would provide them. That said, I don't see what positional parameters are being set based on the code provided. One way to check is to simplyecho $cmd
and see what you get (I have a feeling, nothing). That said, there seems to be multiple questions here - can you narrow it down to just one (i.e. the notify problem as described in your title)?