LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-12-2008, 10:04 AM   #1
runnerpaul
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Fedora 5
Posts: 67

Rep: Reputation: 15
What do these two lines mean?


Hey,

Anybody know what these two lines mean

Code:
serverName=`grep $i $confFile|sed 's/^[   ]*\([^=  ]*\)[=    ].*$/\1/g'`
   pid=`ps -efa | grep $serverName|grep -v 'grep '|sed 's/^[^0-9]*\([0-9]*\)[^0-9].*$/\1/'`
Cheers
Paul
 
Old 08-12-2008, 10:19 AM   #2
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
The first line searches for pattern $i in $confFile and extracts the left hand side of an assignment. The second line searches for a process with a command line (or possibly user name) matching $serverName and extracts the PID. Both lines are examples of pretty bad shell programming.
 
Old 08-13-2008, 08:21 AM   #3
runnerpaul
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Fedora 5
Posts: 67

Original Poster
Rep: Reputation: 15
Cheers. I had forgot about this one. Can you tell me what its about?

motoScript=`echo $i|sed 's/^[ ]*\([^= ]*\)[= ].*$/\1/g'`

How would I write those lines better?
 
Old 08-13-2008, 08:33 AM   #4
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
That is basically the same as the first line of your first example. It extracts the left hand of an assignment from a string, which happens to be assigned to the variable "i".

Using grep and then sed in a pipeline is wasteful, since sed can do everything grep can (and then some). The flag "g" in the first line is also unnecessary, since there can only be one replacement as the entire line is replaced. The second line could be replaced by a suitable use of "pgrep".
 
Old 08-14-2008, 06:21 AM   #5
runnerpaul
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Fedora 5
Posts: 67

Original Poster
Rep: Reputation: 15
Cheers. Got another one for you. What does the wc -1 mean here?
Code:
cscrpt=`ps -efa |grep $SCRIPTNAME |grep -v 'grep ' |wc -l`
 
Old 08-14-2008, 06:39 AM   #6
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
"wc -l" counts the number of lines. You should probably read man pages..
 
Old 08-14-2008, 06:42 AM   #7
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
"wc -l" counts the lines of output. The statement returns the number of instances of the script named "$SCRIPTNAME" that are running. Again, you could also say something like

Code:
pgrep $SCRIPTNAME | wc -l
 
Old 08-14-2008, 07:57 AM   #8
runnerpaul
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Fedora 5
Posts: 67

Original Poster
Rep: Reputation: 15
Instead of returning the number of instances running how could you return the name of the instances running?
 
Old 08-14-2008, 08:47 AM   #9
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
You already know the name. It is assigned to SCRIPTNAME.
 
Old 08-14-2008, 08:51 AM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Is it ok ?
Code:
ps -eao comm=
 
  


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
Asterisk box to connect VoIP lines with normal telephone lines. sraju Linux - Software 1 11-08-2006 12:38 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 02:03 PM
Counting Lines ej25 Programming 20 12-06-2004 02:08 PM
How many lines? WindozBytes Linux - General 17 03-30-2002 07:10 PM

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

All times are GMT -5. The time now is 02:04 PM.

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