Prateek Nagpal
Prateek Nagpal

Reputation: 1

Multiple use of grep command

Can anyone tell what this line actually mean?? Here the grep command is used multiple times.

#chkCount=`ps -aef | grep -i "File_Transfer.sh"|grep -v grep| wc -l`

Upvotes: 0

Views: 205

Answers (3)

shrewmouse
shrewmouse

Reputation: 6050

Let's break this down piece by piece. First of all the command is commented out so it won't run until you remove the starting #

chkCount=`ps -aef | grep -i "File_Transfer.sh"|grep -v grep| wc -l`

The output of the command ps -aef | grep -i "File_Transfer.sh"|grep -v grep| wc -l will be stored in the variable chkCount.

grep -i "File_Transfer.sh" looks for lines containing "File_Transfer.sh" but grep will ignore the case of the string. That means that grep will match "File_Transfer.sh", "File_TRANSFER.SH", or any other combination.

grep -v grep will match any line that DOESN'T contain the word grep. -v inverts the match. The purpose of this is to eliminate the command you issued. In other words ps will list a process called File_Transer.sh and it will list your command ps -aef | grep -i "File_Transfer.sh"|grep -v grep| wc -l. grep -v will ignore ps -aef | grep -i "File_Transfer.sh"|grep -v grep| wc -l and just match any process matching File_Transfer.sh

Lastly, wc -l will count the number of lines fed into wc. So the result will be the number of processes running that matches "File_Transfer.sh"

Upvotes: 2

Zeiss Ikon
Zeiss Ikon

Reputation: 481

Multiple use of grep (in a piped combination like this) is like a two-stage filter; whatever lines are passed from the first instance will be filtered again according to the second instance's arguments. In this case, the result of ps -aef is piped to grep, which does a case-insensitive search for "File_Transfer.sh"; then all the lines output by grep are fed to grep again, which passes all lines that don't contain grep (-v means "invert match", look for what doesn't match, rather than what does) to wc -- at the end, the final result (a count of occurrences) is assigned to variable chkCount.

Upvotes: 0

Adashi
Adashi

Reputation: 461

This line lists all running processes and filters lines that include "File_Transfer.sh".

The output is then further filtered to remove lines that include "grep", since the grep command itself will show up as a running process with the text "File_Transfer.sh".

wc -l counts how many lines found.

Upvotes: 0

Related Questions