LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-12-2006, 03:40 PM   #1
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
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?
 
Old 07-12-2006, 03:52 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
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.
 
Old 07-12-2006, 03:54 PM   #3
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
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.
 
Old 07-12-2006, 04:06 PM   #4
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
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' ?
 
Old 07-12-2006, 04:09 PM   #5
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
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 ~]$
 
Old 07-12-2006, 04:09 PM   #6
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
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"
 
Old 07-12-2006, 04:22 PM   #7
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
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 ~]$
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Difficulty in Cinelerra BobNutfield Linux - Software 2 02-15-2006 07:33 AM
difficulty with wireless - sometimes 105659 Linux - Wireless Networking 0 01-22-2006 08:36 PM
Sound difficulty ruzvay Linux - Hardware 0 06-01-2004 03:26 AM
Qt progaming difficulty nkarakos Programming 9 09-24-2003 07:57 PM
Difficulty with .xsession vidigiani Linux - Newbie 2 08-10-2003 09:24 AM

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

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