OT - bash scripting question

lwindram
Contributor

I am working on a lengthy script (parentScript), part of which generates another script (childScript)

I am using a method I cribbed from Andrina's TempAdmin.sh script, namely:

echo"childScript line 1
childScript line n" > location

This has worked well for me for plists and scripts that do not have variables. BUT... if I send through a variable, the variable is calculated when the childScript is deployed. For instance, if the parentScript sends

X=date '+%m%d%Y:%H:%M:%S'

the childScript receives X=03052015:11:43:49 and then fails to execute properly.

I can get around this by deploying the childScript separately, however I would prefer that the childScript only exist while the parentScript is running.

Thanks in advance.

1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

You have to escape anything in the script that would be considered a variable when trying to echo that into a file, or it gets calculated and the result is echoed into the file instead.

Try this instead-

X=$(date '+%m%d%Y:%H:%M:%S')

The backslash in front of the $( for the variable prevents it from being evaluated when it gets echoed into the script file. It should show up in the child script like:

X=$(date '+%m%d%Y:%H:%M:%S')

View solution in original post

5 REPLIES 5

stevewood
Honored Contributor II
Honored Contributor II

Why not just have both scripts in the JSS, one set to Before and the other set to After. Then add both to a policy and run the policy. Once the Before script finishes the After should run.

Or, two policies, one with the parent script and the other with the child script. The policy with the parent would be set to whatever trigger you want (login/logout/check-in) and you can set the policy with the child to a custom trigger or no trigger at all. Then inside the parent script, call the policy that has the child using that custom trigger or the policy ID.

Would that work?

mm2270
Legendary Contributor III

You have to escape anything in the script that would be considered a variable when trying to echo that into a file, or it gets calculated and the result is echoed into the file instead.

Try this instead-

X=$(date '+%m%d%Y:%H:%M:%S')

The backslash in front of the $( for the variable prevents it from being evaluated when it gets echoed into the script file. It should show up in the child script like:

X=$(date '+%m%d%Y:%H:%M:%S')

davidacland
Honored Contributor II

Hi,

using ` in the variable will cause it to expand and save the output as X. I'm not sure on the final outcome but you could wrap the whole thing in double quotes:

"X=`date '+%m%d%Y:%H:%M:%S'`"

or if you just want the command sent across it would be:

X="date '+%m%d%Y:%H:%M:%S'"

So essentially its the use of ` instead of " or ' that looks to be the problem.

lwindram
Contributor

We have a proxy-based content filter on our student devices. We have a contingent of students who travel for school business to locations where they need to connect to a guest network. This has been a problem as they need access to the proxy server in order to view the guest authentication webpage, but need to fill in the authentication page in order to reach the proxy server. They end up getting caught in a loop that leaves them unable to access the internet with their devices.

As they don't have access to the internet they also don't have access to the JSS, so the script will be installed locally and run by the trip chaperone. It turns off the auto proxy on a temporary basis to allow them an opportunity to connect. The childScript runs simultaneously and logs all of their activity - this includes periodic screenshots. I would prefer to delete it once run so that no incidental logging occurs, however I need it to be available if they are unsuccessful at connecting during their first attempt.

yr_joelbruner
New Contributor III

This might be of some help tangentially:
http://stackoverflow.com/questions/1250079/how-to-escape-single-quotes-within-single-quoted-strings

Also my script below uses some of those quoting techniques in the above link: allowing single quotes within single quotes to be treated as just that, as well as using variables within single quotes: http://www.brunerd.com/blog/2015/02/28/recoveryhd-updater-now-with-pkg-building/

It might help in getting X=date '+%m%d%Y:%H:%M:%S' to be treated as a literal string and not something to be evaluated.... BTW the backtick style should be updated to the $() style, you'll be happy you did :)

Why is $(...) preferred over `...` (backticks)?
http://mywiki.wooledge.org/BashFAQ/082
http://unix.stackexchange.com/questions/126927/have-backticks-i-e-cmd-in-sh-shells-been-deprecated