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

相关推荐

  • 正则表达式的如何使用

    简述:正则表达式主要用于文本的搜索,它表示了搜索文本的过滤条件。根据这些条件,对目标文本朱行进行匹配检查,最后对输出匹配到符合过滤条件的行。 使用:正确高效的使用正则表达式,需要掌握以下基本知识点         1:语法 grep [OPTION] PATTERN FILE……

    Linux干货 2017-06-04
  • M22 wireshark使用方法简介

    wireshark是一款著名的开源抓包软件,它可以抓取网卡的数据包,以供网络管理员分析。 一 安装方法 debain系安装方法: sudo add-apt-repository ppa:wireshark-dev/stable sudo apt update sudo apt install wireshark 启动wireshark: sudo wires…

    2017-03-20
  • Linux文本处理三剑客之grep

    一、grep命令 grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来 作用:文本搜索工具,根据用户指定的“模式”对目标文本逐行进行匹配检查;打印匹配到的行。 模式:由正则表达式字符及文本字符所编写的过滤条件 二、grep命令格式 grep [OPT…

    Linux干货 2016-08-15
  • Linux网络属性配置—ifcfg命令家族

    ifcfg命令家族:ifconfig,route,netstat 1、NAME               ifconfig – configure a network interface       SYNOPSIS   &n…

    Linux干货 2016-11-27
  • sed

    复制/etc/rc.d/rc.sysinit文件到/tmp目录中,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首家#; 2.复制/boot/grub/grub.conf至/tmp中,删除/tmp/grub.conf文件中的行首的空白字符 3.删除/tmp/rc.sysinit文件中的以#开头,且后面跟了一个至少一个空白字符的行行的#…

    Linux干货 2016-12-04
  • scp和rsync的使用

    通过一些简单需求了解scp和rsync的使用

    2017-09-18