shell脚本编写-2

1、条件判断if语句  

    1)、 单分支

if  判断条件;then

    条件为真的分支代码

    fi

2)、双分支

    if  判断条件; then 

    条件为真的分支代码

    else

    条件为假的分支代码

    fi

逐条件进行判断,第一次遇为“真”条件时,执行其分支,而后结束整个if语句

例如:if ping -c1 -W2 station1 &> /dev/null; then

echo 'Station1 is UP'

elif grep "station1" ~/maintenance.txt &> /dev/null;then

echo 'Station1 is undergoing maintenance’

else

echo 'Station1 is unexpectedly DOWN!'

exit 1

   fi

2、条件判断case语句

case 变量引用 in

PAT1)

分支1

;;

PAT2)

分支2

;;

*)

默认分支

;;

Esac

case 支持glob风格的通配符(正则表达式):

*: 任意长度任意字符

?: 任意单个字符

[]:指定范围内的任意单个字符

a|b: a或b

3、练习题示例

1)、写一个脚本/root/bin/createuser.sh ,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id 号等信息

#!/bin/bash

if grep “^$1\>” /etc/passwd >> /dev/dull;then

echo “the username exists”

exit

else

useradd $1

getent passwd $1

fi

2)、写个脚本/root/bin/yesorno.sh ,提示用户输入yes或no, 并判断用户输入的是yes还是no, 或是其它信息

第一种方法:

#!/bin/passwd

read -p "please input the yes or no:" a

if [ $a == yes ];then

echo "yes"

elif [ $a == y ];then

echo "yes"

elif [ $a == Y ];then

echo "yes"

elif [ $a == YES ];then

echo "yes"

elif [ $a == no ];then

echo "no"

elif [ $a == n ];then

echo "no"

elif [ $a == N ];then

echo "no"

elif [ $a == NO ];then

echo "no"

else

echo "other"

fi    

第二种方法:

#!/bin/bash

read –p “please inout yes or no:” a

case $a in

[yY]|[yY][Ee][sS])

echo “yes”

;;

[Nn]|[Nn][Oo])

echo “no”

;;

*)

echo “other”

;;

esac     

注意case语句:

case支持glob风格的通配符(正则表达式):

*: 任意长度任意字符

?: 任意单个字符

[]:指定范围内的任意单个字符

a|b: a或b     

第三种方法:

#!/bin/bash

read -p "please inout yes or no:" a

ans=`echo $a|tr 'A-Z' 'a-z'`

if [ $ans == yes -o $ans == y ]

then

echo "yes"

elif [ $ans == no -o $ans == n ]

then

echo "no"

else

echo "other"

fi                                      

3)、写脚本/root/bin/filetype.sh, 判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它类型)

第一种方法:

#!/bin/bash

read -p "please input the file path:" a

[ ! -e $a ] && echo "the file not exist" && exit

if [ -f $a ];then

echo "this file is common"

elif [ -d $a ];then

echo "this file is directory"

elif [ -h $a ];then

echo "this file is link"

else

echo "this file is other"

fi

4)、写一个脚本/root/bin/checkint.sh, 判断用户输入的参数是否为正整数

首先如何用已有知识表示正整数,注意01也是正整数,可以用正则表达式0*[1-9][0-9]*

#!/bin/bash

read -p "please input the argument:" a

if [[ $a =~ ^0*[1-9][0-9]*$ ]];then

echo "this arg is zzshu"

else

echo "this arg isn't zzshu"

fi

 



原创文章,作者:18612763863,如若转载,请注明出处:http://www.178linux.com/35572

(0)
1861276386318612763863
上一篇 2016-08-15 09:24
下一篇 2016-08-15 09:24

相关推荐

  • 面向对象的实例方法,类方法和静态方法

    类方法和静态方法 普通函数 class Person: def normal_method(): print(‘normal’) Person.normal_method() # Person().normal_method() print(Person.__dict__) Person.normal_method()可以,是因为这个方法只是被Person这…

    2017-11-11
  • linux防火墙介绍

    一、前言firewall(防火墙):工作在网络进入或者流包,进出的网络数据包进行一定的规则进行检查过滤系统。包括iptables和netfilter组件。iptables 是与 Linux 内核集成的 IP 信息包过滤系统。如果 Linux 系统连接到因特网或 LAN、服务器或连接 LAN 和因特网的代理服务器, 则该系统系统中更好地控制 IP 信息包过滤和…

    2017-04-30
  • N25-第五周作业

    1、显示当前系统上root、fedora或user1用户的默认shell; [root@zf ~]# grep -E "^(root|fedora)\>" /etc/passwd | cut -d: -f1,7   &nbs…

    Linux干货 2016-12-27
  • 开学第一天

      今天是我来到马哥教育的第一天,来之前心情非常之忐忑,因为没有接触过linux系统,感觉是个新事物,来到教室里,一百多人的大集体,年龄的差异,使我感觉到压力。首先马哥和张sir的讲话,非常的帮,其中令我记忆最深刻的是那句话,取上者得中,取中者得下。一个人在奋斗的时候如果给自己的目标定的低,那就收获的还要低。所以要高标准的要求自己,一开始给自己定一…

    Linux干货 2018-03-26
  • corosync v2+pacemaker实现mariadb的高可用

    高可用mariadb拓扑图 一、设计前提 1、时间同步 # ntpdate 172.16.0.1 或者 # chronyc sources 2、所有的主机对应的IP地址解析可以正常工作, 主机名要与命令#uname -n 所得的结果一致 因此,/etc/hosts中的内容为以下内容         172.16.23.10 node1.rj.com node…

    2017-11-02
  • 8.1作业

    8月1日作业 创建testuser uid 1234,主组:bin,辅助组:root,ftp,shell:/bin/csh home:/testdir/testuser [root@localhost ~]# useradd -u 1234 -g bin -G root,ftp&…

    Linux干货 2016-08-04