num = 'ps -ef | grep -v grep| grep BatchName| wc -l' [duplicate] - linux

This question already has an answer here:
UNIX grep command (grep -v grep)
(1 answer)
Closed 7 years ago.
I am having the basic knowledge of Unix. I'm programming for a wrapper script but in the above line of code I'm not getting the meaning and output of "grep -v grep". Please explain that in detail. Try to explain requested part only, it would be great help.
Thanks

It's excluding the grep process from the results, that's it. Run
grep --help
And you'll see
-v, --invert-match select non-matching lines
So the line
xxxx 27731 27613 0 17:21 pts/14 00:00:00 grep --color=auto /bin/bash
won't be returned as a result since we excluded "grep".

Related

grepping output of ps, exclude the word grep [duplicate]

This question already has answers here:
More elegant "ps aux | grep -v grep"
(9 answers)
Closed 3 years ago.
I'm using ps to find the pid of a process created to execute the command "sleep 1234 &"
I grep the result to match only "sleep 1234".
ps -A -f | grep "sleep 1234"
however, this matches also the command "grep sleep 1234" itself, returning two lines instead of one. How do I write a pattern for grep to exclude the word 'grep' itself?
Thanks
This is a pretty common problem and the easiest solution is to just surround a character in the grep'ed pattern with square brackets:
ps -A -f | grep "[s]leep 1234"
This will now match sleep 1234, but not [s]leep 1234 (because of the literal ] between s and l), and the grep line no longer matches.
The reason that the grep is in the process list is that pipelines are executed from right to left, so the grep is actually executed prior to the ps.

what is "|" in Ubuntu or Linux in general [duplicate]

This question already has answers here:
What does "|" mean in a terminal command line? [closed]
(3 answers)
Closed 4 years ago.
example :
diskFreeSpace="$(df | grep -v -E 'Filesystem|udev|tmpfs|Home|/dev/sr1|/dev/sr0' | awk '{ print $5-1+1}')"
and hope if you understand what is this command u help me
the symbol is called a "pipe".
The command df | grep is "piping" (redirecting) the standard output of the df command into the standard input of the grep command. In this example the grep command filters the output of the df command. Then the awk commands is for display/format purposes.
Inside the regular expression it is specific to the grep syntax since it is surrounded by quotes it is only considered as an argument passed to grep

I'm trying to understand how to use special characters in grep and wc [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 6 years ago.
Can you explain in detail what happened here on this command?
grep -nw '^root' /etc/passwd
What is the use of ^ and '? Please give me examples and be detailed cuz all I'm hearing is that it's the beginning of the line. I don't understand what each of those special characters means.
How would I go about using wc, grep and ^ to find out the number of non-root processes running on my system?
grep -nw '^root' /etc/passwd
It reads the /etc/passwd file line-by-line and filters out all those lines that ^=begin with the -w=complete word "root". So, for example the line
root:x:0:0:root:/root:/bin/bash
To see all processes on a system, you could use ps aux. and it will show lines like this
root 22866 0.0 [...] 0:00 [kworker/1:0]
As you can see, the lines start with a username. If you pipe the ps aux output through grep, you can use the same RegEx from above, to filter out all lines that do not start with "root".
Use -v to invert pattern matching, so that grep -vw '^root' finds all lines that don't begin with a complete word "root".
ps aux | grep -vw '^root' | wc -l
Finally, wc -l counts the number of lines it receives. So that is the number of all lines from ps aux that do not begin with "root".

display all the ssh pids where the users are connected to in one line [duplicate]

This question already has answers here:
How to join multiple lines of filenames into one with custom delimiter
(22 answers)
Closed 7 years ago.
How can I display all the ssh pids where the users are connected to in one line separated by comma ?
This command displays the output in multiple lines, I would like to have the output in one line.
ps aux | grep -i "ssh" | awk '{print $2}'
from
1325
3255
2323
5321
3252
To
1325, 3255, 2323, 5321, 3252
Thank You!
You may want to use pgrep to get the processes IDs directly:
$ pgrep ssh
1217
5305
This way, you avoid calling ps aux and parsing its output, which will always contain the grep itself.
To join them on a ,-separated list, use paste on a -serial mode:
$ pgrep ssh | paste -s -d,
1217,5305
You could use sed utility in another pipe, so the command would be:
ps aux | grep -i "ssh" | awk '{print $2}' | sed ':a;{N;s/\n/, /};ba'
Where you are in fact replacing new lines (except the final one) by commas.

linux shell scripting kiddie's question

an Unix shell script with only purpose - count the number of running processes of qmail (could be anything else). Easy thing, but there must be some bug in code:
#!/bin/bash
rows=`ps aux | grep qmail | wc -l`
echo $rows
Because
echo $rows
always shows greater number of rows (11) than if I just count rows in
ps aux | grep qmail
There are just 8 rows. Does it work this way on your system too?
Nowadays with linux, there is pgrep. If you have it on your system, you can skip grep -v grep
$ var=$(pgrep bash) # or `pgrep bash | wc -l`
$ echo $var
2110 2127 2144 2161 2178 2195 2212 2229
$ set -- $var; echo ${#}
8
also, if your ps command has -C option, another way
$ ps -C bash -o pid= | wc -l
if not, you can set a character class in your grep pattern
$ ps aux|grep [q]mail | wc -l
It appears that you're counting the grep process itself and the header line that ps normally prints before its output.
I'd suggest something more like:
qprocs=$(ps auxwww | grep -c "[q]mail")
... note that GNU grep has a "-c" switch to have it print a "count" of matches rather than the lines themselves. The trick with the regular expression here is to match qmail without matching the literal string that's on the grep command line. So we take any single character in the string and wrap it in square brackets such that it is a single character "class." The regexp: [q]mail matches the string qmail without matching the string [q]mail.
Note that even with this regex you may still find some false positive matches. If you really want to be more precise then you should supply a custom output format string to your ps command (see the man pages) or you should feed it through a pipemill or you should parse the output of the ps command based on fields (using awk or cut or a while read loop). (The -o option to ps is by far the easiest among these).
No, since I'm not running qmail. However, you will want to, at a bare minimum, exclude the process running your grep:
ps aux | grep qmail | grep -v grep
For debugging, you may want to do:
rows=`ps aux | grep qmail`
echo $rows >debug.input
od -xcb debug.input
(to see your input to the script in great detail) and then rewrite your script temporarily as:
#!/bin/bash
rows=`cat debug.input | wc -l`
echo $rows
That way, you can see the input and figure out what effect it's having on your code, even as you debug it.
A good debugger will eventually learn to only change one variable at a time. If your changing your code to get it working, that's the variable - don't let the input to your code change as well.
Use
$ /sbin/pidof qmail
A few ways...
ps -e | grep ' [q]mail' | wc -l
ps -C qmail -opid= | wc -l
pidof qmail | tr ' ' '\n' | wc -l
pgrep is on many Linux distributions, and I imagine available for other Unices.
[dan#khorium ~]$ whatis pgrep
pgrep (1) - look up or signal processes based on name and other attributes
[dan#khorium ~]$ pgrep mingetty
1920
1921
1922
1923
1924
In your case, pgrep qmail | wc -l should do the trick.

Resources