bash 脚本编程之二 条件判断

简介:
  1. bash中如何实现条件判断

条件判断类型:

    整数判断(双目判断):

        -eq:等于 、equal,测试两个整数之间是否相等,比如$A -eq $B

        -gt:大于、greater than

        -lt:小于、lesser than

        -ne:不等于、no  equal

            这里也可以用另外一种写法,比如[ 2 -ne 3 ]可以写作[ ! 2 -eq 3 ]

        -le:小于或等于、lesser or equal

        -ge:大于等于、greater or equal

        ...

    字符判断:

    文件判断:单目判断

        -e:exist判断文件是否存在

        -d:directory判断文件是否为目录

        -r:read 判断文件是否可读

        -w:write ...........可写

        -x:Executable...........可执行

        ...

    

条件判断的表达式:

    [ expression ] 

        注意[] 最左和左右需要空格,这里linux把表达式当成命令来判断

    ` expression `

        同样需要注意空格,这里linux把表达式当成关键字来判断,至于两者区别,暂时存疑    

    test  expression 


命令间的逻辑关系:

    逻辑与:&&

        使用逻辑与的时候,当第一个条件判断为假,则后面条件不再判断

    逻辑或:||

        使用逻辑或的时候,当第一个条件判断为真,则后面条件不再判断

    逻辑非:!

        对条件判断结果进行取反

示例:

如果用户user1不存在,则添加user1

[root@logstach ~]# id user1 

id: user1: No such user

[root@logstach ~]# ! id  user1 &> /dev/null &&  useradd user1 &> /dev/null

[root@logstach ~]# id  user1

uid=3001(user1) gid=3001(user1) groups=3001(user1)

或者

id  user1|| useradd user1


如果/etc/inittab文件的行数大于100,就显示'large file'

[ `wc -l  /etc/inittab |cut -d' ' -f1 ` -gt 100 ] && echo  "large file"


变量的命令规则:

    只能用数字、字母和下划线组成

    不能用数字开头

    不应该与系统环境变量重名

    最好做到见名知义


练习:

如果用户存在,就显示已经存在,否则添加此用户

id  user1 && echo 'user exists!' || useradd user1 


如果用户不存在就添加,否则显示已经存在

!id user1&&useradd user1 ||echo 'user1  already exists!' 


如果用户不存在,添加并给其与用户名同名的密码,否则显示其已经存在。

id  user1 && echo 'user already exists!'|| useradd user1 && echo ‘user1'|passwd user1 --stdin


[root@logstach ~]# id  user1 && echo 'user ?already exists!' || useradd user1 && echo 'user1'|passwd user1 --stdin

uid=3001(user1) gid=3001(user1) groups=3001(user1)

user ?already exists!

Changing password for user user1.

passwd: all authentication tokens updated successfully.

[root@logstach ~]# id  user1 && ?echo 'user ?already exists!' ||( useradd user1 && echo 'user1'|passwd user1 --stdin)

uid=3001(user1) gid=3001(user1) groups=3001(user1)

user ?already exists!

[root@logstach ~]# 


注意:这里如果不加()的话,当shell执行到||时不执行useradd user1,但是它会继续往下读,到第二个&&时,把前面 id  user1 && echo 'user ?already exists!' || useradd user1当成一个整体,发现整体结果为真,则继续执行后面的内容passwd user1 --stdin。加()可以告诉shell,

useradd user1 && echo 'user1'|passwd user1 --stdin是一个整体。


练习:写一个脚本,完成以下要求:

    添加3个用户user1、user2、user3,但要先判断用户是否存在,不存在而后再添加

    添加完成后,显示最后添加了哪几个用户,当然,不能包含已经存在而没有添加的

    最后显示系统上有几个用户。

#!/bin/bash

! id  user1 &> /dev/null &&  useradd user1 && echo 'useradd user1'

! id  user2 &> /dev/null &&  useradd user2 && echo 'useradd user2'

! id  user3 &> /dev/null &&  useradd user3 && echo 'useradd user3'

TOTAL=`cat /etc/passwd|wc -l`

echo  "$TOTAL users."


练习:写一个脚本,完成以下要求:

给定一个用户:

    如果uid为0就显示为管理员

    否则,就显示其为普通用户

#!/bin/bash

USER=user1

USERID=`grep $USER /etc/passwd|cut -d: -f3`

[ $USERID -eq 0 ] && echo "administrator!" || echo  "common user."


条件判断,控制结构:

单分支if语句:

if 判断条件;then

    statement1

    statement2

    ..

fi

     then如果另起一行,则;可以省略,否则不行


双分支if语句:

if  判断条件;then

    statement1

    statement2

    ...

else

    statement1

    statement2    

    ...

fi


多分支if语句:

if  判断条件1;then

    statement1

    ...

elif 判断条件2;then

    statement2  

    ...

elif 判断条件3;then

    statement3

    ....

else

    statement4

    ...

fi



if  ls /var ...和if `ls  /var` ...的区别:

    if ls /var 会根据命令的执行状态结果进行判断,0为真,1-255为假,而if `ls  /var`则会报错,因为 `ls  /var`返回的是命令的执行结果,if 不能对其进行判断,但是如果添加条件使其能满足条件判断的条件,则可以进行判断,例如:

    if  [ `ls  /var|wc -l` -gt 20 ];then 

        echo 'large directory'

    fi















本文转自biao007h51CTO博客,原文链接: http://blog.51cto.com/linzb/1731741,如需转载请自行联系原作者




相关文章
|
5月前
|
监控 安全 Shell
防止员工泄密的措施:在Linux环境下使用Bash脚本实现日志监控
在Linux环境下,为防止员工泄密,本文提出使用Bash脚本进行日志监控。脚本会定期检查系统日志文件,搜索敏感关键词(如"password"、"confidential"、"secret"),并将匹配项记录到临时日志文件。当检测到可疑活动时,脚本通过curl自动将数据POST到公司内部网站进行分析处理,增强信息安全防护。
168 0
|
5月前
|
存储 Shell Linux
Linux Bash 脚本中的 IFS 是什么?
【4月更文挑战第25天】
105 0
Linux Bash 脚本中的 IFS 是什么?
|
2月前
|
Shell
一个能够生成 Markdown 表格的 Bash 脚本
【8月更文挑战第20天】这是一个使用Bash脚本生成Markdown表格的示例。脚本首先设置表头与内容数据,然后输出Markdown格式的表格。用户可以根据需要自定义表格内容。使用时,只需将脚本保存为文件(如 `generate_table.sh`),赋予执行权限,并运行它,即可在终端看到生成的Markdown表格。
|
2月前
|
Unix Shell Linux
在Linux中,什么是Bash脚本,并且如何使用它。
在Linux中,什么是Bash脚本,并且如何使用它。
|
2月前
|
Shell 开发者
深入理解Bash脚本中的函数
【8月更文挑战第20天】
22 0
|
2月前
|
存储 Shell 数据处理
深入探讨Bash脚本中的数组
【8月更文挑战第20天】
15 0
|
2月前
|
存储 Shell
Bash 脚本中的 `hash` 命令
【8月更文挑战第19天】
13 0
|
4月前
|
Unix Shell Linux
技术经验分享:Bash脚本命令使用详解
技术经验分享:Bash脚本命令使用详解
33 0
|
5月前
|
存储 弹性计算 运维
用bash脚本创建目录
【4月更文挑战第29天】
40 3
|
5月前
|
存储 Unix Shell
【简化Cmake编译过程 】编写通用的bash脚本:简化和构建cmake高效自动化任务
【简化Cmake编译过程 】编写通用的bash脚本:简化和构建cmake高效自动化任务
200 0