LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Difficulty in understanding (https://www.linuxquestions.org/questions/programming-9/difficulty-in-understanding-463431/)

Gins 07-12-2006 03:40 PM

Difficulty in understanding
 
ex-08-03_arithsub.sh*



#!/bin/sh -v
# Chapter 08 - Section Command Substitution
# This script demonstrates the use of command substitution

DATE=`date`
USERS=`who | wc -l`
UP=`date ; uptime`

echo $DATE
echo $USERS
echo $UP

grep `id -un` /etc/passwd
--------------------------

I copied the above program from an online book. I named it as ' unwanted9 '. It worked fine and the following is the output.



[nissanka@c83-250-110-112 ~]$ ./unwanted9
Wed Jul 12 22:25:32 CEST 2006
4
Wed Jul 12 22:25:32 CEST 2006 22:25:32 up 4:40, 4 users, load average: 0.00, 0.00, 0.00
nissanka:x:500:500:Nissanka De Silva:/home/nissanka:/bin/bash


---------------------------------------------------------------------
I can't fathom out certain lines in the program.

USERS=`who | wc -l` ---> What is this doing? I think there is a filter.



grep `id -un` /etc/passwd ----> I am completly clueless as to this line though I know some grep commands. What is ' id -un ' ?

Those apostrophes are tilted. Why is that? How can I write tilted apostrophes?

nadroj 07-12-2006 03:52 PM

Quote:

# 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.

leonscape 07-12-2006 03:54 PM

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.

EDIT:
Damn! to slow.

Gins 07-12-2006 04:06 PM

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' ?

Gins 07-12-2006 04:09 PM

Thanks for the reply leonscape.

I got the following on my system.

[nissanka@c83-250-110-112 ~]$ who | wc -l
3



[nissanka@c83-250-110-112 ~]$ who
nissanka :0 Jul 12 17:45
nissanka pts/0 Jul 12 17:45
nissanka pts/1 Jul 12 23:01
[nissanka@c83-250-110-112 ~]$

leonscape 07-12-2006 04:09 PM

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"

Gins 07-12-2006 04:22 PM

I am not familiar with the '' id '' command. Now I know it too.


[nissanka@c83-250-110-112 ~]$ id
uid=500(nissanka) gid=500(nissanka) groups=500(nissanka)

[nissanka@c83-250-110-112 ~]$ id -un
nissanka
[nissanka@c83-250-110-112 ~]$


All times are GMT -5. The time now is 06:30 AM.