LINUX Shell Detailed explanation of script programming examples ( One ) upper

ping Command to test the 2 Detailed explanation of examples

use linux Already 10 Next year, but it has been limited to the construction of various servers and the optimization of the environment , Ability cannot be improved , Today I suddenly decided to study Shell Script programming , Therefore, it is widely spread on the Internet 《Shell script 100 example 》 Basic learning , Experiments and notes .

Don't talk about chestnuts .

use vim shell100-scanip-1.sh Command to create a script file
#!/bin/bash for i in {1..254} do ping -c2 -i0.3 -W1 192.168.18.$i &>/dev/null
if [ $? -eq 0 ];then echo "192.168.18.$i is up" else echo "192.168.18.$i is
down" fi done
explain :

First sentence #!/bin/bash

stay shell There is often a sentence at the beginning of a script to define which one to use sh Interpreter to interpret scripts .
Currently, the shell There are two main ways in the script :
(1) #!/bin/sh
(2) #!/bin/bash

End of use sh still bash To explain the difference, take the time to write an article , It's not detailed here

The second sentence for i in {1..254}

            do

            ......

           done

It's a circular structure ,for Loop in a similar program for loop ,i Is a cyclic variable , Loop variable value in in{} Cycle in turn , Circulatory body from do start ,done end .

The third sentence ping -c2 -i0.3 -W1 192.168.18.$i &>/dev/null

ping Command parameters

-c2: Set the number of times to complete a request response ; Set here to 2 It is the first time to consider the problem of network routing ping Can't go back and the second time ping Situations that can be returned ( Please read the network principle for specific reasons )

-i0.3: Specify the time interval between sending and receiving information ( Second meter ), The time here is a comprehensive consideration ping Command return timeout time and each time ping Time of , The principle should be : Return timeout <i parameter <ping Command sending interval .

-W1: Time out ( Millisecond meter ), The timeout is 1 millisecond , In a millisecond ping End of command .

&>/dev/null
: Indicates standard output stdout And standard error output stderr Redirect to null Of devices , That is to say, it can't be printed on the screen , And they don't save files ( Put it under the name null Of devices —— The bottomless hole will disappear forever ).

The fourth judgment sentence

if [ $? -eq 0 ];then
           ......     
        else
         ......       
        fi

if Judgment statement [ ] Judgment expression in ,$? Indicates whether the last run was successful , Successful as 0 Unsuccessful 1

The fifth sentence , Sixth sentence

echo "192.168.18.$i is up",echo "192.168.18.$i is down" Indicates echo to screen $i Indicates current i Value of variable

Chestnut 1 Explain it , To be continued

 

Technology
©2019-2020 Toolsou All rights reserved,
One and a half years JAVA Summary of work experience Jsp+Ajax+Servlet+Mysql Add, delete, modify and query ( one ) cartoon | CPU Warfare 40 year , The real king finally appeared ! Don't annoy the panda with any cat !「 Kung Fu Panda 」20 It's the year of man 4 blood IAR Installation and use tutorial Classical algorithm - recursion ( The case of raw rabbit ) Thorough explanation from Zhongtai Random forest R Language implementation R Language cluster analysis case These songs , Programmers, don't listen !