Results 1 to 1 of 1
-
9th Feb 2012, 10:56 AM #1OPMemberWebsite's:
AVANETCO.COM AVAVPS.COMAdvanced Bash Loops Tutorial
The BASH scripting language provides a very handy ?loop? features. In the previous tutorial page we have discussed that we can chain certain commands such as:
strace -p `pgrep -u root sshd`
This command however, provided that pgrep returns more than 1 process, will result in an error, since strace does not expect more than 1 process at a time. To overcome this issue, we can edit the line and add a loop control structure:
for i in `pgrep -u root sshd`; do strace -p $i ; done
What the above line will do is to create a for loop and for each result of pgrep to run strace -p.
Important Please note that you can not manipulate the system processes (executing ps, kill, pgrep, top and strace) on the shared server, since your account will not have such privileges. However, on our dedicated servers you will have root permissions and you will be able to control the system processes. Please check our dedicated servers offers for more details.
Another example would be to use the same for statement to remove a certain line in each file that contains it. Let?s imagine that you have hundreds of files containing a footer with copyright message with wrong year (i.e 1998 for example) and you need to remove it. The problem is the files are numerous and it will take a lot of time to do it by hand. Fortunately, you can use a single line in BASH to do it for you.
For the purpose, we will create a for loop, which will grep the whole directory and its subdirectories for the message we need to remove and pass it to a SED command which will actually process the file and remove it. The full command line will look like this:
for i in `grep * -r -l Copyrighted`; do sed '/Copyrighted/d' -i $i ; done
The line broken in parts is as follows:
1)for loop: for i in something; do something; done;
2)grep * -r -l Copyrighted -- greps the message and returns only the file names containing it
3)sed ?/Copyrighted/d? -i $i -- uses sed to remove the line from the files we have located with the above command.Albert.Nawaro Reviewed by Albert.Nawaro on . Advanced Bash Loops Tutorial The BASH scripting language provides a very handy ?loop? features. In the previous tutorial page we have discussed that we can chain certain commands such as: strace -p `pgrep -u root sshd` This command however, provided that pgrep returns more than 1 process, will result in an error, since strace does not expect more than 1 process at a time. To overcome this issue, we can edit the line and add a loop control structure: for i in `pgrep -u root sshd`; do strace -p $i ; done Rating: 5
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Tutorial [Creating a PHP Framework] {advanced} (PART 3)
By litewarez in forum Tutorials and GuidesReplies: 1Last Post: 4th Feb 2010, 02:12 AM -
Tutorial [Creating a PHP Framework] {advanced} (PART 2)
By litewarez in forum Tutorials and GuidesReplies: 2Last Post: 3rd Feb 2010, 05:37 AM -
Tutorial [Creating a PHP Framework] {advanced} (PART 1)
By litewarez in forum Tutorials and GuidesReplies: 1Last Post: 30th Jan 2010, 03:37 PM -
PHP Tutorial (Loopy Loops) Basics
By litewarez in forum Tutorials and GuidesReplies: 0Last Post: 12th May 2009, 12:53 PM
themaCreator - create posts from...
Version 3.24 released. Open older version (or...