LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 09-22-2009, 02:09 AM   #1
rasa
LQ Newbie
 
Registered: Sep 2009
Posts: 20

Rep: Reputation: 0
How to split strings in shellscript


Hi,

I am trying to write a shell script. The purpose of the script is to provide the list of open files in server.

For that i need to get the pid. Here i am using pidof java and storing it in a variable

Quote:
[root@server usr1]# value=`pidof java`
[root@server usr1]# echo $value
13713 13643 13642
[root@server usr1]#
Here the value is a single string separated with space.
for executing ls -l /proc/procid/fd i need the process id value.

How i will get this.


Regards
Rasa.

Last edited by rasa; 09-22-2009 at 02:11 AM.
 
Old 09-22-2009, 02:22 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
for pid in $(pidof java)
do
something $pid
done

don't use backticks in scripting, use $(...) notation instead.
 
Old 09-22-2009, 02:39 AM   #3
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by rasa View Post
Hi,

I am trying to write a shell script. The purpose of the script is to provide the list of open files in server.

For that i need to get the pid. Here i am using pidof java and storing it in a variable



Here the value is a single string separated with space.
for executing ls -l /proc/procid/fd i need the process id value.

How i will get this.


Regards
Rasa.
I assume you want the PIDs to be separated. Try this:

Code:
array=($(pidof java))

echo ${array[0]}

echo ${array[1]}
etc..
 
Old 09-23-2009, 12:09 AM   #4
rasa
LQ Newbie
 
Registered: Sep 2009
Posts: 20

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by lutusp View Post
I assume you want the PIDs to be separated. Try this:

Code:
array=($(pidof java))

echo ${array[0]}

echo ${array[1]}
etc..
Thanks very much for your replies.

I did it the following way.
Code:
PROCESS_ID=`pidof java`
echo "process id" $PROCESS_ID
VALUE1=`echo $PROCESS_ID | awk '{print $1}'`
VALUE2=`echo $PROCESS_ID | awk '{print $2}'`
VALUE3=`echo $PROCESS_ID | awk '{print $3}'`
 
Old 09-23-2009, 12:53 AM   #5
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
What if there are two or four PIDs? acid_kewpie's method lets you iterate over however many there are and lutusp's can be modified to do that by iterating over the members of the array. But if it's constant, it's constant. Even so, those are probably more efficient than invoking multiple instances of awk. Even if you were going to do it your way, 'cut'ing on space delimiters would be more efficient.

And you're still using backticks.
 
Old 09-23-2009, 02:33 AM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Quote:
Originally Posted by rasa View Post
Thanks very much for your replies.

I did it the following way.
Code:
PROCESS_ID=`pidof java`
echo "process id" $PROCESS_ID
VALUE1=`echo $PROCESS_ID | awk '{print $1}'`
VALUE2=`echo $PROCESS_ID | awk '{print $2}'`
VALUE3=`echo $PROCESS_ID | awk '{print $3}'`
Hmm, rather you than me, a number of much better solutions have already been posted, each with uses in slightly different ways. Seems illogical to me to assign three equivalent bits of data to three differently named variables. Arbitrary and overly rigid at best. Either loop through them implictly, or use an array for longer storage. Again, backticks are long obsolete, use $(...) instead.
 
Old 09-23-2009, 06:07 AM   #7
mrrangerman
Member
 
Registered: Oct 2007
Location: MI
Distribution: Debian Slackware
Posts: 528

Rep: Reputation: 59
Quote:
acid_kewpie

for pid in $(pidof java)
do
something $pid
done

don't use backticks in scripting, use $(...) notation instead.
I'm rather new to scripting but I do know that both backticks and $(...) notation are legal forms of command substitution, so could you please explain why he shouldn't use backticks?

Thanks
 
Old 09-23-2009, 09:36 AM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
I'd really use the looping or array approaches, jave tends to spawn threads all around after all, so just checking for one pid is misleading.

About `...` vs $(...), two things:
  • clarity: remember, you only have to write it once, it's not like in command line where `...` saves some typing... and it's much better readable
  • it can be nested, allowing you to do funny things like "echo $($(which ls) /etc)" -just a random example with no purpose, which can't be done with `...`

On the contrary, I am not sure how other shells deal with that, though. I think that this is a bashism and it's not compatible with the original sh, so if that's a concern to you, double check it, my memory is blurry.
 
Old 09-23-2009, 12:51 PM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
what he said! To be honest I can't provide anything more formal, but backticks were "officially" deprecated over a decade ago now AFAIK.
 
Old 09-23-2009, 02:44 PM   #10
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by i92guboj View Post
On the contrary, I am not sure how other shells deal with that, though. I think that this is a bashism and it's not compatible with the original sh, so if that's a concern to you, double check it, my memory is blurry.
I'm almost positive it's not compatible with v7 sh but it is POSIX and ash has no problems with it. So I'd say it's not ultimately portable in the historical sense, but is portable in a current context.
 
Old 09-23-2009, 03:42 PM   #11
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by rasa View Post
I am trying to write a shell script. The purpose of the script is to provide the list of open files in server. For that i need to get the pid. Here i am using pidof java
Is there really a need to mess with 'ls' or arrays to get fds?:
Code:
pgrep -f java | xargs -iX /usr/sbin/lsof -P -w -n -p 'X' -a -d1-255
 
Old 09-24-2009, 08:45 AM   #12
rasa
LQ Newbie
 
Registered: Sep 2009
Posts: 20

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
Is there really a need to mess with 'ls' or arrays to get fds?:
Code:
pgrep -f java | xargs -iX /usr/sbin/lsof -P -w -n -p 'X' -a -d1-255
Yes this is working fine. But what is the issue with the script which i have provided. Actually i have given that to my client. I am a java programmer and i don't know shell scripting much. I made it by referring some tutorial.


Regards
Rasa.
 
Old 09-24-2009, 09:08 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by rasa View Post
Yes this is working fine. But what is the issue with the script which i have provided.
Nothing, in the sense that it works, but it is unnecessarily complex and obscure so difficult to maintain and will call your competence into question if your client does not appreciate that you did you best in unknown territory and cannot be blamed for wielding a scalpel like a battle axe.

unSpawn's solution, on the other hand, is elegantly minimal and uses the power of the available tools.
 
Old 09-24-2009, 10:46 AM   #14
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Quote:
Originally Posted by unSpawn View Post
Is there really a need to mess with 'ls' or arrays to get fds?:
Code:
pgrep -f java | xargs -iX /usr/sbin/lsof -P -w -n -p 'X' -a -d1-255
you're bloody good at this stuff. The things I learn from you never stop.
 
  


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
Split a delimited string into multiple strings... leeharris Programming 10 02-07-2009 06:10 AM
How to split file , .. awk or split ERBRMN Linux - General 9 08-15-2006 12:02 AM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
Help Shellscript paraiso Linux - Newbie 7 05-12-2005 07:25 AM
shellScript help paraiso Linux - Newbie 2 05-10-2005 07:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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