前言
暂无
for
for 定义变量
do 使用变量,执行动作
done 结束标志
注意:for循环结束后,变量才能传递到外面
-
格式1:
#!/bin/bash for WESTOS in `seq 2 2 10`(步长为2,从2到10) do echo $WESTOS done
-
格式2:
for WESTOS in 1 2 3 do echo $WESTOS done
-
格式3:
for WESTOS in {10..1} do echo $WESTOS done
-
格式4:
for ((WESTOS=0;WESTOS<10;WESTOS++)) do echo $WESTOS done
注意:两个括号的作用,神奇
[root@rhel7_node1 mnt]# a=1+1
[root@rhel7_node1 mnt]# echo $a
1+1
[root@rhel7_node1 mnt]# ((a=1+1))
[root@rhel7_node1 mnt]# echo $a
2
脚本练习:
check_host.sh
用此脚本检测10台与您当前主机直连主机是否网络通常
如果网络通畅请显示主机的ip列表
while
while ture (条件为真)
do (条件成立所作循环动作)
done
until
until false (条件为假)
do(条件不成立所作循环动作)
done
if(用来做多次判定,一旦匹配,之后的都不再执行)
if
then
elif
then
…
else
fi
脚本练习:
check_file.sh
please input filename: file
file is not exist
file is file
file is direcory
此脚本会一直询问直到用户输入exit为止
case(点名)
case $1 in
word1|WORD1)
action1
;;
word2|WORD2)
action2
;;
*)
action3
esac
脚本练习:
system_watch.sh disk memory upload (每秒显示)
disk 监控磁盘使用情况(df)
memory 监控内存使用情况(free)
upload 监控启动负载(uptime)
expect(自动应答语句)
问题脚本
#!/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 KaTeX parse error: Expected '}', got 'EOF' at end of input: …"name" { send "NAME\r”;exp_continue }
“old” { send “KaTeX parse error: Can't use function '\r' in math mode at position 4: AGE\̲r̲";exp_continue …OBJ\r”;exp_continue }
“ok” { send “$FEEL\r” }
}
expect eof
脚本练习:
auto_ssh 192.168.0.1 westos
可以自动连接目标主机当目标主机网络不通时报错
break,continue,exit
contiue ##终止当此次前循环提前进入下个循环
break ##终止当前所在语句所有动作进行语句外的其他动作
exit ##脚本退出
后记
找时间练习