bash编程之多分支if 语句及for循环

第十七章、bash编程之多分支if 语句及for循环

  • if语句三种格式
  • 多分支if语句练习
  • for循环

17.1、if语句的三种格式

单分支if语句

if condition;then
	条件为真执行的代码
fi

双分支if语句

if condition;then
	条件为真执行的代码
else
	条件为假执行的代码
fi

多分支if语句

if condition1;then
	condition1为真时执行的代码
elif condition2;then
	condition2为真时执行的代码
elif condition3;then
	condition3为真时执行的代码
...
elif condition10;then
	condition10为真时执行的代码
else
	条件都为假时执行的代码
fi
# 多分支的if语句elif可以出现n次;

注意:即便多个分支条件可能同时都能满足,分支只会执行其中一个,如果第一个条件满足则后续分支均不执行;

示例

# 练习、写一个脚本
# 1、列出如下菜单给用户
disk)show disk info.
mem)show memory info.
cpu)show cpu info.
*)QUIT
# 2、提示用户给出自己的选择,而后显示对应其选系的相应系统信息;
#!/bin/bash
# -*- coding: utf-8 -*-
# Filename: systeminfo.sh
# Author: buhui
# Date: 2016-12-29
# Description: show system status
cat <<EOF
disk)show disk info.
mem)show memory info.
cpu)show cpu info.
*)QUIT
EOF

read -p "Enter your choice: " option

if [[ $option == "disk" ]];then
    fdisk -l /dev/sd[a-z]
elif [[ $option == "mem" ]];then
    free -m
elif [[ $option == "cpu" ]];then
    lscpu
else
    echo "QUIT..."
    exit 1
fi

17.2、lscpu命令

查看当前服务器的CPU信息;

示例

[root@www scripts]# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    1
Core(s) per socket:    4
座:                 1
NUMA 节点:         1
厂商 ID:           GenuineIntel
CPU 系列:          6
型号:              42
型号名称:        Intel(R) Core(TM) i5-2410M CPU @ 2.30GHz
步进:              7
CPU MHz:             2294.860
BogoMIPS:            4589.72
超管理器厂商:  VMware
虚拟化类型:     完全
L1d 缓存:          32K
L1i 缓存:          32K
L2 缓存:           256K
L3 缓存:           3072K
NUMA 节点0 CPU:    0-3

17.3、for循环

将一段代码重复执行多次;

for循环语法格式

for VARIABLE in LIST;do
	循环体
done

LIST的生成方式

  1. 直接给出一个列表;例如:1 2 3 4 5

  2. 整数列表

    (a) {1..10}:通过命令行展开的方式生成;

    (b)使用seq命令生成

3.返回列表的命令;例如:ls /etc/

4.glob,通过模式匹配;例如:for i in /etc/*;do

5.变量引用;例如:$@, $*

示例

练习1、求100以内所有正整数之和;

#!/bin/bash
# -*- coding: utf-8 -*-
# Filename: sum.sh
# Author: buhui
# Date: 2016-12-29
# Description:

declare -i sum=0

for i in {1..100};do
    let sum=$sum+$i
done

echo "$sum"

练习2、判断/var/log目录下每个文件的内容类型

#!/bin/bash
# -*- coding: utf-8 -*-
# Filename: filetype.sh
# Author: buhui
# Date: 2016-12-29
# Description:

for file in /var/log/*;do
    if [ -f $file ];then
        echo "common file."
    elif [ -d $file ];then
        echo "directory file."
    elif [ -S $file ];then
        echo "socket file."
    elif [ -L $file ];then
        echo "softlink file."
    elif [ -b $file ];then
        echo "block file."
    else
        echo "unkown."
    fi
done

练习3、分别求100以内所有偶数之和,以及所有奇数之和;

#!/bin/bash
# -*- coding: utf-8 -*-
# Filename: sum2.sh
# Author: buhui
# Date: 2016-12-29
# Description:
declare -i even_num=0
declare -i uneven_num=0

for num1 in $(seq 1 2 100);do
    let uneven_num=$uneven_num+$num1
done

for num2 in $(seq 2 2 100);do
    let even_num=$even_num+$num2
done

echo "The sum of all the even for: $even_num"
echo "The sum of all the odd numbers: $uneven_num"

练习4、计算当前系统上所有用户id之和;

#!/bin/bash
# -*- coding: utf-8 -*-
# Filename: userid.sh
# Author: buhui
# Date: 2016-12-29
# Description:
declare -i sum=0

user_list=$(cut -d: -f3 /etc/passwd)

for uid in $user_list;do
    let sum=$sum+$uid
done

echo "All users uid sum for: $sum"

练习5、通过脚本参数传递一个目录给脚本,而后计算此目录下所有文本文件的行数之和;并说明此类文件的总数;

#!/bin/bash
# -*- coding: utf-8 -*-
# Filename: filelinesum.sh
# Author: buhui
# Date: 2016-12-29
# Description:
declare -i linesum=0

if [ $# -lt 1 ];then
	echo "at least one dirctory."
	exit 1
fi

if ! [ -d $1 ];then
	echo "Not a dirctory."
	exit 2
fi

path=$(echo $1|sed -n "s#\(\w\)/#\1#gp")
if [ -z $path ];then
	path=$1
fi
text_file_num=$(file $path/*|grep "text"|wc -l)

file_list=$(file $path/*|grep "text"|awk -F'[ :]' '{print $1}')
for i in $file_list;do
	temp=$(wc -l $i|cut -d' ' -f1)
	let linesum=$linesum+$temp
done
#
echo "text file line sum= $linesum"
echo "text file sum= $text_file_num"
posted @ 2016-12-29 21:31  EnZhiWang  阅读(1123)  评论(0编辑  收藏  举报