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

相关推荐

  • 文本三剑客之sed用法总结

    描述:    sed是Stream EDitor(行编辑器)的简写,是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space ),接着用sed 命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你…

    Linux干货 2016-08-10
  • rsyslog+mysql+loganalyzer日志服务器搭建

    rsyslog+mysql+loganalyzer日志服务器搭建 环境 服务器端:192.168.25.129,centos7 客户机端:192.168.25.130,centos7 rsyslog+Mysql服务器端的配置: 准备好msql server或mariadb server ]# yum -y install&nbs…

    Linux干货 2016-11-07
  • 马哥教育网络班22期+第二周课程练习

    一、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示? cp命令:copy,有2类复制方法  1、单源复制:如果DEST不存在:则实现创建此文件,并复制源文件的数据流至DEST中;     如果DEST存在:如果DEST是非目录文件,则覆盖目标文件;如果DEST是目录文件,则先DEST目录下创建一个与源文件…

    Linux干货 2016-08-22
  • Linux下使用screen协同作业

    1)screen应用场景(拷贝自网络): 来自产品工程的高级维护用户 David 打电话说:“为什么我不能在您部署的这些新机器上编译 supercode.c”。 您会问他:“您运行的是什么机器?” David 答道:“ Posh”。(这个虚够的公司将它的 5 台生产服务器以纪念 Spice Girls 的方式命名)。这下您可以大显身手了,另一台机器由 Dav…

    系统运维 2016-08-15
  • Linux命令帮助文档的使用及简单命令使用-2016-7-25

    Linux命令帮助文档的使用   相关命令 whatis    COMMAND –help    man and info 本地帮助文档/usr/share/doc   在使用系统内建的帮助文档之前,我们需要了解需要命令帮助是否是内部命令和外部命令…

    Linux干货 2016-08-04