shell脚本之四:shift位置参数左移、函数、退出循环

1、Shift位置参数左移指令

1.1、定义:

 shift命令用于对位置参数的移动(左移),通常用于在不知道传入参数个数的情况下依次遍历每个参数然后进行相应处理。

1.2、作用:

     每执行一次,位置参数序列顺次左移一个位置,$#的值减1,用于分别处理每个参数,移出去的参数,不再可用,例如执行一次shift位置参数就左移一次,原来的第一个位置参数被移出去了,不再可用,第二个位置参数成了第一个位置参数,依次类推。

 

1.3、实例:加法计算器

 [root@localhost ~]# cat shift.sh

 #!/bin/bash

 if [ $# -le 0 ];then

  echo "没有提供足够的参数"

  exit 1

 fi

 sum=0

 while [ $# -gt 0 ];do

  sum=$[$sum+$1]

  shift

 done

 echo “result is $sum”

 [root@localhost ~]# sh shift.sh

 没有提供足够的参数

 result is 0

 [root@localhost ~]# sh shift.sh 2 3 8 10

 result is 23

 [root@localhost ~]# sh shift.sh 2 3 8 -10 5

 result is 8

 [root@localhost ~]# cat shift.sh

 #!/bin/bash

 if [ $# -le 0 ];then

  echo "没有提供足够的参数"

  exit 1

 fi

 sum=0

 while [ $# -gt 0 ];do

  #sum=$[$sum+$1]

  sum=$(expr $sum + $1)

  #shift 2 一次移动2个参数

  shift

 done

 echo result is $sum

2、函数的使用

 函数是一个脚本代码块,你可以对它进行自定义命名,并且可以在脚本中任意位置使用这个函数,要使用这个函数,只要使用这个函数名称就可以了

 函数:把一个功能封装起来。使用时直接调用函数名。这样的脚本好处:模块化,代码可读性强。

2.1、函数创建

 两种·创建函数的方法:

 方法一:

 function name() {

            命令序列

      [return value]

 }

 方法二:

 name() {

            命令序列

      [return value]

 }

 注意:

 1、其中function是可选的,这个可以省略掉。name是函数唯一的名称,函数名后面加一个(),里面是没有内容的。而我们执行的命令序列放在{}里面的,由{ }包围的部分称为函数体,调用一个函数,实际上就是执行函数体中的代码。return value表示函数的返回值,其中 return 是 Shell 关键字,专门用在函数中返回一个值;这一部分可以写也可以不写。

 2、在定义函数的时候,必须要在执行程序前面定义或加载(即必须在调用函数地方之前,定义好函数)

 3、在执行函数时(调用函数),函数前面的function和函数后面的小括号都不要写,直接写函数名就可以了

2.2、函数的使用

 2.2.1、创建一个函数

 [root@localhost ~]# cat fun-1.sh

 #!/bin/bash

 # using a function in a script

  

 function func1() {              #定义函数

    echo "This is an example of a function"

 }

  

 count=1

 while [ $count -le 5 ]

 do

 func1                       #调用函数

 count=$[ $count + 1 ]

 done

  

 echo "This is the end of the while"

 func1                       #调用函数

 echo "Now this is the end of the script"

 [root@localhost ~]# sh fun-1.sh

 This is an example of a function

 This is an example of a function

 This is an example of a function

 This is an example of a function

 This is an example of a function

 This is the end of the while

 This is an example of a function

 Now this is the end of the script

2.2.2、函数返回值:函数的退出状态

 默认情况下,函数的退出状态是函数的最后一条命令返回的退出状态。

 [root@localhost ~]# cat fun-2.sh

 #!/bin/bash

 # testing the exit status of a function

  

 func1() {

     echo "trying to display a non-existent file"

     ls -l badfile

 }

  

 echo "testing the function:"

 func1

 echo "The exit status is: $?"

 [root@localhost ~]# sh fun-2.sh

 testing the function:

 trying to display a non-existent file

 ls: cannot access badfile: No such file or directory

 The exit status is: 2

2.2.3、函数返回值:return命令返回特定的退出码

 我们可以通过return命令来退出函数并返回特定的退出码

 return命令可以使用单个整数值来定义函数退出状态,提供了一种通过编程设置函数退出状态的简单方法。

 注:return语句返回一个退出值给调用函数的程序,而exit的返回值是给执行程序当前的SHELL

 [root@localhost ~]# cat fun-3.sh

 #!/bin/bash

 fun1(){

  ls badfile

  return 2

  echo "This is apple"

  return 5

 }

 fun1

 echo result is $?

 [root@localhost ~]# sh fun-3.sh

 ls: cannot access badfile: No such file or directory

 result is 2

在使用这个返回状态码的时候,要注意以下2点:

状态码必须要在函数一结束就进行取值。

 状态码的取值范围(0~255)

2.3、把函数值赋给变量使用

 正如命令输出可以捕获并存放到 shell 变量中一样,函数的输出也可以捕获并存放到 shell 变量中。

 范例1:

 [root@localhost ~]# cat fun-4.sh

 #!/bin/bash

 fun1(){

  read -p "Input a value: " VAR

  echo $[$VAR*5]

 }

  

 NUM=$(fun1)

 echo current num is $NUM

 [root@localhost ~]# sh fun-4.sh

 Input a value: 10

 current num is 50

2.4、向函数传递参数

2.4.1、 第一种情况

 函数可以使用位置参数变量来表示要传递给函数的参数。

 函数名在变量 $0 中定义, 函数命令行的其他参数使用变量 $1、$2..., $#可以用来确定传递给函数的参数数目。

 [root@localhost ~]# cat fun-5.sh

 #!/bin/bash

 function addem() {

     if [ $# -eq 0 ] || [ $# -gt 2 ]

     then

         echo "error,need to input one or two number."

     elif [ $# -eq 1 ]

     then

         echo $[ $1 + $1 ]

     else

         echo $[ $1 + $2 ]

     fi

 }

  

 echo -n "Adding 10 and 15: "

 value=`addem 10 15`

 echo $value

  

 echo -n "Let's try adding just one number: "

 value=`addem 10`

 echo $value

  

 echo -n "Now trying adding no numbers: "

 value=`addem`

 echo $value

  

 echo -n "Finally, try adding three numbers: "

 value=`addem 10 15 20`

 echo $value

 执行脚本测试:

 [root@localhost ~]# sh fun-5.sh

 Adding 10 and 15: 25

 Let's try adding just one number: 20

 Now trying adding no numbers: error,need to input one or two number.

 Finally, try adding three numbers: error,need to input one or two number.

2.4.2、 第二种情况

 函数无法从脚本命令行(shell提示符中)直接访问脚本参数值。如果想在函数中使用这些值,那么必须在调用该函数时手动传递这些数据。

 [root@localhost ~]# cat fun-6.sh

 #!/bin/bash

 # trying to access script parameters inside a function

  

 function func6 {

     echo $[ $1 * $2 ]

 }

  

 if [ $# -eq 2 ]

 then

     value=`func6 $1 $2`

     echo "The result is $value"

 else

     echo "Usage: badtest1 a b"

 fi

  [root@localhost ~]# sh fun-6.sh 4 6

 The result is 24

3、跳出循环

 在我们使用循环语句进行循环的过程中,有时候需要在未达到循环结束条件时强制跳出循环,那么Shell给我们提供了两个命令来实现该功能:break和continue

 注:exit:退出当前整个脚本的

3.1、Break

 结束并跳出当前循环,在forwhile等循环语句中,用于跳出当前所在的循环体,执行循环体之后的语句,后面如果什么也不加,表示跳出当前循环等价于break 1,也可以在后面加数字,假设break 3表示跳出三层循环

范例1:

 [root@localhost ~]# cat break.sh

 #!/bin/bash

 while true

 do

  read -p "Enter a number [1-5]:" num

  case $num in

            1|2|3|4|5)

                              echo "GREATER"

                              ;;

                     *)

                              echo "wrong...Bye"

                              break

  esac

 done 

 [root@localhost ~]# sh break.sh

 Enter a number [1-5]:3

 GREATER

 Enter a number [1-5]:4

 GREATER

 Enter a number [1-5]:5

 GREATER

 Enter a number [1-5]:1

 GREATER

 Enter a number [1-5]:2

 GREATER

 Enter a number [1-5]:3

 GREATER

 Enter a number [1-5]:34

 wrong...Bye

3.2、Continue

 它和break命令差不多,忽略本次循环剩余的代码,直接进行下一次循环;在forwhile等循环语句中,用于跳出当次循环,直接进入下一次循环。

范例2:

 [root@localhost ~]# cat continue.sh

 #!/bin/bash

 while true

 do

     echo -n "Input a number between 1 to 5: "

     read aNum

     case $aNum in

         1|2|3|4|5) echo "Your number is $aNum!"

         ;;

         *)

         echo "You do not select a number between 1 to 5!"

         continue

         echo "Game is over!"

         ;;

     esac

 done

范例3添加用户的脚本

 [root@localhost ~]# cat add_user.sh

 #!/bin/bash

 while true

 do

   read -p "Please enter prefix & passwd & number: " pre pass num

   printf  "user information:

    ***********************

    user prefix:   $pre

    user password: $pass

    user number:   $num

   "

   read -p "Are you sure?[y/n] " action

   if [ "$action" == "y" ];then

      break

   fi

 done

 #adduser

 for i in $(seq -w $num)

 do

  user=${pre}${i}

  id $user &> /dev/null

  if [ $? -ne 0 ];then

    useradd $user

    echo "$pass"|passwd --stdin $user &> /dev/null

    if [ $? -eq 0 ];then

    echo -e "\e[31m$user\e[0m create"

    fi

  else

    echo "User $user exist"

  fi

 done

 执行脚本:

 

 删除用户:

 [root@localhost ~]# for i in `seq -w 1 11`;do userdel -r tech$i;done

posted @ 2020-08-08 13:44  轩辕吊雷  阅读(1217)  评论(0编辑  收藏  举报