LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-26-2014, 08:38 AM   #1
azheruddin
Member
 
Registered: Dec 2011
Posts: 83
Blog Entries: 1

Rep: Reputation: Disabled
Help to understand the script


Hello all,

please help me to uderstand the below snippet of code.

Specially the highlighted in BOLD.

for pid in `ps -eo etime,fname,pid,user | grep pgk1 |egrep '[1-9]-' | awk {'print $3'}`
do
user=`ps -ef | grep $pid | grep -v grep | awk {'print $1'}`
echo "username : $user"
sudo passwd -x -1 $user
echo "Killing PID : $pid"
sudo kill -9 $pid
done

from the comment it is used for killing the unused areas but could you please help which area will get kill by the script means duraion...?
 
Old 11-26-2014, 10:43 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,889

Rep: Reputation: 4259Reputation: 4259Reputation: 4259Reputation: 4259Reputation: 4259Reputation: 4259Reputation: 4259Reputation: 4259Reputation: 4259Reputation: 4259Reputation: 4259
Quote:
Originally Posted by azheruddin View Post
Hello all,
please help me to uderstand the below snippet of code. Specially the highlighted in BOLD.
Code:
for pid in `ps -eo etime,fname,pid,user | grep pgk1 |egrep '[1-9]-' | awk {'print $3'}`
do
user=`ps -ef | grep  $pid | grep -v grep | awk {'print $1'}`
echo "username    : $user"
sudo passwd -x -1 $user
echo "Killing PID : $pid"
sudo kill -9 $pid
done
from the comment it is used for killing the unused areas but could you please help which area will get kill by the script means duraion...?
In this case, the backticks (`) are enclosing a chain of commands. The output of those commands are getting assigned to the "pid" variable. So, for each pid found, it executes that loop. The second bolded statement expires that users password, then the program kills the pid. Not sure why it's expiring the password without seeing the rest of the script, though.

Check out the bash scripting tutorial link in my posting signature. There are other ways of running commands into variables too, but the backticks are just one. You can see the output of that command just by typing everything in backticks at your shell prompt.
 
Old 11-26-2014, 11:12 PM   #3
azheruddin
Member
 
Registered: Dec 2011
Posts: 83
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Thanks for the update.

Could you please explain me what exactly the 'backtricks' command lokking/greping for .
I wanted to know the which value will come in PID.

The script is for killing the unused workstation areas so why the script is expiring the password for that particular workstation.
 
Old 11-27-2014, 12:26 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,352

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
the commands in backticks:
ps -eo etime,fname,pid,user | grep pgk1 |egrep '[1-9]-' | awk {'print $3'}
will be evaluated first, you can try to execute it and check the result. Also you can try to execute it like:
Code:
ps -eo etime,fname,pid,user                                                  # formatted output of ps
ps -eo etime,fname,pid,user | grep pgk1                                      # filtered result
ps -eo etime,fname,pid,user | grep pgk1 |egrep '[1-9]-'                      # even more filtered
ps -eo etime,fname,pid,user | grep pgk1 |egrep '[1-9]-' | awk {'print $3'}   # only column 3 is printed
the for cycle will therefore work on the list of pids.
the line
user=`ps -ef | grep $pid | grep -v grep | awk {'print $1'}`
will find out the user belongs to the pid $pid


why the script is expiring password? I have no idea.....
 
1 members found this post helpful.
Old 12-16-2014, 09:13 PM   #5
azheruddin
Member
 
Registered: Dec 2011
Posts: 83
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Update

Hello,

One quick and small clarification then we can proceed to mark this thread as resolved.

What it does :
Code:
egrep '[1-9]-'
in the below command

Code:
ps -eo etime,fname,pid,user | grep pgk1 |egrep '[1-9]-' | awk {'print $3'}
Output of the command
Code:
ps -eo etime,fname,pid,user | grep pgk1
is as below after that what egrep is looking for dont know.

02:35:20 pkg1 21608 pkb19
02:25:18 pkg1 6300 pkb22
22:54:38 pkg1 20500 pkb20
01:35:24 pkg1 17956 pkb14
01:55:10 pkg1 19957 pkb18
02:28:39 pkg1 1967 pkb21
1-00:13:59 pkg1 27733 pkb17
02:14:37 pkg1 22462 pkb12
02:46:07 pkg1 7189 pkb16
01:59:49 pkg1 12956 pkb15
02:05:12 pkg1 4710 pkb13

03rd coloumn of output of pkg1 is being killed by the script i.e PID .
 
Old 12-16-2014, 11:01 PM   #6
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 81
Quote:
Originally Posted by azheruddin View Post
Hello,

One quick and small clarification then we can proceed to mark this thread as resolved.

What it does :
Code:
egrep '[1-9]-'
in the below command
[1-9] => looking for a single character between 1 to 9
- =>looking for single character '-'

so it matches the line, see highlighted for what it match "1-00:13:59 pkg1 27733 pkb17"

so "print 3" will get this "27733" and the same is being killed after it gets the user running it thats "4th' field.

Last edited by SAbhi; 12-16-2014 at 11:50 PM.
 
1 members found this post helpful.
Old 12-17-2014, 02:38 AM   #7
azheruddin
Member
 
Registered: Dec 2011
Posts: 83
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Update

Thanks Sabhi,

So in this case 1-00:13:59 pkg1 27733 pkb17 so in this case after 9 hours job is killing the session is it?
 
Old 12-17-2014, 02:58 AM   #8
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 81
Quote:
Originally Posted by azheruddin View Post
so in this case after 9 hours job is killing the session is it?
i do not see anything specified as such it should after 9 hours// So no


Quote:
Code:
sudo kill -9 $pid
if you are misunderstanding above to be hours... it is nowhere like that..

check kill commands man page for all details.
 
Old 12-17-2014, 08:06 AM   #9
azheruddin
Member
 
Registered: Dec 2011
Posts: 83
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Update

What I mean that

when it will be become 1-00:13:59 pkg1 27733 pkb17, if you see the other timing i.e output of the ps -etime.. as below .

02:35:20 pkg1 21608 pkb19
02:25:18 pkg1 6300 pkb22
22:54:38 pkg1 20500 pkb20
01:35:24 pkg1 17956 pkb14
01:55:10 pkg1 19957 pkb18

so wha i asked is is that after 9 hours it will 1-00:13:59 pkg1 27733 pkb17
 
Old 12-17-2014, 08:24 PM   #10
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 81
i cant say i understand your question.. your machine is doing what it is told to do..
so the output is in line with it.
and as i have no clue what your machine is doing .. i cant say it is after or before this much hours..
actually it is not about hours.. ps displays the processes running and queried for.. it wont display anything occuring after an interval.
 
  


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
[SOLVED] Shell Script - Need Help to Understand walidsi AIX 6 10-11-2012 03:28 PM
[SOLVED] Trying to understand script kenny53067 Linux - Newbie 17 07-23-2011 07:10 AM
I want to understand script vaibhavs17 Linux - Newbie 2 10-19-2010 05:46 PM
I cannot understand this perl script mq15 Linux - Server 3 10-12-2009 12:44 AM
Help me understand this shell script dwhitney67 Linux - Software 2 10-18-2007 09:02 AM


All times are GMT -5. The time now is 12: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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration