Skip to Main Content

Infrastructure Software

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to write a shell script

444950Sep 21 2005 — edited Oct 28 2005
<div style="background-color: #EEEEEE; padding: 5px 5px 5px 5px;">
Original message by user452590:<br><br>
<div style="position: relative; margin-left: 20px; background-color: #DDDDDD; font-size: 85%; padding: 5px 5px 5px 5px;">
Hi,<br>
<br>
I am a beginner, just started learning to write shell scripts.<br>
<br>
I have to write a test script that all the processes that are running in a directory and then kill all processes.<br>
Can any one kindly guide me through it? <br>
</div>
</div>
<p>
Assuming that you're using Bash for your scripting, consequently that you are running on Linux or Unix of some flavor, and that you are being literal in your requirements, you would need:
<ol><li>To execute "ps" with flags to show all processes and process IDs ("ps -aux" on most Linux systems and "ps -ef" on Solaris 9), and capture the output into a variable or, more likely, array.</li>
<li>Parse the process and process ID out of each line of output, skipping any processes which were not executed by an absolute path (i.e. "/bin/ps" instead of "ps").</li>
<li>Parse the directory from these processes and if the directory matches the directory that we're looking for, kill the process using the process ID.</li>
</ol></p>
<p>
There are a number of approaches to doing this, of varying difficulty and requiring various tools:
<ul><li>"sed" can be used with basic regular expressions to find lines containing a process that has been invoked with an absolute path and return the process ID and directory</li>
<li>"awk" is another utility which can do the same thing as above, and is probably a little easier to use if you have any programming experience</li>
<li>Bash is capable of doing the whole thing itself, but the best ways to do so would probably involve some fairly arcane applications. If you're really interested in this approach, check out Mendel Cooper's Advanced Bash Scripting Guide</li>
</li>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 25 2005
Added on Sep 21 2005
3 comments
1,276 views