Posted on 05-02-2014 08:06 AM
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?
Solved! Go to Solution.
Posted on 05-02-2014 11:12 AM
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
Posted on 05-02-2014 08:18 AM
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.
Posted on 05-02-2014 08:42 AM
one step closer! :) Thanks
Now the output is the name of the variable $SUSNOTE instead of the result.
What do the signify?
Posted on 05-02-2014 09:17 AM
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
Posted on 05-02-2014 09:26 AM
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.
Posted on 05-02-2014 09:36 AM
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.
Posted on 05-02-2014 09:48 AM
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.
Posted on 05-02-2014 10:36 AM
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.
Posted on 05-02-2014 10:53 AM
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?
Posted on 05-02-2014 11:07 AM
Ya. I want to be able to use the variable within casper $4 $5 $6 etc, to make customizable scripts.
Posted on 05-02-2014 11:12 AM
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
Posted on 05-02-2014 12:11 PM
That worked, thanks a million!!
essentially you pre acquire the variable and drop it into the script as a static number?
Posted on 05-02-2014 12:22 PM
@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.
Posted on 05-02-2014 12:27 PM
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!!!