table of Contents
Practical case: Detect the survival status of hosts on the intranet
Shell loop statement (for, while)
Detect the live state of the host in the intranet (optimized version)
Analyze the apache self-starting script
script
What is a script? effect?
The script in Linux is actually similar to the batch processing we write in the windows system, so how do we learn it?
Execute from top to next, if there is an error, it will not be executed, skip to the next command
Here we mainly deal with the requirements of various scenarios and write the corresponding scripts to achieve the process, we go step by step to understand the various syntax of script writing. The meaning of these grammars has actually been understood in the programming languages we have learned before. Here, we mainly need to understand and study its grammatical structure. You will find that it is very easy and interesting to learn...
A simple script
The scene settings are as follows:
Request to set the eth0 network card ip at 9 a.m.
IP:192.168.1.100/24
Gateway: 192.168.1.254
Request to set eth0 network card ip at 2pm
IP:172.16.1.100/24
Gateway: 172.16.1.254
Implementation process:
Stupid way:
①先把ifcfg-eth0文件清空:echo "" > /etc/sysconfig/network-scripts/ifcfg-eth0
②echo "DEVICE=eth0" >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo "TYPE=Ethernet" >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo "BOOYPROTO=static" >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo "IPADDR=192.168.1.100" >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo "NETMASK=255.255.255.0" >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo "GATEWAY=192.168.1.254" >> /etc/sysconfig/network-scripts/ifcfg-eth0
③先关了:ifdown eth0,再开启:ifup eth0I have to lose again in the afternoon and change to 172
Use script:
①Create a file vim fixip.sh first (because you want to modify the ip function, see the name zhiyi , the suffix is sh, because it is written in a shell script)
②The first line of the script is to declare what command interpreter the script is used for To run, such as sh command interpreter, write the location of sh command interpreter (/bin/sh), if it is bash, write (/bin/bash), the
second line and the third line at the beginning of the pound sign are also pound signs At the beginning, write down the role of the script, the creation time, and contact information (you don’t need to write it)
③How does it work? First give it an execution permission
If it turns green, it means that you have the permission to
execute the script. The method of running the script: if it is in the same directory, use ./ to run—— "./fixup.sh
The company will randomly assign different ips to modify
The optimized script is an interactive script, which leads to the variable assignment
100.1.1.100/24
10.1.1.254 that we will talk about below.
Please enter the ip address:
please enter the subnet mask:
please enter the gateway:
Variable assignment
A, as the name implies, is the variable we defined. We assign a value to it, and then call out the value (string) it represents through $A
[root@Waffle Desktop]# read -p "please input ipaddr:" IP
(大写IP说明这个东西是变量,please input ipaddr是显示内容,显示的内容输入完后就存在变量里面了)
please input ipaddr:10.1.1.1
[root@Waffle Desktop]# echo $IP
10.1.1.1
Optimize the script:
write read -p "please input ipaddr:" IP on the second line of the script just written
IF judgment statement
if single branch structure
if condition
then holds sub-statement
fi (to write fi at the end)
Case: Determine whether 3 is less than 5
Note [ Please write a space after it, otherwise an error
if double branch structure
if condition
then sub-statement
else does not hold sub-statement
be
Case: Determine if 6 is less than 5
There is no requirement for indentation in linux
if multi-branch structure
if condition
then sub-statement
elif condition
then sub-statement
else does not hold sub-statement
be
Case: Determine the relationship between the number entered on the keyboard and 10
Note: There must be a space before and after [
In Linux, greater than -gt is less than -lt equal to -eq
Practical case: Detect the survival status of hosts on the intranet
Our usual ping operation, we can modify it and change its specific output display:
-c specifies how many packets it sends
-i sending interval time (s)
-W wait for response time (s)
[root@Waffle Desktop]# ping -c2 -i0.2 -W2 12.34.56.79 (to write together, send two packets, the interval time is 0.2s, the waiting time is 2s)
[root@Waffle Desktop]# ping -c2 -i0.2 -W2 12.34.56.79 &> /dev/null (It means to export all the output whether correct or wrong to /dev/null, this file is equivalent to a hole )
Add two dots before and after (on the left side of numeric keypad 1, different from single quotation marks)
optimization:
If you want to test the survival of hosts on a network segment, it is not very useful
Shell loop statement (for, while)
for loops according to the value list
while loops according to the conditions
for loop
Format:
for variable in value list
do
sub-statement
done
Case: How to use for loop to realize 1~10 output on the screen?
while loop
while conditional
do
sub-statement
done
Case: Output 1~3 on the screen
Detect the live state of the host in the intranet (optimized version)
Continuing, we return to the previous detection of host survival. This time we need to detect the host survival status of the LAN in a real environment.
Configure our network properties to bridge mode
1. Set the eth0 network card of centos6-1 to bridge mode
2. Modify the network card configuration
[root@Waffle Desktop]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
3. Write a script
#!/bin/bash
NET=10.0.110.
for i in {1..254}
do
if `ping -c2 -i0.2 -w2 $NET$IP &> /dev/null`
then echo -e "$ NET$IP is \033[31mup\033[0m" is to change UP to red and DOWN to green
else echo -e "$NET$IP is \033[32mdown\033[0m"
fi
done
Look at the while loop
#!/bin/bash
NET=10.1.1.
IP=200
while [ $IP -lt 254 ]do
let IP=IP+1
if `ping -c2 -i0.2 -w2 $NET$IP &> /dev/null`
then echo -e "$NET$IP is \033[31mup\033[0m"
else echo -e "$NET$IP is \033[32mdown\033[0m"
fi
done
case statement
case
Case study
./case.sh centos
redhat./case.sh redhat
centos./cash.sh xxx
usage case.sh {redhat|centos}
#!/bin/bash
case $1 in
redhat)
echo "centos"
;;
centos)
echo "redhat"
;;
*)--》*表示所有
echo "useage $0 {redhat|centos}"
esac
Execution after empowerment
case format
case variable in
mode 1)
sub-statement
;;
*)
sub-statement
;;
esac
function
Store part of the code in a variable and
then execute this function when you call it
Case: Design a function named A, and the screen will output OK when running A
The return value of the function can be obtained by echo $? (return)
redhat(){ echo cetos return 0 } centos(){ echo redhat return 0 } case $1 in redhat) redhat (equivalent to calling the above function) ;; centos) centos ;; *) echo "Usage $0 {redhat|centos }" esac
Analyze the apache self-starting script
apache start function
apache's shutdown function
Do these look familiar, they are all written by what we learned earlier
Next, we will write a self-starting script for nginx
Nginx self-starting script
First, we have to write our Nginx startup script nginxd in the /etc/init.d/ directory