shell脚本2——顺序选择语句

流程控制

     顺序执行

     选择执行

     循环执行

顺序执行:

    条件选择:if语句

if语句为选择执行

注意:if语句可嵌套

单分支

if  判断条件:then
    条件为真的分支代码
fi

双分支

if  判断条件; then
    条件为真的分支代码
else
    条件为假的分支代码
fi

多分支

if  CONDITION1 ; then
    if-true
elif CONDITION2 ; then
    if-ture
elif CONDITION3 ; then
    if-ture
...
else
    all-false
fi

    条件判断:case语句

case 变量引用 in
    PAT1)
        分支1
        ;;
    PAT2)
        分支2
        ;;
    ...
    *)
        默认分支
        ;;
esac

case 支持glob 风格的通配符:

    *:任意长度任意字符

    ?:任意单个字符

    []:指定范围内的任意单个字符

    a|b:a或b

练习:

1 、写一个脚本/root/bin/createuser.sh ,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息

#!/bin/bash
#descriptio
#version 0.1
#author gm
#date 20160812
if `id $1 &> /dev/null`;then
    echo "$1 user is exist;"
else
    useradd $1
    echo "useradd $1."
    echo "$1 `id $1`"
fi

[root@CentOS6 bin]# createuser.sh  gao
gao user is exist;
[root@CentOS6 bin]# createuser.sh  test
useradd test.
test uid=522(test) gid=522(test) groups=522(test)

2 、写一个脚本/root/bin/yesorno.sh ,提示用户输入yes或no, 并判断用户输入的是yes 还是no, 或是其它信息

#!/bin/bash
#description
#version 0.2
#author gm
#date 20160812
read -p "please input yes or no: " string
case $string in
    [yY]|[yY][eE][sS])
        echo "user input is yes.";;
    [nN]|[nN][oO])
        echo "user input is no";;
    *)
        echo "user input is other";;
esac

[root@CentOS6 bin]# yesorno.sh
please input yes or no: yse
user input is other
[root@CentOS6 bin]# yesorno.sh
please input yes or no: yes
user input is yes.
[root@CentOS6 bin]# yesorno.sh
please input yes or no: y
user input is yes.

3 、写一个脚本/root/bin/filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)

#!/bin/bash
#description
#version 0.1
#author gm
#date 20160812
read -p "please file path: " path
if [ ! -e $path ] ;then
    echo "$path is not exist."
elif [ -h $path ] ;then
    echo "$path is link file."
elif [ -f $path ] ;then
    echo "$path is common file."
elif [ -d $path ] ;then
    echo "$path is dir file"
else
    echo "$path is other file."
fi

[root@CentOS6 bin]# filetype.sh
please file path: /etc
/etc is dir file
[root@CentOS6 bin]# filetype.sh
please file path: /dev/sda
/dev/sda is other file.
[root@CentOS6 bin]# filetype.sh
please file path: /etc/fstab
/etc/fstab is common file.

4 、写一个脚本/root/bin/checkint.sh, 判断用户输入的参数是否为正整数

#!/bin/bash
#description
#version 0.2
#author gm
#date 20160812
read -p "please ont number of int: " num
testnum=$num
echo $num | grep -qE "^[0-9]+$"
if [ $? -eq 0 ] ; then
    if [ $num -ne 0 ] ; then
        echo "$num is positive integer."
    else
        echo "$num in not positive integer."
    fi
else
    echo "$num is not positive integer."
fi

[root@CentOS6 bin]# checkint.sh
please ont number of int: 123
123 is positive integer.
[root@CentOS6 bin]# checkint.sh
please ont number of int: sdfj
sdfj is not positive integer.
[root@CentOS6 bin]# checkint.sh
please ont number of int: 1.123324
1.123324 is not positive integer.
[root@CentOS6 bin]# checkint.sh
please ont number of int: -123.123
-123.123 is not positive integer.

原创文章,作者:megedugao,如若转载,请注明出处:http://www.178linux.com/36886

(0)
megedugaomegedugao
上一篇 2016-08-18 10:09
下一篇 2016-08-18 10:09

相关推荐

  • 第六周作业

    请详细总结vim编辑器的使用并完成以下练习题 1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; %s@^[[:space:]]\+@#&@g 2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的…

    Linux干货 2017-03-03
  • grep &&正则表达式&&通配,元字,转义符

    grep 基于正则表达式的查询 1正则表达式: 正则表达式是通过使用特殊的字符所编写的模式,一些字符不表示字符的表面意思,具有控制和通配的功能. 使用正则表达式进行匹配具有两种方法:一是通过使用特殊的元字符进行字符匹配;二是使用次数匹配。 一, 正则表达式的特殊字符(元字符) :  #建议再温习一下之前所学的统通配符,对比一下,防止混淆。 (1)元…

    Linux干货 2016-08-05
  • RPM软件包管理

    Linux应用程序的组成 安装完一个软件包以后,可能会向系统中复制大量的数据文件,并进行相关设置。在Linux系统中,典型的应用程序通常由以下几部分组成。 普通的可执行程序文件:一般保存在“/usr/bin”目录中,普通用户即可执行。 服务器程序、管理程序文件:一般保存在”/usr/sbin“目录中,只有管理员能执行。 配置文件:一般保存在”/etc“目录中…

    Linux干货 2016-08-21
  • 马哥教育网络班20期+第5周课程练习

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

    Linux干货 2016-08-15
  • 博客开始的地方

    来北京挺长时间了,一直都在瞎忙,特别想找个地逛逛,约朋友一起去,结果人家只喜欢吃饭,逛街,来北京瞎逛过两次,第一次是面试路过潘家园,然后进去逛了半天,看各种各样的老古董,当然最多的还是手串,那种核桃,当然还是看到了好多好多有意思的东西,比如下面的小和尚和老和尚,小和尚光着头感觉特别可爱,老和尚还卖萌, 当然还有下面这个关老爷,他的肚子为什么被雕刻的那么大,难…

    2018-03-26
  • 网络班26期-第1周课程作业

    1、描述计算机的组成及其功能? (1)CPU:计算机的大脑,计算机的运算,命令的控制功能,寄存器及缓存都由CPU功能承担; (2)内存:RAM,随机访问存储器,计算机的另一个核心功能,系统指令存放及运行的基础。 (3)输入设备:人工给计算机下指令的设备,例如鼠标,键盘等; (4)输出设备:计算机输出人工输入的执行命令的结果;例如,音响,显示屏等; …

    Linux干货 2017-01-10