One Line Command how to use pipe statements to execute and comment on multiple possible outcomes

Hello Forum,

I'm looking to expand the following command:

INACTIVE_KERNELS=$(python -mplatform | grep -qi red && rpm -qa | grep '^kernel-[0-9]' |grep -vE `uname -r` | paste -sd \; || echo "Not Red Hat Server")

Currently this command will check if my server is RedHat server using the grep -qi red statement. If my Server is not RedHat I echo an statement that says it's not a red hat server.

I've just discovered that if the server is Red Hat and this

rpm -qa | grep '^kernel-[0-9]' |grep -vE `uname -r` | paste -sd \;

results with no inactive kernels, nothing gets written out to this INACTIVE_KERNELS variable. I would like to have my one line code to either write out the Inactive Kernels or to write out a message that says "No Inactive Kernels Found".

From my perspective, I don't understand why you do not do all the text processing and system call in python, since your command starts out with python.

python works just fine to process text, make system calls, etc.

For example, you can run the rpm -qa command in Python as follows:

import os
os.system('rpm -qa')

Or a more likely syntax, here is an example:

import subprocess 
time = subprocess.check_output('date')
print 'Now the time is', time

Since your script starts out with python, why not do the rest of the processing in python?

2 Likes

Appending this would test if the returned string is empty, so you could try that:

| grep .

--
The return code of the earlier grep command is nixed by the successful paste command

1 Like

Thank you @Neo, I am interested in moving my script to another method of running. Ideally I will be using Ansible once we have that setup. For now this quick and dirty bash script I'm using/modifying is getting us over the hump of getting the detail we need now.

Thank you @Scrutinizer for your reply. Where would I put this | grep . code you suggested? I tried adding your suggestion as follows but the result is still a blank response:

INACTIVE_KERNELS=$(python -mplatform | grep -qi red && rpm -qa | grep '^kernel-[0-9]' |grep -vE `uname -r` | paste -sd \; || echo "Not Red Hat Server" | grep . | echo "Empty")

Thank you.

Ansible is a Python application, so if you write this simple text processing script in Python it will fit into your Ansible configuration management nicely; and it is a very easy script to write in Python so since you are moving to Python anyway, it does seem good to use Python script to process simple text processing requirements.

Python is very easy to work with.

But, you do what you want to do :slight_smile:

Try this modification to your code:

INACTIVE_KERNELS=$(python -mplatform | grep -qvi red && echo "Not Red Hat Server" || rpm -qa | grep '^kernel-[0-9]' |grep -vE `uname -r` | paste -sd \; | grep . || echo "No Inactive Kernels Found")
1 Like

Thanks very much Scrutinizer for your help! That was the bit of code that solved my issue.

Fully agree with you Neo. Now that my report is providing the information for my team as required I'll build in time to learn how to move my script to Python so I'll be ready to use with Ansible.