Missing variables in script

b3nn
New Contributor II

i have a script that creates a few scripts on the users computers.and one of the scripts seems to pull the variable out of the script. any idea how to fix this?

This is what i write:

#Creates the script that the Launchd will call to make the popup
echo "#!/bin/sh
SUSNOTE= softwareupdate -l | grep '*' -c
terminal-notifier -message 'There are several updates waiting for you in Self Service Click her to launch Self Service' -title ' $SUSNOTE Pending Updates' -activate com.jamfsoftware.selfservice -appIcon /.pVault/Spark.png " > /var/uits/UpdateWarningPopup.sh

but once the file is created on the users computer, the script looks like this:

#!/bin/sh
SUSNOTE= softwareupdate -l | grep '*' -c
terminal-notifier -message 'There are several updates waiting for you in Self Service Click her to launch Self Service' -title '  Pending Updates' -activate com.jamfsoftware.selfservice -appIcon /.pVault/Spark.png

I'm assuming that casper is trying to use the variable in some way.

Thoughts?

1 ACCEPTED SOLUTION

Josh_S
Contributor III

Ok, there are a lot of ways to skin this cat. Just wanted to make one note, and let you know about a test I did and the output to see if it helps you.

= the assignment operator (no space before and after)

http://www.tldp.org/LDP/abs/html/varassignment.html

Entire script, directly copied and pasted from my test JSS:

#!/bin/bash

SUSNOTE="$(softwareupdate -l | grep '*' -c)"

mkdir /var/uits

#Creates the script that the Launchd will call to make the popup
echo "#!/bin/sh
terminal-notifier -message 'There are several updates waiting for you in Self Service Click her to launch Self Service' -title '$SUSNOTE Pending Updates' -activate com.jamfsoftware.selfservice -appIcon /.pVault/Spark.png " > /var/uits/UpdateWarningPopup.sh

chmod +x /var/uits/UpdateWarningPopup.sh

Resulting file on test computer, with 6 updates available:
/var/uits/UpdateWarningPopup.sh

#!/bin/sh
terminal-notifier -message 'There are several updates waiting for you in Self Service Click her to launch Self Service' -title '6 Pending Updates' -activate com.jamfsoftware.selfservice -appIcon /.pVault/Spark.png

View solution in original post

13 REPLIES 13

Josh_S
Contributor III

You've wrapped the entire echo statement in double-quotes, rather than single quotes, so the variable $SUSNOTE is being evaluated. Since it's null, it gets stripped. If you escape the $ character, it won't be evaluated.

Another issue is how you're defining SUSNOTE. I assume you would like to set it to the output of the shell command 'softwareupdate -l | grep '*' -c'.

Give this a shot:

#Creates the script that the Launchd will call to make the popup
echo "#!/bin/sh
SUSNOTE="$(softwareupdate -l | grep '*' -c)"
terminal-notifier -message 'There are several updates waiting for you in Self Service Click her to launch Self Service' -title "$SUSNOTE Pending Updates" -activate com.jamfsoftware.selfservice -appIcon /.pVault/Spark.png " > /var/uits/UpdateWarningPopup.sh

Edit: Fixed typo, referenced below, for anyone else that comes across this.

b3nn
New Contributor II

one step closer! :) Thanks
Now the output is the name of the variable $SUSNOTE instead of the result. What do the signify?

Josh_S
Contributor III

Is that not what you're trying to do? I assumed that you wanted the script "/var/uits/UpdateWarningPopup.sh" to run the softwareupdate command and set the variable "SUSNOTE" when you run that script, not when you run this script. The is an escape character. It signifies that the following character should be taken literally, not indicating that it is a variable signifier in this case. If that's not what you want...

SUSNOTE="$(softwareupdate -l | grep '*' -c)"

#Creates the script that the Launchd will call to make the popup
echo "#!/bin/sh
terminal-notifier -message 'There are several updates waiting for you in Self Service Click her to launch Self Service' -title '$SUSNOTE Pending Updates' -activate com.jamfsoftware.selfservice -appIcon /.pVault/Spark.png " > /var/uits/UpdateWarningPopup.sh

b3nn
New Contributor II

Ya, i tried that one before, but it also excludes the $SUSNOTE from the -title

Its mean to be:

-title '$SUSNOTE Pending Updates'

but after Casper gets a hold of it it becomes

-title '  Pending Updates'

.... on the saved file on the end user.

mm2270
Legendary Contributor III

Try changing the SUSNOTE line to this-

SUSNOTE=$(echo $(softwareupdate -l | grep '*' -c))

In some quick tests I was also getting a blank value returned into the resulting script file even though the variable was reporting a value of '4" It started to work after doing the echo of the softwareupdate result like above.

Josh_S
Contributor III

It will replace it with the value of the "SUSNOTE" variable. Which should be the number of '' that "softwareupdate -l" shows. Casper just runs the script, I've never noticed anything different from running something locally or running it through Casper. I did notice, in your original script, that there is a space following the equals sign. That will result in not setting the SUSNOTE variable. It will then be null, and replaced with nothing - as expected.

All that said, it might be easier to not have your first script create a second script. If you create a package with the following script as part of your policy, it should do what you want. If it's not needed, you can even delete this script from the machine using your original script if it's run 'after' deploying the package.

/var/uits/UpdateWarningPopup.sh

#!/bin/sh
SUSNOTE="$(softwareupdate -l | grep '*' -c)"
terminal-notifier -message 'There are several updates waiting for you in Self Service Click her to launch Self Service' -title "$SUSNOTE Pending Updates" -activate com.jamfsoftware.selfservice -appIcon /.pVault/Spark.png

* With the exception that the first three parameters passed are set as mount point, computer name, and username. Also, take note that variables effectively start out as null until defined. Which might screw up your testing if you're testing in a terminal window which might have variables set already somewhere else that you aren't aware of. Start your testing with a fresh terminal window to help with this.

b3nn
New Contributor II

The space after the = was then only way to get it to work until I added the ( ).

Sadly the script doesn't work once casper deploys it.

I thought of adding a pkg instead of making a script make a script. But I have a lot of future uses for this process that I would love to find out why. I just submitted a ticket with JAMF to see if they have a suggestion.

alexjdale
Valued Contributor III

Yeah, having the script echo out the second script is going to be messy. Is your goal just to make it easier/quicker to deploy script files to systems?

b3nn
New Contributor II

Ya. I want to be able to use the variable within casper $4 $5 $6 etc, to make customizable scripts.

Josh_S
Contributor III

Ok, there are a lot of ways to skin this cat. Just wanted to make one note, and let you know about a test I did and the output to see if it helps you.

= the assignment operator (no space before and after)

http://www.tldp.org/LDP/abs/html/varassignment.html

Entire script, directly copied and pasted from my test JSS:

#!/bin/bash

SUSNOTE="$(softwareupdate -l | grep '*' -c)"

mkdir /var/uits

#Creates the script that the Launchd will call to make the popup
echo "#!/bin/sh
terminal-notifier -message 'There are several updates waiting for you in Self Service Click her to launch Self Service' -title '$SUSNOTE Pending Updates' -activate com.jamfsoftware.selfservice -appIcon /.pVault/Spark.png " > /var/uits/UpdateWarningPopup.sh

chmod +x /var/uits/UpdateWarningPopup.sh

Resulting file on test computer, with 6 updates available:
/var/uits/UpdateWarningPopup.sh

#!/bin/sh
terminal-notifier -message 'There are several updates waiting for you in Self Service Click her to launch Self Service' -title '6 Pending Updates' -activate com.jamfsoftware.selfservice -appIcon /.pVault/Spark.png

b3nn
New Contributor II

That worked, thanks a million!!

essentially you pre acquire the variable and drop it into the script as a static number?

Josh_S
Contributor III

@b3nn Exactly. Just make sure you note that it is a static number. It won't update when you run /var/uits/UpdateWarningPopup.sh again. You'll need to regenerate that file every time. The alternative is what I first posted... and just now realized there is a typo in. Variables don't expand within single quotes.

#Creates the script that the Launchd will call to make the popup
echo "#!/bin/sh
SUSNOTE="$(softwareupdate -l | grep '*' -c)"
terminal-notifier -message 'There are several updates waiting for you in Self Service Click her to launch Self Service' -title "$SUSNOTE Pending Updates" -activate com.jamfsoftware.selfservice -appIcon /.pVault/Spark.png " > /var/uits/UpdateWarningPopup.sh

Note, however, that scripts run by Casper are executed as root. If you're executing this second script from a LaunchAgent, and you need root privileges to gather a value for your variable, you'll need to pre-acquire the value.

b3nn
New Contributor II

I setup the policies to work that way anyway. Once they do all their updates the LaunchD and .sh are deleted until the next time it detects updates, then rebuilds those files from scratch. I also did that because i have the Terminal notifier app updating through Github every week, incase there are improvements.

Thanks a million!!!