bash脚本编程
- 过程式编程语言的执行流程:
顺序执行
选择执行
循环执行
选择执行:
(1) &&, ||
(2) if语句
(3) case语句
- if语句:三种格式
单分支的if语句
双分支的if语句
多分支的if语句if CONDITION1; then 条件1为真分支 elif CONDITION2; then 条件2为真分支 elif CONDITION3; then 条件3为真分支 ... elif CONDITIONn; then 条件n为真分支 else 所有条件均不满足时的分支 fi
示例:
脚本参数传递一个文件路径给脚本,判断此文件的类型;
练习:写一个脚本
(1) 传递一个参数给脚本,此参数为用户名;
(2) 根据其ID号来判断用户类型:
0: 管理员
1-999:系统用户
1000+:登录用户
[root@localhost ~]# bash -x /scripts/testuid.sh
+ read -p 'input an user name:' uname
input an user name:gentoo
+ id gentoo
uid=4003(gentoo) gid=4003(gentoo) groups=4003(gentoo),5000(edu)
++ id gentoo
++ head -1
++ grep -o '[[:digit:]]*'
+ uid=4003
+ [[ 4003 -eq 0 ]]
+ [[ 4003 -lt 500 ]]
+ echo 'common user'
common user
[root@localhost ~]# cat /scripts/testuid.sh
#!/bin/bash
#
read -p 'input an user name:' uname
if id $uname ;then
uid=`id $uname | grep -o "[[:digit:]]*" | head -1`
else
exit 1
fi
if [[ $uid -eq 0 ]];then
echo 'root'
elif [[ $uid -lt 500 ]];then
echo 'system user'
else
echo 'common user'
fi
练习:写一个脚本
(1) 列出如下菜单给用户:
disk) show disks info;
mem) show memory info;
cpu) show cpu info;
*) quit;
(2) 提示用户给出自己的选择,而后显示对应其选择的相应系统信息;
[root@localhost ~]# bash /scripts/menu.sh
disk)show disks info
mem)show menory info
cpu)show cpu info
*)quit
mem
total used free shared buff/cache available
Mem: 1005656 139784 605724 7644 260148 679932
Swap: 2097148 0 2097148
[root@localhost ~]# cat /scripts/menu.sh
#!/bin/bash
#
echo 'disk)show disks info'
echo 'mem)show menory info'
echo 'cpu)show cpu info'
echo '*)quit'
read option
if [[ $option == 'disk' ]];then
fdisk -l
elif [[ $option == 'mem' ]];then
free
elif [[ $option == 'cpu' ]];then
lscpu
else
echo 'unkown'
exit 1
fi
循环执行: 将一段代码重复执行0、1或多次;
进入条件:条件满足时才进入循环;
退出条件:每个循环都应该有退出条件,以有机会退出循环;
bash脚本:
- for循环
- while循环
- until循环
for循环:
两种格式:
1.遍历列表
2.控制变量
-
遍历列表:
for VARAIBLE in LIST; do 循环体 done
进入条件:只要列表有元素,即可进入循环;
退出条件:列表中的元素遍历完成;LIST的生成方式:
- 直接给出;
- 整数列表
- {start..end}
- seq [start [increment]] last
- 可返回列表的命令 e.g. ls
- glob
- 变量引用
- $@, $*
...
示例:求100以内所有正整数之和;
[root@localhost ~]# bash /scripts/sum100.sh
5050
[root@localhost ~]# cat /scripts/sum100.sh
#!/bin/bash
declare -i sum=0
for i in {1..100} ; do
let sum+=i
done
echo $sum
示例:判断/var/log目录下的每一个文件的内容类型
[root@localhost ~]# bash /scripts/varlog.sh
/var/log/anaconda : directory
/var/log/audit : directory
/var/log/boot.log : empty
/var/log/boot.log-20190119 : ASCII text, with CRLF line terminators, with escape sequences
...
/var/log/vmware-vmsvc.log : ASCII text
/var/log/wtmp : data
/var/log/yum.log : ASCII text
[root@localhost ~]# cat /scripts/varlog.sh
#!/bin/bash
for i in /var/log/* ;do
echo "$i : `file -b $i`"
# echo "$i"
done
练习:
1、分别求100以内所有偶数之和,以及所有奇数之和;
[root@localhost ~]# bash /scripts/sumeven.sh
2550
[root@localhost ~]# cat /scripts/sumeven.sh
#!/bin/bash
declare -i sum=0
for i in `seq 2 2 100`;do
let sum+=i
done
echo $sum
2、计算当前系统上的所有用户的id之和;
[root@localhost ~]# bash /scripts/sumuid.sh
19620
[root@localhost ~]# cat /scripts/sumuid.sh
#!/bin/bash
declare -i sum=0
for i in `cat /etc/passwd | cut -d : -f3`;do
sum=$[$sum+$i]
done
echo $sum
3、通过脚本参数传递一个目录给脚本,而后计算此目录下所有文本文件的行数之和;并说明此类文件的总数;
[root@localhost ~]# bash /scripts/forexer.sh
input a directory path:/var/log
lines number is :86
text file total :55
[root@localhost ~]# cat /scripts/forexer.sh
#!/bin/bash
read -p 'input a directory path:' dirpath
declare -i sum=0
declare -i count=0
for filename in $dirpath/* ; do
if [[ -f $filename ]];then
let sum+=`wc -l $filename|grep -o '[[0-9]]*'`
let count+=1
fi
done
echo "lines number is :$sum"
echo "text file total :$count"
网友评论