1.for语句
for
do
done
循环语句
示例:
[root@localhost mnt]# vim file.sh
#!/bin/bash
for num in {1..5}
do
if
[ "$num" -eq 3 ]
then
$1
fi
echo $num
done
echo hello world
[root@localhost mnt]# sh file.sh
[root@localhost mnt]# sh file.sh exit #执行到3后退出(直接退出整个脚本的执行)
[root@localhost mnt]# sh file.sh break #执行到3后,退出for语句(还会执行脚本中这个for语句外的内容)
[root@localhost mnt]# sh file.sh continue #跳过3后继续执行
脚本内容:
for num in {1..5}
for num in seq 1 5
for num in seq 1 2 5
#间隔为2
[root@localhost mnt]# vim file.sh
#!/bin/bash
for num in `{1..5}`
do
echo $num
done
[root@localhost mnt]# sh file.sh
脚本内容:
执行结果
[root@localhost mnt]# vim file.sh
#!/bin/bash
for num in `seq 1 5`
do
echo $num
done
[root@localhost mnt]# sh file.sh
脚本内容:
[root@localhost mnt]# vim file.sh
#!/bin/bash
for num in `seq 1 2 5`
do
echo $num
done
[root@localhost mnt]# sh file.sh
脚本内容:
例子:将数据库中除schema结尾的其他数据库备份,文件名为“数据库名称.sql”
[root@localhost mnt]# vim mysql_dump.sh
#!/bin/bash
DATABASE_MESSAGE=`mysql -uroot -EN -e "show databases;"|grep -E "^\*|schema$" -v`
mkdir -p /mnt/mysql_dump
for DATABASE_NAME in $DATABASE_MESSAGE
do
mysqldump -uroot $DATABASE_NAME > /mnt/mysql_dump/${DATABASE_NAME}.sql
[ "$?" -eq "0" ]&&{
echo -e "\033[32m$DATABASE_NAME is backuped!!\033[0m"
}
done
[root@localhost mnt]# sh mysql_dump.sh
执行:
脚本内容:
2.while语句
while
do
done
例1:
[root@localhost mnt]# vim test.sh
#!/bin/bash
while true
do
echo -n `uptime`
echo -ne "\r \r"
sleep 1
done
[root@localhost mnt]# sh test.sh
^C :28 up 4:28, 2 users, load average: 0.01, 0.02, 0.05
脚本内容:
例2:当/分区超过80%发送邮件提醒
[root@localhost mnt]# dd if=/dev/zero of=/bigfile bs=1M count=5000
[root@localhost mnt]# vim file.sh
#!/bin/bash
DISK_NUM=`df -h | awk '/\/$/{print $5}' | awk -F "%" '{print $1}'`
TTY=`ps |awk '/bash$/{print $2}'`
while true
do
[ "$DISK_NUM" -ge "80" ]&&{
echo "Your / will full!!" > /dev/$TTY
}
sleep 3
done
[root@localhost mnt]# sh file.sh
Your / will full!!
Your / will full!!
脚本内容:
3.if语句
if
then
elif
then
else
fi
例1:if语句判断脚本后跟的第一串字符
[root@localhost mnt]# vim if.sh
#!/bin/bash
if
[ "$1" = a ]
then
echo '$1' is a
elif
[ "$1" = b ]
then
echo '$1' is b
elif
[ "$1" = c ]
then
echo '$1' is c
else
echo unkwon $1
fi
[root@localhost mnt]# sh if.sh a
$1 is a
[root@localhost mnt]# sh if.sh b
$1 is b
[root@localhost mnt]# sh if.sh c
$1 is c
[root@localhost mnt]# sh if.sh d
unkwon d
脚本内容:
执行结果:
例2:用if语句判断文件类型
[root@localhost mnt]# vim check_file.sh
#!/bin/bash
check_file()
{
if
[ "$1" "$2" ]
then
echo "$2" is $3
exit 0
fi
}
if
[ "$#" -ne "1" ]
then
echo "please input a file"
elif
[ ! -e "$1" ]
then
echo "$1" is not exist!!
else
check_file -L $1 link
check_file -f $1 "commen file"
check_file -b $1 block
check_file -S $1 socket
check_file -c $1 character
check_file -d $1 directory
fi
[root@localhost mnt]# sh check_file.sh /mnt/
/mnt/ is directory
[root@localhost mnt]# sh check_file.sh /var/lib/mysql/mysql.sock
/var/lib/mysql/mysql.sock is socket
[root@localhost mnt]# sh check_file.sh file
file is commen file
[root@localhost mnt]# sh check_file.sh file1
file1 is link
[root@localhost mnt]# sh check_file.sh /dev/vdb
/dev/vdb is block
[root@localhost mnt]# sh check_file.sh /dev/pts/0
/dev/pts/0 is character
脚本内容:
执行结果:
例3:建立用户,要求:文件数量不对报错;文件不存在报错;文件行数不匹配报错;用户存在显示用户存在但不改变密码;用户不存在建立用户并设定相应密码。
[root@localhost mnt]# vim user_create.sh
#!/bin/bash
if
[ "$#" -ne 2 ]
then
echo -e "\033[31mplease input userfile and passwordfile\033[0m"
elif
[ ! -e $1 ]
then
echo -e "\033[31m$1 is not exist!!\033[0m"
exit 1
elif
[ ! -e $2 ]
then
echo -e "\033[31m$2 is not exist!!\033[0m"
exit 1
elif
NUM1=`cat $1 2> /dev/null | wc -l`
NUM2=`cat $2 2> /dev/null | wc -l`
[ "$NUM1" -ne "$NUM2" ]
then
echo -e "\033[31merror:there are difference in the number of two file lines\033[0m"
else
MAX_LINE=`awk 'BEGIN{N=0}{N++}END{print N}' $1`
for LINE_NUM in `seq 1 $MAX_LINE`
do
USERNAME=`sed -n "${LINE_NUM}p" $1`
PASSWORD=`sed -n "${LINE_NUM}p" $2`
useradd $USERNAME &&{
echo $PASSWORD | passwd --stdin $USERNAME
}
done
fi
脚本内容:
执行结果:
4.case语句
case语句中可以不用一个一个命令往下判断,当识别到被判断的关键字时会跳过前面的判断,直接执行有关键字的那行命令
例:输入为cat输出dog,当输入为dog输出cat,输入其他时报错
[root@localhost mnt]# vim test.sh
#!/bin/bash
case $1 in
dog)
echo cat
;;
cat)
echo dog
;;
*)
echo error
esac
脚本内容:
执行结果
5.expect语句
expect是自动应答命令用于交互式命令的自动执行。
spawn是expect中的监控程序,其运行后会监控命令提出的交互问题。
send发送问题答案给交互命令。
.“\r”表示回车。
.exp_ continue标示当问题不存在时继续回答下面的问题。
.expect eof标示问题回答完毕退出expect环境。
.interact标示问题回答完毕留在交互界面。
.set NAME [ lindex $argvn]定义变量
在这里先说一下多行录入格式的脚本
例1:删除磁盘第二个分区
[root@localhost mnt]# vim fdisk.sh
#!/bin/bash
fdisk /dev/vdb <<EOF
d
2
wq
EOF
partprobe
脚本内容:
执行结果:
例2:判断是否有扩展分区:如果没有,建立扩展分区
[root@localhost mnt]# vim fdisk.sh
#!/bin/bash
DESK_MESSAGE=`fdisk -l | awk '/Extended/'`
if
[ -z "$DESK_MESSAGE" ]
then
fdisk /dev/vdb <<EOF
n
e
wq
EOF
partprobe
fi
脚本内容:
执行结果:
expect语句举例:
[root@localhost mnt]# vim ask.sh
#!/bin/bash
read -p "what's your name: " NAME
read -p "How old are you:" AGE
read -p "Which obj you study:" OBJ
read -p "Are you happy?" FEEL
echo "$NAME is $AGE years old and study $OBJ feel $FEEL"
[root@localhost mnt]# vim answer.exp
#!/usr/bin/expect
set timeout 2
spawn /mnt/ask.sh
expect "name"
send "lin\r"
expect "old"
send "18\r"
expect "study"
send "linux\r"
expect "happy"
send "happy\r"
expect eof
[root@localhost mnt]# expect anwser.exp
/mnt/ask.sh内容:
/mnt/anwser.exp内容:
执行结果:
当/mnt/ask.sh中少一个问题:
执行结果发生错行:
这时候需要重新编写/mnt/answer.exp
[root@localhost mnt]# vim answer.exp
#!/usr/bin/expect
set timeout 2
spawn /mnt/ask.sh
expect {
name: { send "lin\r";exp_continue }
old { send "18\r";exp_continue }
study { send "linux\r";exp_continue }
happy { send "happy\r" }
}
expect eof
[root@localhost mnt]# expect answer.exp
脚本内容:
执行结果:
如果要用sh执行脚本,要用到多行录入:
[root@localhost mnt]# vim answer.sh
#!/bin/bash
/usr/bin/expect <<EOF
set timeout 2
spawn /mnt/ask.sh
expect {
name: { send "lin\r";exp_continue }
old { send "18\r";exp_continue }
study { send "linux\r";exp_continue }
happy { send "happy\r" }
}
expect eof
EOF
[root@localhost mnt]# sh answer.sh
脚本内容:
执行结果:
6.脚本中的语句控制器
exit n #脚本退出,退出值为n
break #退出当前循环
continue # 提前结束循环内部的命令,但不终止循环
例:遇到4时跳过
[root@localhost mnt]# vim num2.sh
#!/bin/bash
for num in {1..10}
do
while [ "$num" -ne 4 ]
do
echo $num
break
done
done
[root@localhost mnt]# sh num2.sh
脚本内容:
执行结果: