ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
# This script demonstrates the use of command substitution
please note the type of accents used, they are backquotes (`) not regular (' or ") quotes. ` is normally above 'tab' on the top left of the keyboard.
Quote:
USERS=`who | wc -l` ---> What is this doing? I think there is a filter.
its declaring a variable 'USERS' and assigning a value to it (=). again note the ` backquotes, which means that the commands in the backquotes will be substituted for the output they produce. 'who' is a command to see which users are logged in. the vertical line (pipe |) is used to take the output of one command (who) and make it the input of the command on the right (wc). 'wc' is 'word count'. the -l option means to count the number of lines.
Quote:
grep `id -un` /etc/passwd ----> I am completly clueless as to this line though I know some grep commands. What is ' id -un ' ?
'grep' is a powerful command to extract lines of text that contain certain words or expressions. `id' is a program to show IDs of the person logged in. the -u option displays the User ID, the -n option displays the name of this user. '/etc/passwd' means to search this file for the expressions that result from 'id -un' and return all lines that match.
please use the 'man' command to display manual/help pages of any command. open a terminal window and run 'man wc' and youll see all you need to know about this command.
the bits in back quotes, are sub calls, which get replaced with the output of the commands within them. | is a pipe, and is used to join the output of one program to the input of the next program.
So "who" produces the output...
leonscape :0 2006-07-12 21:42
on my machine, basically it tells who you are and the date and time, "w"c counts the number of things in the input either bytes, words ( for which it go the name Word Count. )
or lines. -l option tells it to count the lines, so in my example it would output only 1. So "who" tells you who's logged into the computer and with piping the input into wc -l, its telling you how many lines it outputs, So you get the number of logged in users. Its then assigning the result to USERS.
"grep" is a search command that uses regular expressions. id returns lots of things in this case -u for effrective user, and -n for name instead of number, so you get the name of the effective user.
Its passing the result of this sub call as the first parameter of grep, which is what its to look for, in the second part which is the main password file for the machine, So it should only output the line that matches the name passed into it.
Its quite easy to find out what each command is for and what its options are by using either man, or info followed by the command name.
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Original Poster
Rep:
Thanks nadroj for taking time to reply me. You are very clever at scripting. I am trying to learn when time permits. Sometimes I run C and C++ programmes too.
I need help from you all.
What would happen if I replaced 'backquotes' with ordinary 'quotes' ?
It changes it from a sub command, to just a strongly quoted string, So USERS would equal exactly what you wrote and grep would be searching for a user called "id -un"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.