LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-26-2010, 12:30 AM   #1
henrtm05
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Rep: Reputation: 0
Need help understanding a command


I have the following command to show all users belonging to a group.

grep :`grep ^mygroup /etc/group | cut -d: -f3`: /etc/passwd

I would like to understand what is happening here. The command starts with grep then space and a : and another : after -f3 what does the first grep do and what are the colons doing in this command. If someone could explain this it would be very helpful.

Thanks
 
Old 09-26-2010, 01:07 AM   #2
malekmustaq
Senior Member
 
Registered: Dec 2008
Location: root
Distribution: Slackware & BSD
Posts: 1,669

Rep: Reputation: 498Reputation: 498Reputation: 498Reputation: 498Reputation: 498
Hi hentrtm05,

You will get better perception if you first understand the elements of the script (series of commands). Please read the following manual pages in the terminal, type:

--$ man grep
--$ man cut
--$ man passwd
--$ man group

Then, when anyone try to explain to you about your script posted above you shall be ready enough to understand "WHY" and "WHAT'S" happening behind the screen.

Hope it helps.
 
Old 09-26-2010, 01:35 AM   #3
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
There's a couple of shell syntax to go over for you to fully understand.

The pipe | (located above the return key and is part of the backslash key) character is used to redirect the output of one command as input to another. Google shell pipe commands for solid examples.

The backtick ` (located above the tab key and is part of the tilde key ~) uses the output of a command as input in the middle of another command. The command within the backticks is evaluated first and then the overall command. Google shell backticks or see the following for an easy example.
Code:
echo 127.0.0.1
ping 127.0.0.1 -n 4
ping `echo 127.0.0.1` -n 4
Explanation of command
Original command
Code:
grep :`grep ^mygroup /etc/group | cut -d: -f3`: /etc/passwd
The first command executed is:
Code:
grep ^mygroup /etc/group
Grep outputs all lines which start with "mygroup" (^ means start with, read grep man page) from the file /etc/group. Lets say for example the output of that grep command is:
Code:
mygroup:S-1-5-32-545:545:
Now lets evaluate the code within the backticks as a whole.
Code:
grep ^mygroup /etc/group | cut -d: -f3
Cut will take the output of grep and chop it up using a colon : as a delimiter ( -d: ) and takes the 3rd field from the left ( -f3 ). So the output of the full command will be:
Code:
545
Now lets replace the backticks with the output of the commands within the backticks and the command may make a little more sense.
Code:
grep :545: /etc/passwd
Now I don't know if the final result of your command outputs anything but it doesn't in mine (I used a group found on my own system and still output nothing).

That's exactly how the shell code is acting. What're you using it for? Just curious.

Like malekmustaq stated, reading the man pages will clear all the command behavior up and tell you what the arguments are doing.

Last edited by sag47; 09-26-2010 at 01:37 AM.
 
Old 09-26-2010, 10:53 AM   #4
henrtm05
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 0
I am using this command to take all users belonging to two different groups and creating one group containing all those users.

grep :`grep ^mygroup /etc/group | cut -d: -f3`: /etc/passwd

I am sorry, I should have been more specific on my question. I understood what all the commands did. I just didn't know what the colon after the first grep did and before the /etc/passwd did. I think I figured it out, the part in between the colons represent the group number pulled from /etc/group and the program will then use the colons as part of the search in /etc/passwd. An example would be :503: as my search criteria. I am using Linux Centos to perform this operation.
 
Old 09-26-2010, 01:57 PM   #5
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Yes I beleive it is using the colons as search terms as well. I don't think they have any special meaning. Interesting command, I tried it again on my Linux netbook (was using cygwin when I was explaining it) and it works as expected.

Just to be sure it's a good practice to quote the expression and escape the characters. Though grep is smart enough to know not to use the : in its special commands because it doesn't match any cases (e.g. grep "[:alnum:]\+" same as grep "[0-9a-zA-Z]\+"); That may not always be the case when you use this command on other systems with older versions of grep.

Code:
grep "\:`grep ^mygroup /etc/group | cut -d: -f3`\:" /etc/passwd
Produces the same result.

Last edited by sag47; 09-26-2010 at 02:04 PM.
 
  


Reply



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
Tutorial for understanding TOP Command. Raghu140 Linux - General 18 08-01-2010 02:58 PM
Difficulty in understanding nohup Command. sowraj Linux - Newbie 2 01-28-2010 01:59 AM
help with understanding scp command 7mza Linux - Newbie 4 09-15-2009 03:09 AM
understanding read command as a test frankie_DJ Programming 11 08-08-2005 01:30 PM
help understanding this command vince_2x Linux - General 4 09-21-2004 12:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:52 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