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

相关推荐

  • mkdir用法实践

    mkdir:make directories mkdir [OPTION]… DIRECTORY… -p: 自动按需创建父目录; -v: verbose,显示详细过程; -m MODE:直接给定权限;   1、创建/tmp目录下的:a_c,a_d, b_c, b_d ~]# mkdir -v /tmp/{a,b}_{c,d}…

    Linux干货 2016-11-06
  • class7 文本处理命令及文本处理工具grep

    一、文本处理命令   1、文件内容查看cat, tac,rev     cat [OPTION]… [FILE]…      正序查看文本文件          -E:  显示行结束符$ [roo…

    Linux干货 2016-08-08
  • 文件查找工具locate和find的使用分析

    文件查找工具locate和find的使用分析 不管是在windows系统中还是在Linux系统中,我们经常会一些文件进行搜索查找,而在Linux系统中经常用到的搜索工具有locate和find,这两种搜索工具的工具原理和用法都不相同,一下将这对这两种搜索工具的使用进行分析。 1、locate工具的工作原理是对/var/lib/mlocate/mlocat.d…

    Linux干货 2016-08-18
  • shell脚本编程基础(1)

    一.位置变量相关知识     1.位置变量定义:在脚本代码中调用通过命令行传递给脚本的的参数     2.位置变量种类:              &…

    Linux干货 2016-08-15
  • 关于大型网站技术演进的思考(十四)–网站静态化处理—前后端分离—上(6)

    原文出处: 夏天的森林  前文讲到了CSI技术,这就说明网站静态化技术的讲述已经推进到了浏览器端了即真正到了web前端的范畴了,而时下web前端技术的前沿之一就是前后端 分离技术了,那么在这里网站静态化技术和前后端分离技术产生了交集,所以今天我将讨论下前后端分离技术,前后端分离技术讨论完后,下一篇文章我将会以网站 静态化技术的角度回过头来…

    Linux干货 2015-02-26
  • 运维架构之httpd

    Web Service 传输层:提供进程地址 Port number tcp:传输控制协议,面向连接,通信前需建立虚拟链路,结束后拆除;0-65535 udp:用户数据报协议,无连接;0-65535 IANA 0-1023:众所周知,永久分配给固定应用使用;22/tcp(ssh)、80/tcp(http)、443/tcp(https) 1024-41951:…

    系统运维 2018-06-04