LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-26-2017, 09:27 AM   #1
Reprovo
Member
 
Registered: Jul 2013
Posts: 59

Rep: Reputation: Disabled
Issue saving a PID to a variable using ps ax and grep/awk


Hi.

If I run the following command:

Code:
 ps ax | grep [b]ash | awk '{print $1}';
I always get the expected answer but if i try to save to variable:

Code:
 myvar=$(ps ax | grep [b]ash | awk '{print $1}');

$myvar is giving strange output:

echo $myvar

2 170

2 174 177


Would appreciate any assistance as to why this is happening ?

Last edited by Reprovo; 11-26-2017 at 10:34 AM.
 
Old 11-26-2017, 10:13 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,849

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
First of all, please use [code][/code] tags in your posts to preserve formatting.

Second, what exactly are you trying to do here? What is your ultimate goal?
(Check out the "asking the right question" link in my signature.)

grepping ps ax for "bash" is likely to give you multiple lines of output, depending on how many processes are running at the time. And storing the results from multiple lines in a single variable is probably not what you want. Use arrays for multiple values.

Third, you don't need grep here. awk can do it all.

Code:
ps ax | awk '/bash/ { print $1 }'
(And why are you using "[b]ash"? You don't need grouping brackets for single characters.)

Fourth, try using pgrep instead.

Fifth, proper quoting is very important in the shell. echo $myvar may not give you the output you expect. Especially if you have multiple values stored in it (see #2). Read: http://mywiki.wooledge.org/Quotes

Last edited by David the H.; 11-26-2017 at 10:19 AM. Reason: fixed tags & expanded on question
 
1 members found this post helpful.
Old 11-26-2017, 10:44 AM   #3
Reprovo
Member
 
Registered: Jul 2013
Posts: 59

Original Poster
Rep: Reputation: Disabled
Hi.

Thanks for taking a look.


1) have added code brackets



2) I'm just doing this for learning purposes. I'm running a bash shell in windows, there Is only 1 bash process running. If I just run the command:

Code:
 ps ax | grep [b]ash | awk '{print $1}';
output is always the same:

Code:
root@WLCYLTP02:~/learning/sed/mine# ps ax | grep [b]ash | awk '{ print $1 }';
2
root@WLCYLTP02:~/learning/sed/mine# ps ax | grep [b]ash | awk '{ print $1 }';
2
root@WLCYLTP02:~/learning/sed/mine# ps ax | grep [b]ash | awk '{ print $1 }';
2
root@WLCYLTP02:~/learning/sed/mine# ps ax | grep [b]ash | awk '{ print $1 }';
2
root@WLCYLTP02:~/learning/sed/mine# ps ax | grep [b]ash | awk '{ print $1 }';
2



3)
Tried with:

Code:
 ps ax | awk  '/bash/ { print $1 }'
Code:
root@WLCYLTP02:~/learning/sed/mine# ps ax | awk '/bash/ { print $1 }'
2
199
root@WLCYLTP02:~/learning/sed/mine# ps ax | awk '/bash/ { print $1 }'
2
201
root@WLCYLTP02:~/learning/sed/mine# ps ax | awk '/bash/ { print $1 }'
2
203
Checking the above, it appears when i save as variable, i'm saving the awk PID also.

Code:
root@WLCYLTP02:~/learning/sed/mine# ps ax
  PID TTY      STAT   TIME COMMAND
    1 ?        Ss     0:00 /init
    2 tty1     Ss     0:01 /bin/bash
  204 tty1     R      0:00 ps ax

i was using [b]ash so the grep process doesn't appear as it wont search for [b]ash , only wanted the single bash process

Code:
root@WLCYLTP02:~/learning/sed/mine# ps ax | grep [b]ash ;
    2 tty1     Ss     0:01 /bin/bash
root@WLCYLTP02:~/learning/sed/mine# ps ax | grep bash ;
    2 tty1     Ss     0:01 /bin/bash
  224 tty1     S      0:00 grep --color=auto bash

4,5 - will check.

Last edited by Reprovo; 11-26-2017 at 11:12 AM.
 
Old 11-26-2017, 11:40 AM   #4
Reprovo
Member
 
Registered: Jul 2013
Posts: 59

Original Poster
Rep: Reputation: Disabled
Ok.

I see what's going on based on what you mentioned earlier regarding the multiple bash processes. I thought only one is running but when i run the command substition, i'm creating another /bin/bash process.

Thanks for the help.
 
  


Reply

Tags
awk, bash, grep


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
variable+grep+shell script issue. aayushgoel Programming 4 11-10-2011 12:35 AM
[SOLVED] storing grep output to feed awk to retrieve entire records matching variable chargaff Programming 8 08-13-2010 06:10 AM
awk variable issue cdestiny Linux - General 5 03-10-2010 11:26 PM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM
kill pid.... won't work with 'pid' variable given.. sachitha Programming 6 03-06-2006 07:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration