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)
上一篇 2016-08-18 10:09
下一篇 2016-08-18 10:10

相关推荐

  • N25第8周作业

    1、写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态;     在线的主机使用绿色显示;      不在线的主使用红色显示; #!/bin/bash ipnet=172.20.51. trap ‘trap_action’ INT trap…

    Linux干货 2017-02-22
  • 03硬链接和软链接的区别和联系

    首先我们需要了解linux下硬链接以及软连接的基本概念. 硬链接:新建的文件是已经存在的文件的一个别名,当原文件删除时,新建的文件仍然可以使用. 软链接:也称为符号链接,新建的文件以“路径”的形式来表示另一个文件,和Windows的快捷方式十分相似,新建的软链接可以指向不存在的文件. 硬链接和软连接之间的区别: 1.硬链接和原来的文件没有什么区别,而且共享一…

    Linux干货 2016-10-20
  • Linux系统启动过程中grub故障与修复(一)

    GRUB 是引导装入器 — 它负责装入内核并引导 Linux 系统。GRUB 可以引导多种操作系统,如Linux、 DOS、 Windows 。 GRUB共分为三个阶段:stage1主要负责BIOS和GRUB之间的交接,载入存放于各个分区中的开机文件;stage1.5是连接stage1和stage2之间的通道,起着过渡的作用,负责识别stage2所在/bo…

    2017-09-03
  • 用户和组

    3A安全介绍 资源分派: Authentication:认证 Authorization:授权 Accouting|Audition:审计 安全上下文 Linux安全上下文 运行中的程序:进程 (process) 以进程发起者的身份运行: root: /bin/cat mage: /bin/cat 进程所能够访问资源的权限取决于进程的运行者的身份 用户use…

    Linux干货 2016-08-08
  • 超全超详细的HTTP状态码大全

    本部分余下的内容会详细地介绍 HTTP 1.1中的状态码。这些状态码被分为五大类:  100-199 用于指定客户端应相应的某些动作。 200-299 用于表示请求成功。 300-399 用于已经移动的文件并且常被包含在定位头信息中指定新的地址信息。 400-499 用于指出客户端的错误。 500-599 用…

    Linux干货 2015-03-20
  • 马哥教育网络班22期+第5周课程练习

    1、显示当前系统上root、fedora或user1用户的默认shell;cat /etc/passwd | grep "^\<root\>"cat /etc/passwd | grep "^\<fedora\>"cat /etc/passwd | grep "^\<user1\…

    Linux干货 2016-09-19