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

相关推荐

  • 命令的查看帮助 和 命令行的历史

    命令的查看 分内部命令 和外部命令  查看外部命令 可用 man command–help info  查看内部命令 可用 help command man 帮助是 使用最频繁的 命令查看 而 man 又有很多选项 如下    man – format and display the on-li…

    Linux干货 2016-08-12
  • 正则表达式

    正则表达式:             由一类特殊字符及文本字符所编写的模式,其中有些字符(元字符)不表示字符            字面意义,而表示控制或通配的功能程序支持:  grep,sed,awk,vim,less…

    2017-08-05
  • Linux基础知识—Vim编辑器/crond周期性任务计划

    vim文本编辑 vim是vi文本编辑的增强版本,因其编辑功能强大、且支持各种语言开发环境关键字自动增亮功能,也可称为是开发工具。在Centos7/RHEL7开始,默认将vi定义成vim –color=auto,已经取代了vi的地位。下面总结下关于vim工具的使用 vim有三种模式,每个模式下有其特定的功能;我们可自由在三者之间切换使用: COMM…

    Linux干货 2016-11-01
  • 马哥linux运维26期开班感悟

         今天的收获,有以下几点      1.自己的公众演说能力很弱,要多多练习,给自己更多的机会。     2.自己想提升自己的心很强,但是自己却不知道该怎么做?     3.即便学完了也只是一个开始,希望自己有一颗归零的心态。

    Linux干货 2017-07-10
  • 推荐-Centos常用的进程管理和资源查看工具

    一、pstree     pstree命令以树状图的方式展现进程之间的派生关系 -a:显示每个程序的完整指令,包含路径,参数或是常驻服务的标示;  -c:不使用精简标示法;  -G:使用VT100终端机的列绘图字符;  -h:列出树状图时,特别标明现在执行的程序;  -H<…

    Linux干货 2016-04-05