Ask Your Question

How I can store the output of a linux command in a variable

asked 2015-04-23 05:33:50 -0600

bantubanerji gravatar image

Is it possible to store the output of an UNIX command in a variable like - $myversion = generate ("/bin/ls -l /usr/lib/jvm|/bin/grep -v lrwxrwxrwx|/usr/bin/tail -1|/bin/awk '{ print $9 }'")

If not please advice how we can define dynamic value in a variable.

edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted

answered 2015-04-23 07:10:09 -0600

JohnsonEarls gravatar image

updated 2015-12-03 07:13:20 -0600

Try this:

$myversion = generate ("/bin/bash", "-c", "/bin/ls -l /usr/lib/jvm | grep -v lrwxrwxrwx | /usr/bin/tail -1 | /bin/awk '{print \$9}'")

The reason: generate accepts a variable number of arguments. The first argument is the command to run, and the remaining arguments are passed to that command. generate does not run the command through a shell, so if you need to do shell-style logic (pipes, redirects, etc.), then you need to run the shell yourself and pass your shell command in as an argument to the -c option of the shell, as shown in this example.

Edit: Because the shell string is in " quotes, the awk $9 parameter needs to be \ escaped.

edit flag offensive delete link more

answered 2015-04-24 12:49:56 -0600

GregLarkin gravatar image

The first question is, do you want your command to run on the master or the agent? The code you have above will run on the master, but if you want to run it on an agent, you need to use a custom fact.

edit flag offensive delete link more


That's a good point; I hadn't realized that during my testing.

JohnsonEarls gravatar imageJohnsonEarls ( 2015-04-24 15:24:29 -0600 )edit

answered 2015-04-25 10:54:00 -0600

scoffland gravatar image

I would suggest using a custom fact to store the variable. Custom facts can be gather by facter in the external-dir dependent on your puppet version see . In this case assuming you are using the open source version you would create a script /etc/facter/facts.d/ with the following content

### This fact creates the variable $myversion
echo myversion=$("/bin/ls -l /usr/lib/jvm|/bin/grep -v lrwxrwxrwx|/usr/bin/tail -1|/bin/awk '{ print $9 }'")

You also instead might use

 java -version 2>&1| grep version | cut -f2 -d'"' 
 java -fullversion 2>&1| grep version | cut -f2 -d'"'

Note: This approach requires the fact to be in place before the puppet run so if you want to use puppet to put the fact in place it will take one run to install the fact and then the fact will be available for future runs.

edit flag offensive delete link more


Thank you very much Scoffland, your idea helped me a ton. But I had to remove the leading and training double quote ("). I used this - echo myversion=$(/usr/bin/test -f /usr/bin/java 2>&1 && /usr/bin/java -version 2>&1| /bin/grep version | awk -F'"' '{ print $2 }' || echo null)

bantubanerji gravatar imagebantubanerji ( 2015-04-28 04:49:54 -0600 )edit

answered 2015-04-24 20:28:24 -0600

bantubanerji gravatar image

Thank you very much to all for your time and advice.

I would like to use the command in standalone puppet using "puppet apply .. " command.

Johnson's advice did not throw any error in --noop, but surprisingly it did not read the entire command. For example, instead of reading /bin/ls -l /usr/lib/jvm | grep -v lrwxrwxrwx | /usr/bin/tail -1 | /bin/awk '{print $9}' it reads till /bin/ls -l /usr/lib/jvm | grep -v lrwxrwxrwx | /usr/bin/tail -1 and sets the variable. Not sure if puppet has any limitation on how many pipe (I) it can interpret ?

However, I will try the advice in master as well and will see the outcome.

I will also try to write a custom fact as Greg advices.

edit flag offensive delete link more


can you edit this answer or the original question and include both your test manifest and the output from `puppet apply --debug` ? (Remember to put 4 spaces before each line in order to get them to display as code blocks)

JohnsonEarls gravatar imageJohnsonEarls ( 2015-04-24 21:24:00 -0600 )edit

FYI - I just realized why you would have seen what you did. I modified my `generate` command to include \ before the $9 in the awk statement; because of the "" interpolation, the $9 was disappearing and so awk was printing the entire line. Using \$9 instead fixes that.

JohnsonEarls gravatar imageJohnsonEarls ( 2015-12-03 07:17:45 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2015-04-23 05:33:50 -0600

Seen: 8,194 times

Last updated: Dec 03 '15