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


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers One Line Command how to use pipe statements to execute and comment on multiple possible outcomes
# 1  
Old 11-11-2019
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:

Code:
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
Code:
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".
# 2  
Old 11-11-2019
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:

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

Or a more likely syntax, here is an example:

Code:
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?
These 2 Users Gave Thanks to Neo For This Post:
# 3  
Old 11-11-2019
Appending this would test if the returned string is empty, so you could try that:
Code:
| grep .


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

Last edited by Scrutinizer; 11-12-2019 at 12:04 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 11-12-2019
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:

Code:
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.
# 5  
Old 11-12-2019
Quote:
Originally Posted by greavette
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.
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 Smilie
# 6  
Old 11-12-2019
Quote:
Originally Posted by greavette
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:

Code:
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.
Try this modification to your code:
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")


Last edited by Scrutinizer; 11-12-2019 at 01:49 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 11-12-2019
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Quick and easy way to comment out multi lined print statements

Is there a quick and easy way to comment out multi lined print statements? something like this? printf("3408 strings_line_tokens %s \n", strings_line_tokens); (6 Replies)
Discussion started by: cokedude
6 Replies

2. Shell Programming and Scripting

Read line by line and execute command

Hi ALL, I have a requirement like this. 1.GET ALL TABLE NAME (just table name) keep in file 2.Read line by line and get the count of table from tablename files. tablename detail has a sql statement "db2 select tabname from syscat.tables" (1 Reply)
Discussion started by: netdbaind
1 Replies

3. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. UNIX for Dummies Questions & Answers

Execute multiple comands on one single line

hi, I have a litle problem i wish to excute this comand : ./test.sh -e txt /home -l a so what it says is that "-e" shows me all the extenions that are ".txt" in "home" this works but then i wish that "-l" will show all files begining in this case with the letter "a" . Both comands work... (5 Replies)
Discussion started by: samirboss
5 Replies

5. Web Development

cannot execute copy comment but can do echo

I have set the sh file to chmod 755 i only able to print the output but cannot execute the copy why? Below is the code for sh file echo "Hello" cp hello.sh helloworld.sh Below is my code for php. <html> <head> </head> <body> <?php echo '<p>Hello</>'; ?> <?php shell_exec('sh... (4 Replies)
Discussion started by: zhengkoon8
4 Replies

6. Shell Programming and Scripting

execute multiple statements in if-else

how can we execute multiple statements in else condition i have if then statement else statements fi in else condition i have multiple statements but it executing only one statement is there any way to execute multiple statements (4 Replies)
Discussion started by: nani1984
4 Replies

7. Shell Programming and Scripting

How to execute a no of SELECT COUNT(*) statements using a loop

HI Unix Gurus, I have a number of SELECT count(*) statements in an input file and I want to execute it using a shell script but one by one using loop in script.... How can I do this..... (7 Replies)
Discussion started by: ustechie
7 Replies

8. Shell Programming and Scripting

How do I execute multiple statements within If then else

Please help me. I have been doing this for several hours. Here is the code if then echo a b c d >> file.txt echo 1111 >> file.txt fi The reason I want the two echo is because I want these statements printed on multiple lines. I keep getting error . First it tells me... (2 Replies)
Discussion started by: asemota
2 Replies

9. Shell Programming and Scripting

How to execute the multiple line sql using shell script

Hi All, Please help me to write a shell script to execute the below sql query. select c.account_no,b.bill_no,a.pay_type,(b.total_due + b.recvd + b.adjusted + b.disputed + b.transferred) as amt_not_billed,d.cash_on_delivery, (select j.bill_no from billinfo_T y, bill_t j where... (1 Reply)
Discussion started by: girish.raos
1 Replies

10. UNIX for Dummies Questions & Answers

exit status of command in a pipe line

Hi, I am trying to test the exit status of the cleartool lsvtree statement below, but it doesn't seem to be working due to the tail pipe, which it is testing instead. Is there a way around this without adding a tonne of new code? cleartool lsvtree $testlocation/$exe_name | tail -15 ... (10 Replies)
Discussion started by: topcat8
10 Replies
Login or Register to Ask a Question