Shell dead Loop

Source: Internet
Author: User

Example 1: After running the script, the Firefox browser is loaded automatically and the specified Web page is opened. If the user closes Firefox, the script will automatically re-open Firefox again.
To end the loop, abort the t2.sh process. Be careful not to execute two t1.sh scripts at the same time, otherwise ...
If you do not know the script name beforehand, you can also use the "Pstree | grep Firefox command learns its process tree and finds its parent process name killall out.
Or use a similar to "ps-elf | grep Firefox | Grep-v grep|  awk ' {print $} ' command to find the parent process PID of the Firefox-related process, choose the smallest one to kill. File name: t1.sh
Test method: Execute SH t1.sh &
File contents:
#!/bin/bash
While True
Do
/usr/bin/firefox http://www.baidu.com
Wait
Done ###########################################################################
Example 2: After running the script will check, correct the eth0 network card IP settings and other parameters, and check whether the Web program (80 port) is listening normally, otherwise automatically load the default httpd service. Check every 5 minutes later.
File name: t2.sh
Test method: Sh t2.sh &
File contents:
#!/bin/bash
While True
Do
nowip= '/sbin/ifconfig eth0 | grep ' inet addr ' | Cut-d:-f2 | awk ' {print '} '
if [$NOWIP! = ' 192.168.2.20 ']; Then
/sbin/ifconfig eth0 192.168.2.20/24
/sbin/ifconfig eth0 up
/sbin/route Add default GW 192.168.2.1
Echo-e ' nameserver 192.168.2.1\nnameserver 202.106.0.20 ' >/etc/resolv.conf
Fi
Pnum= ' NETSTAT-ANPTL | grep:80 | Wc-l '
If [$PNUM-eq 0]; Then
/ETC/INIT.D/HTTPD start
Fi
Sleep 5m
Done

Add:
While statement
Grammar:
While command/condition
Do
Statement
Done

Mechanism: If the command after the while is executed successfully, or if the condition is true, the statement between do and done is executed, and after execution completes, the command and condition after the while is judged again; if the command execution fails after the while, or the condition is false, the loop ends

For example:
While date
Do
echo "AA"
Done

Special case: Unconditional circulation
While True
Do
Statement
Done

While can be used as an infinite loop, many places use an infinite loop. An infinite loop is as follows:
while (true) {
Cyclic content;
...
}
An infinite loop can be ended by a conditional formula in its own loop. The following is an example of a loop internal termination:
while (true) {
Statement
if (conditional)
Break Jump off Loop
...
}

Http://www.cublog.cn/u1/40349/showart_392705.html
http://wanghai.blog.51cto.com/770518/160642
http://edu.codepub.com/2009/0531/5100.php

Shell dead Loop

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.