您的位置:首页 > 运维架构 > Shell

6.3bash条件测试

2016-04-22 00:00 447 查看
条件测试:
整型测试:数值间的大小比较
-gt, -lt, -eq, -ne, -ge, -le
字符串测试:字符串大小比较
>, <, ==, !=, =~, -n不空为真, -z
文件测试

[root@linux_basic ~]#type hostname
hostname is hashed (/bin/hostname)
[root@linux_basic ~]#hostname --help
Usage: hostname [-v] {hostname|-F file} set hostname (from file) 跟参数,则为设置主机名
domainname [-v] {nisdomain|-F file} set NIS domainname (from file)
hostname [-v] [-d|-f|-s|-a|-i|-y|-A|-I] display formatted name
hostname [-v] display hostname 不接参数则是显示主机名

hostname -V|--version|-h|--help print info and exit

dnsdomainname=hostname -d, {yp,nis,}domainname=hostname -y

-s, --short short host name
-a, --alias alias names
-i, --ip-address addresses for the hostname
-I, --all-ip-addresses all addresses for the host
-f, --fqdn, --long long host name (FQDN)
-A, --all-fqdns all long host names (FQDNs)
-d, --domain DNS domain name
-y, --yp, --nis NIS/YP domainname
-F, --file read hostname or NIS domainname from given file

This command can read or set the hostname or the NIS domainname. You can
also read the DNS domain or the FQDN (fully qualified domain name).
Unless you are using bind or NIS for host lookups you can change the
FQDN (Fully Qualified Domain Name) and the DNS domain name (which is
part of the FQDN) in the /etc/hosts file.

例如:如果当前主机的主机名为localhost,则将其修改为www.magedu.com

比较时,
if [ `hostname` == 'localhost' ]; then
hostname www.magedu.com
fi
---------------------------------------------------------------
[root@linux_basic scripts]#cat hostname.sh
#!/bin/bash
#
if [ "$(hostname)" == "localhost" ]
then
hostname www.magedu.com
fi
[root@linux_basic scripts]#bash -n hostname.sh
[root@linux_basic scripts]#chmod +x hostname.sh
[root@linux_basic scripts]#./hostname.sh
[root@linux_basic scripts]#hostname localhost
[root@linux_basic scripts]#hostname
localhost
[root@linux_basic scripts]#./hostname.sh
[root@linux_basic scripts]#hostname
www.magedu.com
---------------------------------------------------------------

[root@linux_basic ~]#echo $sr

[root@linux_basic ~]#[ $st == 'localhost' ]
-bash: [: ==: unary operator expected 要求的是双目运算符的操作,而这里是只有一个
操作数($sr是为空值的)
要模拟空值比较,需要空值加双引号才行
[root@linux_basic ~]#[ "$st" == 'localhost' ]
[root@linux_basic ~]#echo $? 以上状态为假,所以值不为0
1

例如:如果当前主机的主机名为空,则将其修改为用户通过命令行参数传递过来的用户名
hostName=`hostname`

if [ -z "$hostName" ]; then
hostname $1
fi 这里是需要注意 空格和值为空 是不同的概念的
-----------------------------------------------------------------
[root@linux_basic scripts]#cat hostname2.sh
#!/bin/bash
#
if [ -z "$(hostname)" ];then
hostname $1
fi
[root@linux_basic scripts]#bash -n hostname2.sh
[root@linux_basic scripts]#chmod +x hostname2.sh
[root@linux_basic scripts]#./hostname2.sh root
[root@linux_basic scripts]#hostname
www.magedu.com
[root@linux_basic scripts]#hostname ' '
[root@linux_basic scripts]#hostname

[root@linux_basic scripts]#./hostname2.sh root
[root@linux_basic scripts]#hostname

[root@linux_basic scripts]#echo "hostname >> hostname2.sh"
hostname >> hostname2.sh
[root@linux_basic scripts]#echo "hostname" >> hostname2.sh
[root@linux_basic scripts]#./hostname2.sh root

[root@linux_basic scripts]#cat hostname2.sh
#!/bin/bash
#
if [ -z "$(hostname)" ];then
hostname $1
fi
hostname
[root@linux_basic scripts]#bash -x ./hostname2.sh root
++ hostname
+ '[' -z ' ' ']'
+ hostname

[root@linux_basic scripts]#hostname ''
[root@linux_basic scripts]#bash -x ./hostname2.sh root
++ hostname
+ '[' -z '' ']'
+ hostname root
+ hostname
root 因为hostname是环境变量,所以在当前shell和其子shell下,值都是修改过后的
--------------------------------------------------------------------

组合条件测试:在多个条件间实现逻辑运算
常用的三种运算: 与运算 或运算 非运算
与 有两种写法:[ condition1 -a condition2 ]
condition1 && condition2
或 有两种写法:[ condition1 -o condition2 ]
condition1 || condition2
非 有两种写法:[ -not condition ] 注意这种:-not
! condition

例如:如果当前主机的主机名为空,或者为"localhost",则将其修改为www.magedu.com
#!/bin/bash
#
hostName=`hostname`

if [ -z "$hostName" -o "$hostName" == 'localhost' ]; then
hostname www.magedu.com
fi

-----------------------------------------------------------------------
[root@linux_basic scripts]#cat hostname3.sh
#!/bin/bash
#
if [ -z "$(hostname)" -o "$(hostname)" == "localhost" ];then
hostname www.magedu.com
fi

hostname
[root@linux_basic scripts]#bash -n hostname3.sh
[root@linux_basic scripts]#chmod +x hostname3.sh
[root@linux_basic scripts]#hostname
root
[root@linux_basic scripts]#hostname ''
[root@linux_basic scripts]#hostname

[root@linux_basic scripts]#./hostname3.sh
www.magedu.com
[root@linux_basic scripts]#hostname
www.magedu.com
[root@linux_basic scripts]#hostname root
[root@linux_basic scripts]#./hostname3.sh
root
------------------------------------------------------------------------
如果某用户存在,则显示id号:
if id $userName &> /dev/null; then
id -u $userName
fi

例如:输入某用户,如果用户存在,且answer变量的值为“yes",则显示用户的ID号;否则,说用户选择了退出;
id $userName
retVal=$? 保存执行状态返回值

if [ $retval -eq 0 -a "$answer" == 'yes' ]; then

上述方式改为:

if id $userName &> /dev/null && [ "$answer" =='yes' ]; then
id $userName &> /dev/null 这里是命令的执行状态
[ "$answer" =='yes' ] 这里是字符串的比较,所以要有 [ ] 中括号
-----------------------------------------------------------------------
[root@linux_basic scripts]#cat user1.sh
#!/bin/bash
#
answer="yes"
if id $1 &>/dev/null && [ "$answer" == 'yes' ]
then
id -u $1
else
echo "User select exit."
exit 1
fi
[root@linux_basic scripts]#./user1.sh
0
[root@linux_basic scripts]#./user1.sh user
510
[root@linux_basic scripts]#id -u user
510
-------------------------------------------------------------------------

例如:如果answer不为"quit",也不为"q",则说用户选择了继续;此为与

例如:如果answer不为quit或q,则说明用户选择了继续; 此为或

德 摩根定律:

练习:给定一个用户,如果其shell为/bin/bash且其ID号大于等于500,则说这是一个可登录普通用户;否则,则显示其为非登录用户或管理员。
[root@linux_basic scripts]#cat user.sh
#!/bin/bash
#
#if [ "$(grep "^${1}:" /etc/passwd|cut -d: -f7)" == "/bin/bash" ] && [ "$(id -u $1)" -ge 500 ]
下面写法是错误的,因为 && 的使用不能在 [ ]中括号里
#if [ "$(grep "^${1}:" /etc/passwd|cut -d: -f7)" == "/bin/bash" && "$(id -u $1)" -ge 500 ]
写法正确
if [ "$(grep "^${1}:" /etc/passwd|cut -d: -f7)" == "/bin/bash" -a "$(id -u $1)" -ge 500 ]
then
echo "The is can login user."
else
echo "The is not login user."
fi

#!/bin/bash
#
if ! id $1 &> /dev/null; then
echo "No this user."
exit 3
fi

userShell=$(grep "^$1\>" /etc/passwd | cut -d: -f7)
userID=$(id -u $1)

if [ "$userShell" == '/bin/bash' -a $userID -ge 500 ]; then
echo "Login user."
else
echo "not login user."
fi
-----------------------------------------------------------
[root@linux_basic scripts]#cat islogin.sh
#!/bin/bash
#
if ! id $1 &>/dev/null;then
echo "$1 no exists."
exit 1 不存在时,记住退出状态不能少了
fi

userid=$(grep "^$1:" /etc/passwd | cut -d: -f3)

if [ $userid -ge 500 ]
then
echo "$1 is login."
else
echo "$1 is no login."
fi
[root@linux_basic scripts]#bash -n islogin.sh
[root@linux_basic scripts]#./islogin.sh usr
usr no exists.
[root@linux_basic scripts]#./islogin.sh user
user is login.
[root@linux_basic scripts]#bash -x ./islogin.sh user100
+ id user100
+ echo 'user100 no exists.'
user100 no exists.
+ exit 1
--------------------------------------------------------------

练习:写一个脚本
如果某用户不存在,就添加之;
#!/bin/bash
#
if ! id $1 &> /dev/null; then
useradd $1
fi

练习:写一脚本
1、添加10个用户:tuser501-tuser510
如果用户不存在,才添加;如果存在,则显示已经有此用户
2、显示一共添加了多少个用户;
#!/bin/bash
#
declare -i count=0

for i in {501..510}; do
if id tuser$i &> /dev/null; then
echo "tuser$i exists."
else
useradd tuser$i
let count++
fi
done

echo "Total add $count users."

练习:写一脚本
1、添加10个用户:tuser601-tuser610
如果用户不存在,才添加,并以绿色显示添加成功;如果存在,则以红色显示已经有此用户;
2、显示一共添加了多少个用户;
#!/bin/bash
#
declare -i count=0

for i in {501..510}; do
if id tuser$i &> /dev/null; then
echo -e "\033[31mtuser$i\033[0m exists."
else
useradd tuser$i
echo -e "add user \033[32mtuser$i\033[0m successfully."
let count++
fi
done

echo "Total add $count users."

练习:写一个脚本
传递用户名给脚本
1、判断此用户的shell是否为/bin/bash,如果是,则显示此用户为basher
2、否则,则显示此用户为非basher

#!/bin/bash
#
userShell=`grep "^$1\>" /etc/passwd | cut -d: -f7`

if [ "$userShell" == '/bin/bash' ]; then
echo "basher"
else
echo "not basher"
fi

切记命令引用需要使用 反引号`` 或者是 $() 来取命令的值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: