shell脚本之判断 if,elif, case (二)
练习,写一个脚本,完成以下要求:
给定一个用户:
1、如果其UID为0,就显示此为管理员;
2、否则,就显示其为普通用户;
练习:写一个脚本,完成以下要求:
通过参数传递一个磁盘设备文件给脚本,让脚本来判断其是否有扩展分区;有则显示有,否则显示为无;
练习:写一个脚本
给定一个文件,比如/etc/inittab
判断这个文件中是否有空白行;
如果有,则显示其空白行数;否则,显示没有空白行。
练习:写一个脚本;(要求:不使用id命令获得其id号;)
给定一个用户,判断其UID与GID是否一样
如果一样,就显示此用户为“good guy”;否则,就显示此用户为“bad guy”。
练习:写一个脚本
给脚本传递两个参数(整数);
显示此两者之和,之乘积;
写一个脚本,分别显示当前系统上所有默认shell为bash的用户和默认shell为/sbin/nologin的用户,并统计各类shell下的用户总数。显示结果形如:
BASH,3users,they are:
root,redhat,gentoo
NOLOGIN, 2users, they are:
bin,ftp
bash脚本知识点:
条件测试方式:
bash命令;
[ expression ]
[[ expression ]]
test expression
条件测试:
整数测试
大于:-gt
小于:-lt
等于:-eq
大等:-ge
小等:-le
不等:-ne
字符测试
文件测试
命令执行状态返回值: 0-255
0: 正确执行
1-255: 错误执行
exit [n]
脚本执行的最后一条件命令的状态返回值;
bash字符测试:
>: 大于
<: 小于
==: 等于
=~: 判断左边的字符串是否能够被右边的模式所匹配;通常用于[[]];
[[ $opt1 =~ $opt2 ]]
一般做行首、行尾锚定;不要加引号;
单目:
-z $STRING: 为空则为真,不空则为假;
-n $STRING: 为空则为假,不空则真;
例子:写一个脚本,判定用户的shell是否为bash;
[ "$Shell" == "/bin/bash" ]
#!/bin/bash
#
Shell=`grep "^$1:" /etc/passwd | cut -d: -f7`
if [ "$Shell" == "/bin/bash" ]; then
echo "Bash User."
Ret=0
else
echo "Not Bash User."
Ret=9
fi
exit $Ret
改进版:
#!/bin/bash
#
Shell=`grep "^$1:" /etc/passwd | cut -d: -f7`
if [ -z $Shell ]; then
echo "No such user or User's shell is null."
exit 10
fi
if [ "$Shell" == "/bin/bash" ]; then
echo "Bash User."
Ret=0
else
echo "Not Bash User."
Ret=9
fi
exit $Ret
例子:根据用户shell的结束符是否为sh来判定其是否为登录用户:
#!/bin/bash
#
Shell=`grep "^$1:" /etc/passwd | cut -d: -f7`
if [ -z $Shell ]; then
echo "No shell."
exit 3
fi
if [[ "$Shell" =~ sh$ ]]; then
echo "Login User."
Ret=0
else
echo "None Login User."
Ret=4
fi
exit $Ret
写一个脚本:
判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor id一行中。
如果其生产商为GenuineIntel,就显示其为Intel公司;
否则,就显示其为AMD公司;
#!/bin/bash
#
Vendor=`grep "vendor_id" /proc/cpuinfo | uniq | cut -d: -f2`
if [[ "$Vendor" =~ [[:space:]]*GenuineIntel$ ]]; then
echo "Intel"
else
echo "AMD"
fi
练习:写一个脚本
通过参数传递一个字符串给脚本,如果传递的字符串为“memory”或“Memory”,就以MB为单位显示当前主机的内存信息;
否则,就显示/proc/uptime文件的内容。
#!/bin/bash
#
if [[ $1 =~ [Mm]emory$ ]]; then
free -m
else
cat /proc/uptime
fi
bash知识点:组合条件测试
-a: 与
-o: 或
!: 非,单目操作符
A=3
UID>=1, UID<=499
bash测试:
bash命令
[]
[[]]
test
bash命令组合测试:
&&: 与
||: 或
!: 非
写一脚本,给定用户,如果其不存在,就退出脚本。
if ! id $1 &> /dev/null; then
echo "No such user."
exit 6
fi
if [ `id -u $1` -eq `id -g $1` ]; then
echo "Good Guy"
else
echo "Bad Guy"
fi
练习:写一个脚本
通过参数传递一个字符串给脚本,如果传递的字符串为“memory”或“Memory”,就以MB为单位显示当前主机的内存信息;
否则,就显示/proc/uptime文件的内容。
[ $1 == "memory" -o $1 == "Memory" ]
bash条件判断之多分支if语句:
语法格式:
if 条件1; then
语句1
语句2
...
elif 条件2; then
语句1
语句2
...
elif 条件3; then
语句1
语句2
...
else
语句1
语句2
...
fi
写一个脚本:
判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor id一行中。
如果其生产商为GenuineIntel,就显示其为Intel公司;
如果其生产商为AuthenticAMD,就显示其为AMD公司;
否则,就显示无法识别;
#!/bin/bash
#
Vendor=`grep "vendor_id" /proc/cpuinfo | uniq | cut -d: -f2`
if [[ $Vendor =~ [[:space:]]*GenuineIntel$ ]]; then
echo "intel"
elif [[ $Vendor =~ [[:space:]]*AuthenticAMD$ ]]; then
echo "AMD"
else
echo "Unknown"
fi
练习:通过参数传递给脚本一个字符串,如Fedora, Gentoo, Redhat,判断Linux发行版所处理主流发行系列:
如果为fedora, centos, redhat,就显示RedHat;
[ $1 == fedora -o $1 == centos -o $1 == redhat ]
如果为suse, opensuse,就显示为SUSE;
如果为ubuntu, mint, debian,就显示为Debian;
否则,显示为其它或无法识别;
写一个脚本,其可以接受三个参数,最后一个参数为文件名,但参数可变化,形如:
script.sh -a MageEdu /magedu.com/scripts/test1.sh
script.sh -d 2013-07-19 /magedu.com/scripts/test1.sh
script.sh -D 'some infomation' /magedu.com/scripts/test1.sh
此脚本能够创建/magedu.com/scripts/test1.sh文件,并且,如果给出了-a MageEdu,则文件前两行为:
#!/bin/bash
# Author: MageEdu
如果给出了-d 2013-07-19,则文件前两行为:
#!/bin/bash
# Date: 2013-07-19
如果给出了-D "some infomation",则文件前两行为:
#!/bin/bash
# Description: some infomation
其它任何参数,均提示错误并退出;
进一步:如果没有退出,则使用vim打开此文件,并使用光标默认处于最后一行;
再进一步:保存退出后,如果文件有语法错误提示用户有错误;
更进一步:如果没有语法错误,则给些文件赋予执行权限;
a.sh -a mageedu /tmp/test.sh
#!/bin/bash
# Author: mageedu
a.sh -d 2013-07-19 /tmp/test.sh
#!/bin/bash
# Date: 2013-07-19
a.sh -D "test script" /tmp/test.sh
#!/bin/bash
# Description: test script
mkscript
#!/bin/bash
if [ $# -ne 3 ]; then
echo "the number of arguements is wrong."
exit 4
fi
echo '#!/bin/bash' >> $3
if [ $1 == '-a' ]; then
echo "# Author: $2" >> $3
elif [ $1 == '-d' ]; then
echo "# Date: $2" >> $3
elif [ $1 == '-D' ]; then
echo "# Description: $2" >> $3
else
echo "Unknown option, ignore it."
rm -f $3
exit 5
fi
vim + $3
if bash -n $3 &> /dev/null; then
chmod +x $3
else
echo "Syntax wrong in $3."
fi
bash测试之文件测试:
操作符 文件路径
-f: 测试其是否为普通文件,即ls -l时文件类型为-的文件;
-d: 测试其是否为目录文件,即ls -l时文件类型为d的文件;
-e: 测试文件是否存在;存在为真,否则为假;
-r: 测试文件对当前用户来说是否可读;
-w: 测试文件对当前用户来说是否可写;
-x: 测试文件对当前用户来说是否可执行;
-s: 测试文件大小是否不空,不空则真,空则假;
如果/tmp/test10不存在,就创建之;
if [ ! -e /tmp/test10 ]; then
mkdir /tmp/test10
fi
短路操作:只要前半段已经可以决定最终结果,后半段就不再运算;
与运算:
真 && 真 = 真
真 && 假 = 假
假 && {真|假} = 假
或运算:
假 || 0 = 0
假 || 1 = 1
真 || =1
[ -e /tmp/test10 ] || mkdir /tmp/test10
id $UserName &> /dev/null || useradd $UserName
! id $UserName &> /dev/null && useradd $UserName || echo "$UserName exists."
id $UserName &> /dev/null && echo "$UserName exists." || useradd $UserName
练习:判断当前Linux发行版是RedHat, Fedora, CentOS还是其它。
方法:取得/etc/issue文件第一行的第一个单词后进行比较;
bash如何与用户交互:bash内置命令, read
-p "prompt":提示信息
-t #: 超时秒数
例子:
#!/bin/bash
#
read -p "Do you agree [yes|no]?: " YesNo
case $YesNo in
y|Y|[Yy]es)
echo "Agreed, proceed." ;;
n|N|[nN]o)
echo "Disagreed, can't proceed." ;;
*)
echo "Invalid input." ;;
esac
例子:写一个脚本
1、显示如下菜单给用户:
m|M) show memory usages;
d|D) show disk usages;
q|Q) quit
2、如果用户选择了第一项,则显示内存使用信息;
如果选择了第二项,则显示磁盘挂载及使用相关信息;
如果是第三项,退出,并显示选择退出;
其它任何内容,均说明错误选项;
#!/bin/bash
cat << EOF
m|M) show memory usages;
d|D) show disk usages;
q|Q) quit
EOF
read -p "Your choice: " Choice
case $Choice in
m|M)
free -m ;;
d|D)
df -lh ;;
q|Q)
echo "Quit..."
exit 0
;;
*)
echo "Invalid input."
exit 5
;;
esac
blog comments powered by Disqus