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

相关推荐

  • 马哥教育网络20期+第五周课程练习

    1、显示/boot/grub/grub.conf中以至少一个空白字符开头的行; [root@localhost ~]# grep "^[[:space:]]\+" /boot/grub/grub.conf 2、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又…

    Linux干货 2016-07-07
  • 浅谈vim使用

    vim常用命令总结 2013年10月12日 ⁄ 综合 ⁄ 共 3264字 ⁄ 字号 小 中 大 ⁄  评论关闭        vim 选择文本,删除,复制,粘贴   文本的选择,对于编辑器来说,是很基本的东西,也经常被用到,总结如下: v    从光标当前位置开始,光标所经过的地…

    Linux干货 2016-08-12
  • history命令详解

       有效地使用命令历史机制将会使效率获得极大提升。history:   保存你输入的命令历史。 可以用它来重复执行命令。   history [-c] [-d offset] [n]   history -anrw [filename]   history …

    2017-03-26
  • Linux程序包管理方式

    Linux程序包安装和管理方式共计三种:          一、[yum|dnf],通过官网或者其他开源网站提供的文件服务器,本机镜像源等途径进行安装。         二、rpm,通过官网或者其他开源网站通过…

    Linux干货 2016-07-29
  • SUID_SGID_Sticky简单总结

    参考: http://blog.chinaunix.net/uid-25314474-id-3313109.html —————————————权限——&#82…

    Linux干货 2015-09-14
  • Linux系统上获取命令的帮助信息方法

    当我们在操作linux时忘记相关命令的用法时,可以使用man 命令或者命令 -h来查询该命令的用法; man文档共有9个章节 1:所有用户可以操作的指令或可执行文件 2:系统核心调用的函数与工具 3:子调用,常用的函数与函数库 4:设备,硬件文件说明,通常是/dev/的文件 5:文件格式,配置文件或者是某些档案的格式 6:游戏相关 7:杂项,例如linux文…

    Linux干货 2018-03-04