0

I've been struggling for the past two hours on this simple example :

I have this line:

python -c 'import yum, pprint; yb = yum.YumBase(); pprint.pprint(yb.conf.yumvar, width=1)'

Which gives:

Loaded plugins: product-id
{'arch': 'ia32e',
 'basearch': 'x86_64',
 'releasever': '7Server',
 'uuid': 'd68993fd-059a-4753-a7ab-1c4a601d206f',
 'yum8': 'rhel',
 'yum9': '7.1'}

And now, I would just like to get the line with the 'releasever'.

Directly on my Linux, there is no problem:

$ python -c 'import yum, pprint; yb = yum.YumBase(); pprint.pprint(yb.conf.yumvar, width=1)' | grep releasever
 'releasever': '7Server',

I have the answer I am looking for.

But when it comes to put it in a script, I am so helpless.

Currently, I have:

#!/bin/ksh
check="$(python -c 'import yum, pprint; yb = yum.YumBase(); pprint.pprint(yb.conf.yumvar, width=1)')"


echo "${check}"
# The echo works as expected. But now, I would like to do a grep on that variable:

check2=${check}|grep releasever

echo "${check2}"

And the result is empty.

I've tried a lot of different things, like brackets, parentheses, quote, double quote, all-in-one command, but I can't get what I want. I don't know what's happening behind that code, which is very simple. But still…

Can someone help me?

2
  • 4
    Wouldn't it be easier to just do it all in a Python script rather than abuse python -c and then bash the output? Commented Jan 2, 2020 at 11:58
  • I roiled back your edit. Accepting the answer which helped you is quite sufficient; your question should remain strictly a question. Commented Jan 2, 2020 at 13:13

2 Answers 2

1

There seem to be 3 options.

It looks like yb.conf.yumvar is a dictionary. In which case, you could either print yb.conf.yumvar['releasever'] directory in your python script in which case the grep would be unnecessary.

Secondly, don't use grep. Instead, have your python script parse the yb.conf.yumvar into json and print it and then use jq from the outside to print the value of releasever

Third, and this is the sanest way, do what you want in Python directly. ksh scripts will be harder to manage over a longer period of time so just do what you want to do in Python and us os.system to execute external programs.

Sign up to request clarification or add additional context in comments.

Comments

0

Your problem is that you are not putting check in any kind of std input. This would work fine:

check2=$(echo "$check" | grep releasever)

or you could put check content into a file and do like grep <string> < <file>.

3 Comments

This is missing a command substitution. Right now it will try to run a command "$check".
Hello,Thanks for your quick answer. It worked perfeclty. :) I did not think I would have to print the result of a variable again. For me, a variable allows to stock the output of a command so that you can do a grep on it or something. It's strange, but I am quite new to that. I'll remember it for the next time.
Probably don't suggest a useless cat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.