Script Parameter Variable while running command as currently logged in user

karthikeyan_mac
Valued Contributor

Hi,

We are running into issue with Script Parameters while adding it with the script. It works fine if we call that in the script, but when I run any command as currently logged in user the parameter variable $4 does not work.

For Example
I am specifying the string "TEST" in Parameter 4 Not Labeled while adding the script. When I call $4 in the script directly it works, but I need to run a command as currently logged in user. When I echo $4 as currently logged in user, its blank.

My Script is to send an email based on the Script Parameter specified at Paramater 4($4).

#!/bin/sh
echo $4 # this echoes "TEST"
LOGGEDUSERS=`who | awk '/console/ {print $1}'`
su -l $LOGGEDUSERS -c 'echo "$4 Contents" | mail -s "Success" admin@test.com'   # this triggers the mail but $4 value is blank in email

Is there any way to call the script parameters while running the command as logged in user?

Regards,
Karthikeyan

1 ACCEPTED SOLUTION

mikedodge04
New Contributor II

I'm not sure why you would be using this, more context would be helpful. But the reason that $4 is not getting passed is because you had it wrapped in single quotes ''. Variables don't get evaluated within single quotes. If you see below I close the single quote, wrap the variable with double quotes, used the variable, and then restarted the single quote with no spaces.

#!/bin/sh
echo $4 # this echoes "TEST"
LOGGEDUSERS=`who | awk '/console/ {print $1}'`
su -l $LOGGEDUSERS -c 'echo "'"$4"' Contents" | mail -s "Success" admin@test.com'    # this triggers the mail but $4 value is blank in email

View solution in original post

3 REPLIES 3

mm2270
Legendary Contributor III

Without testing it out I can't be sure, but you may need to use the launchctl bsexec trick here. I've had to use some code posted by others here on JN on several occasions with scripts I've written that needed to do something as the user. It seems as we moved toward Lion and Mountain Lion this need increased, since Apple added a lot more sandboxing to the OS, preventing processes from running commands as another user without going through some hoops.

Totally untested, so may be off, but just a quick idea below-

#!/bin/sh
echo $4 # this echoes "TEST"
LOGGEDUSERS=`who | awk '/console/ {print $1}'`
LOGGEDPID=`ps -axj | awk "/^$LOGGEDUSERS/ && /Dock.app/ {print $2;exit}"`

/bin/launchctl bsexec "${LOGGEDPID}" sudo -iu "${LOGGEDUSERS}" "echo "$4 Contents" | mail -s "Success" admin@test.com"

I'll be curious to hear if something like that helps in this case.

Slight Edit- escaped the double quotes within the command.

mikedodge04
New Contributor II

I'm not sure why you would be using this, more context would be helpful. But the reason that $4 is not getting passed is because you had it wrapped in single quotes ''. Variables don't get evaluated within single quotes. If you see below I close the single quote, wrap the variable with double quotes, used the variable, and then restarted the single quote with no spaces.

#!/bin/sh
echo $4 # this echoes "TEST"
LOGGEDUSERS=`who | awk '/console/ {print $1}'`
su -l $LOGGEDUSERS -c 'echo "'"$4"' Contents" | mail -s "Success" admin@test.com'    # this triggers the mail but $4 value is blank in email

karthikeyan_mac
Valued Contributor

Thanks Mike. Adding the quote to variable specified you works fine.