1. Write the script, the requirements are as follows:
When writing a script, try to put the function definition at the beginning of the script, not at the end of the script, otherwise the function will not be read;
2. For statement:
结构:
for 定义变量
do 使用变量,执行动作
done 结束标志
格式1:
#!/bin/bash
for WESTOS in `seq 2 2 10` #开始为2,结束为10(包含10),部长是2;即10以内的偶数
do
echo $WESTOS
done
格式2:
for WESTOS in 1 2 3(westos linux lin)
do
echo $WESTOS
done
格式3:
for WESTOS in {
10..1} (从10到1)
do
echo $WESTOS
done
格式4:
for ((WESTOS=0;WESTOS<10;WESTOS++))
do
echo $WESTOS
done
Supplement: seq command: used to generate all integers from a certain number to another
Syntax:
seq [options]... mantissa
seq [options]... first number and mantissa
seq [options]... first number increment mantissa
Parameters:
-f, --format=format uses printf style floating-point format
-s, --separator=string uses the specified string to separate numbers (default use: \n)
-w, --equal-width in front of the column Add 0 to make the width the same
Script writing:
check_host.sh
Use this script to detect whether 10 hosts directly connected to your current host have a normal network.
If the network is normal, please show the IP list of the host
Script writing:
create_user_file.sh add file usernamefile add file userpasswd
and prompt if the user exists, create if not;
3. While statement and until statement:
while :
while ture #条件为真
do #条件成立所作循环动作
done
until :
until false ##条件为假
do
#条件不成立所作循环动作
done
In the execution block (do) of the while statement and the until statement, there needs to be a condition to control the variable, otherwise it will loop indefinitely: The
while statement outputs a 99 multiplication table:
The until statement outputs the 99 multiplication table:
4. If statement:
if
then
elif
then
...
else
fi
Script exercise:
check_file.sh
please input filename: file
file is not exist
file is
file is direcory
This script will keep asking until the user enters exit
The if statement will execute the judgment multiple times from top to bottom, until the statement that meets the conditions is judged (judgment mechanism, yes and no). The
case statement is judged only once (calling mechanism)
5.case statement:
case $1 in
word1|WORD1)
action1
;;
word2|WORD2)
action2
;;
*)
action3
esac
Script exercise:
system_watch.sh disk memory upload (displayed every second)
disk monitor disk usage
memory monitor memory usage
upload monitor startup load
6.expect statement:
问题脚本
#!/bin/bash
read -p "what's your name:" NAME
read -p "How old are you: " AGE
read -p "Which objective: " OBJ
read -p "Are you ok? " OK
echo $NAME is $AGE\'s old study $OBJ feel $OK
应答脚本
#!/usr/bin/expect
set timeout 1 ##脚本问答等待时间
set NAME [ lindex $argv 0 ] ##第一个字符
set AGE [ lindex $argv 1 ]
set OBJ [ lindex $argv 2 ]
set FEEL [ lindex $argv 3 ]
spawn /mnt/ask.sh
expect {
"name" {
send "$NAME\r";exp_continue }
"old" {
send "$AGE\r";exp_continue }
"objective" {
send "$OBJ\r";exp_continue }
"ok" {
send "$FEEL\r" }
}
expect eof
将应答脚本写入shell:
#!/bin/bash
echo hello westos
/usr/bin/expect <<EOF
spawn /mnt/ask.sh
expect {
"name" {
send "$1\r";exp_continue }
"old" {
send "$2\r";exp_continue }
"objective" {
send "$3\r";exp_continue }
"ok" {
send "$4\r" }
}
expext eof
Script exercise:
host_list.sh
detects whether the 172.25.254.1-172.25.254.10 network is open.
If it is open, the parse list is generated as follows:
ip hostname
such as: 172.25.254.1 westos_Student1.westos.org 7.break
, continue, exit controller:
"$? "Indicates the exit value, the correct result output or the exit value after exit is 0;
continue ##终止当此次前循环提前进入下个循环
break ##终止当前所在语句所有动作进行语句外的其他动作
exit ##脚本退出