&& 逻辑的 AND 的意思, -a 也是这个意思,两个条件同时成立,为真。
|| 逻辑的 OR 的意思, -o 也是这个意思,两个条件一个成立,为真。
示例 猜测用户输入的成绩是否良好(59~79)之间 [root@localhost ~]# num=59;[ $num -gt 60 -a $num -lt 80 ];echo $? 1 [root@localhost ~]# num=81;[ $num -gt 60 -a $num -lt 80 ];echo $? 1 [root@localhost ~]# num=60;[ $num -gt 60 -a $num -lt 80 ];echo $? 1 [root@localhost ~]# num=61;[ $num -gt 60 -a $num -lt 80 ];echo $? 0 多种表达方法 [root@localhost ~]# [ 1 -lt 2 -a 5 -gt 10 ];echo $? 1 [root@localhost ~]# [ 1 -lt 2 -o 5 -gt 10 ];echo $? 0 注意:&&调用正则时,请使用双中括号。 [root@localhost ~]# [[ 1 -lt 2 && 5 -gt 10 ]];echo $? 1 [root@localhost ~]# [[ 1 -lt 2 || 5 -gt 10 ]];echo $? 0 或者 [root@localhost ~]# [ 1 -lt 2 ] && [ 11 -gt 10 ];echo $? 0编写脚本,由用户输入用户名,如果用户不存在,则创建该用户,并设置密码为123456
[root@localhost ~]# vim 1.sh #!/bin/bash read -p "Input username: " name id $name &> /dev/null if [ $? -ne 0 ]; then useradd $name echo "123456" | passwd --stdin $name &> /dev/null echo "$name create finished,the password is 123456" fi 或者运用取反的方法 [root@localhost ~]# vim 1.sh #!/bin/bash read -p "Input username: " name if ! id $name &> /dev/null; then useradd $name echo "123456" | passwd --stdin $name &> /dev/null echo "$name create finished,the password is 123456" fi编写脚本,由用户输入用户名,如果用户不存在,则创建该用户,并设置密码为123456;否则,提示用户已经存在
[root@localhost ~]# vim 1.sh #!/bin/bash read -p "Input username: " name #id $name &> /dev/null #if [ $? -ne 0 ] ;then if ! id $name &> /dev/null; then useradd $name echo "123456" | passwd --stdin $name &> /dev/null echo "$name create finished,the password is 123456" else echo "$name already exist" fi 示例2编写脚本,由用户输入用户名,判断该用户的uid及gid,如果相同,则显示Good user; 否则显示Bad user.
[root@localhost ~]# vim 1.sh #!/bin/bash read -p "Input username: " name 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编写脚本,取出系统时间的小时,对数字进行判断 6–10 this is morning 11-13 this is noon 14-18 this is afternoon 其他 this is night
[root@localhost ~]# vim 1.sh #!/bin/bash hour=`date +%H` if [ $hour -ge 6 -a $hour -le 10 ];then echo "This is morning" elif [ $hour -ge 11 -a $hour -le 13 ];then echo "This is noon" elif [ $hour -ge 14 -a $hour -le 18 ];then echo "This is afternoon" else echo "This is night" fi邀请用户输入待删除用户名。 询问用户,确定要继续删除吗 yes/no
[root@localhost ~]# vim 1.sh #!/bin/bash #1请输入删除的用户名: read -p "please input a username : " user #2输出用户ID id $user &> /dev/null #3判断用户是否存在 if [ $? -ne 0 ];then echo "no such user: $user" exit 1 fi #4请用户确认是否删除 read -p "are you sure?[y/n]: " action if [ "$action" = "y" -o "$action" = "Y" ] ;then userdel -r $user echo "$user is deleted!" else echo "thank you" fi [root@localhost ~]# vim 1.sh #!/bin/bash #1请输入删除的用户名: read -p "please input a username : " user #2输出用户ID id $user &> /dev/null #3判断用户是否存在 if [ $? -ne 0 ];then echo "no such user: $user" exit 1 fi #4请用户确认是否删除 read -p "are you sure?[y/n]: " action #if [ "$action" = "y" -o "$action" = "Y" ] ;then # userdel -r $user # echo "$user is deleted!" #else # echo "thank you" #fi #5case写法 case "$action" in Y|y|YES|yes) userdel -r $user echo "$user is deleted!" ;; *) echo "thank you" ;; esac1)web1 2)web2 3)web3 h)help q)exit
[root@localhost ~]# vim jump-server.sh #!/bin/bash web1=10.8.162.66 web2="待分配2" web3="待分配3" while : do cat <<EOF 1.web1 2.web2 3.web3 4.quit 5.exit EOF read -p "input number:" num case $num in 1) ssh root@$web1 ;; 2) ssh root@$web2 ;; 3) ssh root@$web3 ;; 4) quit ;; 5) exit ;; *) echo "no" esac doneh 显示命令帮助 f 显示磁盘分区 d 显示磁盘挂载 m 查看内存使用 u 查看系统负载 q 退出程序
[root@localhost ~]# vim systemmanage.sh #!/bin/bash cat <<EOF h.help f.disk partation d.filesystem mount m.memory u.system load q.exit EOF while : do read -p "please input:" action case "$action" in f) fdisk -l ;; d) df -hT ;; m) free -m ;; u) uptime ;; q) exit ;; h) cat <<EOF h.help f.disk partation d.filesystem mount m.memory u.system load q.exit EOF ;; *) echo "error" ;; esac done