Linux SHELL脚本—02
分支判断结构
if, case
结构1)
if CONDITIOIN; then
statement
statement
fi
结构2)
if CONDITIOIN; then
statement
statement
else
statement
statement
fi
CONDITIOIN条件:
COMMAND
[ expression ]
编写脚本,由用户输入用户名,判断用户是否存在,如果不存在,就显示用户不存在,如果存在,以下面格式输出用户相关信息:
用户名:
宿主目录:
SHELL程序:
!/bin/bash
read -p "Enter user: " name
if id $name &> /dev/null; then
echo "用户名:$name"
homedir=grep "^$name:" /etc/passwd | awk -F: '{print $6}'
shname=grep "^$name:" /etc/passwd | awk -F: '{print $7}'
echo "宿主目录:$homedir"
echo "SHELL名称:$shname"
else
echo "用户$name不存在"
fi
编写脚本,判断文件中是否存在空行,有则显示空行个数,没有则显示文件内容,并在每一行前显示行号
!/bin/bash
read -p "请输入文件名称(Full Path): " file
if grep "^$" $file &> /dev/null; then
number=grep "^$" $file | wc -l
echo "文件$file中空行的数量:$number"
else
echo "文件$file内容如下:"
cat -n $file
fi
用法3: 多分支if结构
if CONDITION; then
statement
statement
elif CONDITION; then
statement
statement
elif CONDITIOIN; then
statement
statement
else
statement
statement
fi
多个条件的写法:
AND [ condition1 -a condition2 ] [ condition1 ] && [ condition2 ]
OR [ condition1 -o condition2 ] [ condition1 ] || [ condition2 ]
编写脚本, 判断当前系统时间的小时数字
9--11 morning
12--14 noon
15---18 afternoon
night
!/bin/bash
hour=date +%H
if [ $hour -ge 9 -a $hour -le 11 ]; then
echo "Morning"
elif [ $hour -ge 12 -a $hour -le 14 ]; then
echo "Noon"
elif [ $hour -ge 15 -a $hour -le 18 ]; then
echo "Afternoon"
else
echo "Night"
fi
[ expression ]
数学表达式
字符表达式
[ str1 == str2 ]
[ str1 != str2 ]
[ -z str1 ] 判断字符串是否为空的
!/bin/bash
read -p "请输入密码:" pwd1
read -p "请再次输入密码:" pwd2
if [ "$pwd1" == "$pwd2" ]; then
echo "密码设置成功"
else
echo "密码不一致,请重试"
fi
文件目录表达式
[ -e file ] 判断文件目录是否存在
[ -f file ] 判断是否为文件
[ -d file ] 判断是否为目录
[ -r file ]
[ -w file ]
[ -x file ]
双目表达式
单目表达式: [ -e file ] [ ! -e file ]
用法4: 嵌套if
if CONDITION; then
if CONDITION; then
statement
statement
else
statement
statement
fi
else
statement
statement
fi
!/bin/bash
read -p "Enter user: " name
if id $name &> /dev/null; then
获取UID,GID并进行判断
user_id=id -u $name
group_id=id -g $name
if [ $user_id -eq $group_id ]; then
echo "Good user"
else
echo "Bad user"
fi
else
echo "用户$name不存在"
fi
编写脚本,由用户输入文件名,再由用户输入目录名称,将该文件备份到对应的目录
!/bin/bash
read -p "输入备份文件(Full path): " file
if [ -f $file ]; then
备份文件
read -p "输入备份目录: " dir
if [ -d $dir ]; then
cp $file $dir
echo "文件$file已被复制到$dir目录"
else
mkdir -p $dir
cp $file $dir
echo "文件$file已被复制到$dir目录"
fi
else
echo "文件$file不存在"
fi
编写脚本,判断光盘是否挂载,显示其挂载点名称;否则由用户输入挂载点名称,挂载光盘
!/bin/bash
if df -hT | grep sr0 &> /dev/null; then
mount_point=df -hT | grep sr0 | awk '{print $NF}'
echo "光盘挂载点: $mount_point"
else
read -p "输入挂载点目录:" mount_point
if [ -d $mount_point ]; then
mount /dev/cdrom $mount_point &> /dev/null
echo "光盘被挂载到$mount_point目录"
else
mkdir -p $mount_point
mount /dev/cdrom $mount_point &> /dev/null
echo "光盘被挂载到$mount_point目录"
fi
fi
case分支判断结构
语法:
case 变量名称 in
value1)
statement
statement
;;
value2)
statement
statement
;;
value3)
statement
statement
;;
*)
statement
statement
;;
esac
编写脚本,判断用户输入的字符串
!/bin/bash
read -p "Enter string: " str
case $str in
linux|Linux)
echo "windows."
;;
windows|Windows)
echo "linux."
;;
*)
echo "other."
;;
esac
特殊变量:
位置变量
$1, $2 ,$3 ,$4 ...... $9, ${10},
$1: 命令的第1个参数
$0 命令本身
$# 命令参数的个数
使用位置变量:
!/bin/bash
case $1 in
linux)
echo "windows."
;;
windows)
echo "linux"
;;
*)
echo "other"
;;
esac
!/bin/bash
if [ -z $1 ]; then
echo "用法:./11.sh {linux|windows|other}"
exit 9
fi
case $1 in
linux)
echo "windows."
;;
windows)
echo "linux"
;;
*)
echo "other"
;;
esac
使用$#判断参数是否正确
!/bin/bash
if [ $# -ne 1 ]; then
echo "用法:./11.sh {linux|windows|other}"
exit 9
fi
case $1 in
linux)
echo "windows."
;;
windows)
echo "linux"
;;
*)
echo "other"
;;
esac
[root@shell ~]# basename /etc/sysconfig/network-scripts/ifcfg-eth0 >>>获取文件名称
ifcfg-eth0
[root@shell ~]# dirname /etc/sysconfig/network-scripts/ifcfg-eth0 >>>获取文件所在的目录名称
/etc/sysconfig/network-scripts
[root@shell ~]#