Shell Scripting Question

Jason
Contributor II

I've mostly scripted with PowerShell and vbscript and have no experience with shell scripting, so I apologize for the newbness of this question. I'm writing an extension attribute that has a line like this:

ver='/usr/sbin/jamfds -v'
echo "<result>$ver Installed</result>"

When I run that from Terminal i see that $ver has a value of "jamfds 9.32" (which is what I expect). But when I look at a computer in the JSS I only get "/usr/sbin/jamfds -v Installed" in the value field. Any thoughts?

1 ACCEPTED SOLUTION

joshuasee
Contributor III

Uhg. Okay, try grabbing both stdout and stderr.

#!/bin/sh
var=`/usr/sbin/jamfds -v 2>&1`;
/bin/echo $var;

View solution in original post

13 REPLIES 13

JPDyson
Valued Contributor

Change the ' to ` (back tick, same key as ~). So,

#!/bin/sh
ver=`/usr/sbin/jamfds -v`
echo "<result>$ver Installed</result>"

Fuller explanation: What you've actually done is set the variable ver to be a string containing /usr/sbin/jamfds -v, because you used single quotes. Using the back tick means that you want the results from that command, not the string that happens to be a command.

Jason
Contributor II

@JPDyson
No luck with that change. Now I get this as the result:
" Installed"

It doesn't look like it's storing anything in ver now.

frozenarse
Contributor II

"Jamfds" only exists on devices acting as a JDS correct?

joshuasee
Contributor III

Is the shebang present at the start of the script and giving the expected shell? It could be using a different interpreter than expected. You could also try $(command_name) instead of the back ticks.

Jason
Contributor II

@frozenarse
Yes correct. I'm writing this script to be able to create a smart computer group that lists all of the JDS's (so they can be excluded from certain policies)

@joshuasee
Yes. I have "#!/bin/sh" at the very beginning. I've tried $(command_name) with the same result. Here is the Terminal output. Seems like it's not storing the value in $var, but instead just spitting the output to the Terminal.

<HOST>:~ <USER>$ var=`/usr/sbin/jamfds -v`
jamfds 9.32
<HOST>:~ <USER>$ $var
<HOST>:~ <USER>$
<HOST>:~ <USER>$ var=$(/usr/sbin/jamfds -v)
jamfds 9.32
<HOST>:~ <USER>$ $var
<HOST>:~ <USER>$

JPDyson
Valued Contributor

You'd have to echo at the shell to see the contents...

host$ var=`jamf version`
host$ echo $var
version=8.73
host$

Not sure what to tell you; syntax is as I said.

Jason
Contributor II

@JPDyson
I think I see the difference. When I run "jamf version" as you did I get identical results. $var does get the version information stored in it just as you showed. But "jamfds -v" does not for some reason.

var=jamf version does not spit out to console. Does store in $var

var=jamfds -v
Does spit out to console. Does not store in $var

Could be a difference in how JAMF coded the two binaries. I'm assuming there must be some way to get it into a variable though....

joshuasee
Contributor III

Uhg. Okay, try grabbing both stdout and stderr.

#!/bin/sh
var=`/usr/sbin/jamfds -v 2>&1`;
/bin/echo $var;

JPDyson
Valued Contributor

^^I agree with the sentiment and the fix.

Jason
Contributor II

Perfect!
"JDS Role: jamfds 9.32 Installed"

JPDyson
Valued Contributor

So... why is the output going to stderr in the first place?

joshuasee
Contributor III

Apostate! Do not antagonize the JAMF gods, for they provide order unto the LANs, grids, and webs of the Maciverse. It is unthinkable that there would be less than perfect omnipipeance across all binaries, or that all their engineers thoughts are less than perfectly consistent.

joshuasee
Contributor III

More seriously, it is an inconsistency, and I would expect the output to go the stdout since the command exits 0 and has no other output. However, I'm not sure the difference rises to the level of being a bug, not am I aware of an RFC or similar standard covering the matter.