Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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?
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.
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.
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?:
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.