Linux shell脚本实现磁盘管理实战
过程式编程语言的执行流程:
- 顺序执行
 - 选择执行
 - 循环执行
 
选择执行:
- (1) &&, ||
 - (2) if语句
 - (3) case语句
 
shell 编程之 if 语句详解
if语句:三种格式
单分支的if语句
if CONDITION; then if-true-分支; fi
双分支的if语句
if  CONDITION; then
    if-true-分支
else
    if-false-分支
fi
多分支的if语句
if  CONDITION1; then
    条件1为真分支
elif  CONDITION2; then
    条件2为真分支
elif  CONDITION3; then
    条件3为真分支
...
elif  CONDITIONn; then
    条件n为真分支
else
    所有条件均不满足时的分支
fi
注意:即便多个条件可能同时都能满足,分支只会执行中其中一个,首先测试为“真”;
示例:脚本参数传递一个文件路径给脚本,判断此文件的类型;
#!/bin/bash
#
if [ $# -lt 1 ]; then
    echo "At least on path."
    exit 1
fi
if ! [ -e $1 ]; then
    echo "No such file."
    exit 2
fi
if [ -f $1 ]; then
    echo "Common file."
elif [ -d $1 ]; then
    echo "Directory."
elif [ -L $1 ]; then
    echo "Symbolic link."
elif [ -b $1 ]; then
    echo "block special file."
elif [ -c $1 ]; then
    echo "character special file."
elif [ -S $1 ]; then
    echo "Socket file."
else
    echo "Unkown."
fi	
练习:写一个脚本
- (1) 传递一个参数给脚本,此参数为用户名;
 - (2) 根据其ID号来判断用户类型:
 - 0: 管理员
 - 1-999:系统用户
 - 1000+:登录用户
 
#!/bin/bash
#
[ $# -lt 1 ] && echo "At least on user name." && exit 1
! id $1 &> /dev/null && echo "No such user." && exit 2
userid=$(id -u $1)
if [ $userid -eq 0 ]; then
    echo "root"
elif [ $userid -ge 1000 ]; then
    echo "login user."
else
    echo "System user."
fi		
 
练习:写一个脚本
(1) 列出如下菜单给用户:
disk) show disks info; mem) show memory info; cpu) show cpu info; *) quit;
(2) 提示用户给出自己的选择,而后显示对应其选择的相应系统信息;
#!/bin/bash
#
cat << EOF
disk) show disks info
mem) show memory info
cpu) show cpu info
*) QUIT
EOF
read -p "Your choice: " option
if [[ "$option" == "disk" ]]; then
    fdisk -l /dev/[sh]d[a-z]
elif [[ "$option" == "mem" ]]; then
    free -m
elif [[ "$option" == "cpu" ]];then
    lscpu
else
    echo "Unkown option."
    exit 3
fi
shell 编程之 for 语句详解
循环执行: 将一段代码重复执行0、1或多次;
进入条件:条件满足时才进入循环;
退出条件:每个循环都应该有退出条件,以有机会退出循环;
bash脚本:
- for循环
 - while循环
 - until循环
 
for循环:
两种格式:
- (1) 遍历列表
 - (2) 控制变量
 
遍历列表:
- 进入条件:只要列表有元素,即可进入循环;
 - 退出条件:列表中的元素遍历完成;
 
for  VARAIBLE  in  LIST; do
    循环体
done
LISTT的生成方式:
- (1) 直接给出;
 - (2) 整数列表
 - (a) {start..end}
 - (b) seq [start [incremtal]] last
 - (3) 返回列表的命令
 - (4) glob
 - (5) 变量引用
 - $@, $*
 - ...
 
简单小案例
#!/bin/bash
#
for username in user21 user22 user23; do
    if id $username &> /dev/null; then
        echo "$username exists."
    else
        useradd $username && echo "Add user $username finished."
    fi
done
示例:求100以内所有正整数之和;
#!/bin/bash
#
declare -i sum=0
for i in {1..100}; do
    echo "\$sum is $sum, \$i is $i"
    sum=$[$sum+$i]
done
echo $sum
示例:判断/var/log目录下的每一个文件的内容类型
#!/bin/bash
#
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

                
            
        
浙公网安备 33010602011771号