shell 脚本之程序流程控制命令(1)

if-then-elif-else-fi

语法:if expression

                   then

                      [elif expression

                          then

                                then-ommand-list  ]    

                    ........

                    [

                     else

                          else-command-list]

fi

用途:实现二路或多路跳转

第一种if语句没有任何可选择特性,这决定了它通常用于二路跳转:

语法:if expression

                   then

                   then-command

          fi

例程:

$ cat  if_demol

#! /bin/bash

if test $# -na 1

     then

         echo "usage :$0 ordinary_file"

         exit 1

fi

if test -f "$1"

then

      filename="$1"

      set $(ls -il $filename)

      inode="$1"

      size="$6"

     echo "Name Tnode Size"

     echo

     echo "$filename $inode $size"

    exit 0

fi

echo "$0: argument must be an ordinary file"

exit 1

第二种if 语句;

语法:if expression

                   then

                            then-command

                   else

                           else-command

            fi

用途:实现二路跳转

例程:

$ cat if_demol2

#! /bin/bash

if[$# -ne 1]

        then

               echo "Usage: $0 ordinary_file"

               exit 1

fi

if [-f  "$1" ]

        then

                filename="$1"

                set  $(ls -il $filename)

                inode="$1"

                size="$6"

                echo "Name Inode Size "

                echo

                echo "$filename $inode $size"

                exit

        else

               echo

               echo "$0 argment must be an ordinary file"

               exit 1

fi

第三种if语句用来实现多路跳转

语法:if expression

                    then

                           then-command

                    elif expression2

                           elif-command

                    elif expression3

                           elif-command

                    else

                           else-command

fi

例程:

$ cat if_demol3

#! /bi/bash

if[ $# -ne 1]

         then

                 echo "Usage: $0  file"

                 exit 1

        else

                ls "$1"2>/dev/null 1>$2

if  [$? -ne 0 ]

           then

           echo"$1:not find"

exit 1

fi

       if [ -f "$1"]

          then

                  filename="$1"

                  set (ls -il $filename)

                  inode="$1"

                  size="$6"

                  echo "Name Inode Size"

                  echo

                  echo "$filename $inode $size"

                  exit 0

      elif  [ -d "$1"]

             then

                  nfiles=$(ls "$1 | wc -w")

                  echo  "The number of files in the directionary is $nfiles."

                  exit 0

            else

                 echo "$0:argument must be an ordinary file or directory"

                 exit 1

           fi

fi

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值