目录

6.9 脚本示例与练习

脚本示例与练习

/images/linux_mt/linux_mt.jpg

本节是一些常用的脚本示例,可供我们学习参考

1. 条件判断练习

更改主机名

将当前主机名称保存至hostName变量中;主机名如果为空,或者为localhost.localdomain,则将其设置为www.magedu.com;

1
2
> hostName=$(hostname)
> [ -z "$hostName" -o "$hostName" == "localhost.localdomain" -o "$hostName" == "localhost" ] && hostname www.magedu.com

比较求大者

通过命令行参数给定两个数字,输出其中较大的数值;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/bin/bash
#
if [ $# -lt 2 ]; then
    echo "Two integers."
    exit 2
fi

declare -i max=$1

if [ $1 -lt $2 ]; then
    max=$2
fi

echo "Max number: $max."

2. 命令行参数

判断用户ID 奇偶

通过命令行参数给定一个用户名,判断其ID号是偶数还是奇数;

比较文件行数

通过命令行参数给定两个文本文件名,如果某文件不存在,则结束脚本执行;都存在时返回每个文件的行数,并说明其中行数较多的文件;

3. for 循环练习

添加用户

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/bin/bash
#
for username in user21 user22 user23; do
    if id $username &> /dev/null; then
        echo "$username exists."
    else
        useradd $username
        if [ $? eq 0 ]; then
            echo "$username" | passwd --stdin "$username" &> /dev/null
            echo "Add $username finished"
        fi
    fi
done

求和

1
2
3
4
5
6
7
8
9
#!/bin/bash
# 示例:求100以内所有正整数之和;
declare -i sum=0

for i in {1..100}; do
    echo "\$sum is $sum, \$i is $i"
    sum=$[$sum+$i]
done
echo $sum

判断文件类型

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash
# 示例:判断/var/log目录下的每一个文件的内容类型
for filename in /var/log/*; do
    if [ -f $filename ]; then
        echo "Common file."
    elif [ -d $filename ]; then
        echo "Directory."
    elif [ -L $filename ]; then
        echo "Symbolic link."
    elif [ -b $filename ]; then
        echo "block special file."
    elif [ -c $filename ]; then
        echo "character special file."
    elif [ -S $filename ]; then
        echo "Socket file."
    else
        echo "Unkown."
    fi                    
done
1
2
3
4
5
6
7
8
#!/bin/bash
# 打印成法口诀表
for i in {1..9}; do
    for j in $(seq 1 $i); do
        echo -e -n "${i}X${j}=$[$i*$j]\t"
    done
    echo
done

4. 类 C 风格for 循环

求和

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 示例:求100以内所有正整数之和
#!/bin/bash
#
declare -i sum=0

for ((i=1;i<=100;i++)); do
    let sum+=$i
done

echo "Sum: $sum."

打印九九乘法表

1
2
3
4
5
6
7
8
9
# 示例:打印九九乘法表
#!/bin/bash
#
for ((j=1;j<=9;j++)); do
    for ((i=1;i<=j;i++)); do
        echo -e -n "${i}X${j}=$[${i}*${j}]\t"
    done
    echo
done                

5. 显示一个菜单给用户

要求

1
2
3
4
5
6
7
8
# 显示一个如下的菜单给用户
# cpu) display cpu information
# mem) display memory information
# disk) display disks information
# quit) quit

# 要求:(1) 提示用户给出自己的选择;
#      (2) 正确的选择则给出相应的信息;否则,则提示重新选择正确的选项;

bash 脚本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/bash
#
cat << EOF
cpu) display cpu information
mem) display memory infomation
disk) display disks information
quit) quit
===============================
EOF

read -p "Enter your option: " option

while [ "$option" != "cpu" -a "$option" != "mem" -a "$option" != "disk" -a "$option" != "quit" ]; do
    echo "cpu, mem, disk, quit"
    read -p "Enter your option again: " option
done

if [ "$option" == "cpu" ]; then
    lscpu
elif [ "$option" == "mem" ]; then
    free -m
elif [ "$option" == "disk" ]; then
    fdisk -l /dev/[hs]d[a-z]
else
    echo "quit"
    exit 0
fi        

6. 服务框架脚本

要求

1
2
3
4
5
# 示例:写一个服务框架脚本;
# $lockfile,  值/var/lock/subsys/SCRIPT_NAME
# (1) 此脚本可接受start, stop, restart, status四个参数之一;
# (2) 如果参数非此四者,则提示使用帮助后退出;
# (3) start,则创建lockfile,并显示启动;stop,则删除lockfile,并显示停止;restart,则先删除此文件再创建此文件,而后显示重启完成;status,如果lockfile存在,则显示running,否则,则显示为stopped.

bash 脚本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
#
# chkconfig: - 50 50
# description: test service script
#
prog=$(basename $0)
lockfile=/var/lock/subsys/$prog

case $1  in
start)
    if [ -f $lockfile ]; then
        echo "$prog is running yet."
    else
        touch $lockfile
        [ $? -eq 0 ] && echo "start $prog finshed."
    fi
    ;;
stop)
    if [ -f $lockfile ]; then
        rm -f $lockfile
        [ $? -eq 0 ] && echo "stop $prog finished."
    else
        echo "$prog is not running."
    fi
    ;;
restart)
    if [ -f $lockfile ]; then
        rm -f $lockfile
        touch $lockfile
        echo "restart $prog finished."
    else
        touch -f $lockfile
        echo "start $prog finished."
    fi
    ;;
status)
    if [ -f $lockfile ]; then
        echo "$prog is running"
    else
        echo "$prog is stopped."
    fi
    ;;
*)
    echo "Usage: $prog {start|stop|restart|status}"
    exit 1
esac