首页 > 其他分享 >shift位置参数左移、函数、退出循环

shift位置参数左移、函数、退出循环

时间:2023-04-17 23:32:12浏览次数:36  
标签:函数 shift 左移 number echo sh nfs2 root

shift位置参数左移、函数、退出循环

Shift位置参数左移指令

  • shift命令用于对位置参数的移动(左移),通常用于在不知道传入参数个数的情况下依次遍历每个参数然后进行相应处理。
  • 每执行一次,位置参数序列顺次左移一个位置,$#的值减1,用于分别处理每个参数,移出去的参数。
[root@nfs2 ~]# cat shift.sh
 #!/bin/bash
 if [ $# -le 0 ];then
        echo "没有提供足够的参数"
  exit 1
 fi
 sum=0
 while [ $# -gt 0 ];do
        sum=$(expr $sum + $1)
        shift
 done
 echo “result is $sum”
[root@nfs2 ~]# sh shift.sh
没有提供足够的参数
[root@nfs2 ~]# sh shift.sh 1 2 3
“result is 6”
[root@nfs2 ~]# sh shift.sh 1 2 3 -4
“result is 2”
[root@nfs2 ~]# sh -x shift.sh 1 3 5 10 20
+ '[' 5 -le 0 ']'
+ sum=0
+ '[' 5 -gt 0 ']'
++ expr 0 + 1
+ sum=1
+ shift
+ '[' 4 -gt 0 ']'
++ expr 1 + 3
+ sum=4
+ shift
+ '[' 3 -gt 0 ']'
++ expr 4 + 5
+ sum=9
+ shift
+ '[' 2 -gt 0 ']'
++ expr 9 + 10
+ sum=19
+ shift
+ '[' 1 -gt 0 ']'
++ expr 19 + 20
+ sum=39
+ shift
+ '[' 0 -gt 0 ']'
+ echo “result is 39”
“result is 39”
  • shift 位置参数的左移: shift [n]: n是个正整数,表示位置参数左移的个数
[root@nfs2 ~]# cat shift1.sh
 #!/bin/bash
 echo "First and Seconde pos argu:$1"
 shift
 echo "Third pos argu: $1"
[root@nfs2 ~]# sh shift1.sh 100 900
First and Seconde pos argu:100
Third pos argu: 900
  • shift的默认值为1,每次只左移一个位置参数
[root@nfs2 ~]# cat shift1.sh
 #!/bin/bash
 echo "First and Seconde pos argu:$1,$2"
 shift 2
 echo "Third pos argu: $1"
[root@nfs2 ~]# sh shift1.sh 100 110 200
First and Seconde pos argu:100,110
Third pos argu: 200

函数的使用

  • 函数是一个脚本代码块,你可以对它进行自定义命名,并且可以在脚本中任意位置使用这个函数,要使用这个函数,只要使用这个函数名称就可以了
    函数:把一个功能封装起来。使用时直接调用函数名。这样的脚本好处:模块化,代码可读性强。

函数创建

  • 方法一
function name() {
 		命令序列
      [return value]
 }
  • 方法二
name() {
 		命令序列
      [return value]
 }

函数的使用

  • 创建一个函数
[root@nfs2 ~]# 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=$(expr $count + 1)
 let count++
 done

 echo "This is the end of the while"
 func1                       #调用函数
 echo "Now this is the end of the script"
[root@nfs2 ~]# 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
  • 函数返回值:函数的退出状态 默认情况下,函数的退出状态是函数的最后一条命令返回的退出状态。
[root@nfs2 ~]# 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@nfs2 ~]# 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
  • 函数返回值:return命令返回特定的退出码 return命令可以使用单个整数值来定义函数退出状态,提供了一种通过编程设置函数退出状态的简单方法。 注:return语句返回一个退出值给调用函数的程序,而exit的返回值是给执行程序当前的SHELL
[root@nfs2 ~]# sh fun-3.sh
ls: cannot access badfile: No such file or directory
result is 2
[root@nfs2 ~]# cat fun-3.sh
 #!/bin/bash
 fun1(){
        ls badfile
        return 2
        echo "This is apple"
        return 5
 }
 fun1
 echo result is $?
[root@nfs2 ~]# sh fun-3.sh
ls: cannot access badfile: No such file or directory
result is 2
  • 在使用这个返回状态码的时候,要注意以下2点:
  • 码必须要在函数一结束就进行取值。
  • 码的取值范围(0~255)

把函数值赋给变量使用

  • 函数的输出也可以捕获并存放到 shell 变量中。
[root@nfs2 ~]# cat fun-4.sh
 #!/bin/bash
 fun1(){
        read -p "Input a value: " VAR
        echo $[ $VAR * 5 ]
 }

 NUM=$(fun1)
 echo current num is $NUM
[root@nfs2 ~]# sh -x fun-4.sh
++ fun1
++ read -p 'Input a value: ' VAR
Input a value: 5
++ echo 25
+ NUM=25
+ echo current num is 25
current num is 25

向函数传递参数

  • 函数可以使用位置参数变量来表示要传递给函数的参数。 函数名在变量 $0 中定义, 函数命令行的其他参数使用变量 $1、$2..., $#可以用来确定传递给函数的参数数目。
  • 第一种情况
[root@nfs2 ~]# 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 $(expr $1 + $1)
     else
         echo $(expr $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@nfs2 ~]# 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.
  • 第二种情况
  • 函数无法从脚本命令行(shell提示符中)直接访问脚本参数值。如果想在函数中使用这些值,那么必须在调用该函数时手动传递这些数据。
[root@nfs2 ~]# cat fun-6.sh
 #!/bin/bash
 # trying to access script parameters inside a function

 function func6 {
     echo $(expr $1 \* $2)
 }

 if [ $# -eq 2 ]
 then
     value=`func6 $1 $2`
     echo "The result is $value"
 else
     echo "Usage: badtest1 a b"
 fi
[root@nfs2 ~]# sh fun-6.sh 4 6
The result is 24
[root@nfs2 ~]# sh -x fun-6.sh 4 6
+ '[' 2 -eq 2 ']'
++ func6 4 6
+++ expr 4 '*' 6
++ echo 24
+ value=24
+ echo 'The result is 24'
The result is 24

跳出循环

  • Break
  • 语句进行循环的过程中,有时候需要在未达到循环结束条件时强制跳出循环,Shell提供了两个命令来实现该功能:break和continue 注:exit:退出当前整个脚本的
[root@nfs2 ~]# 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@nfs2 ~]# sh break.sh
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]:4
GREATER
Enter a number [1-5]:5
GREATER
Enter a number [1-5]:33
wrong...Bye
  • Continue
  • 它和break命令差不多,忽略本次循环剩余的代码,直接进行下一次循环;在for、while等循环语句中,用于跳出当次循环,直接进入下一次循环。
[root@nfs2 ~]# 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
[root@nfs2 ~]# sh continue.sh
Input a number between 1 to 5: 1
Your number is 1!
Input a number between 1 to 5: 2
Your number is 2!
Input a number between 1 to 5: 3
Your number is 3!
Input a number between 1 to 5: 4
Your number is 4!
Input a number between 1 to 5: 5
Your number is 5!
Input a number between 1 to 5: 33
You do not select a number between 1 to 5!
Input a number between 1 to 5: 1
Your number is 1!
Input a number between 1 to 5: 2
Your number is 2!
Input a number between 1 to 5: ^C
  • 添加用户脚本
[root@nfs2 ~]# 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@nfs2 ~]# sh add_user.sh
Please enter prefix & passwd & number: aabc 123 10
user information:
        ***********************
        user prefix:   aabc
        user password: 123
        user number:   10
   Are you sure?[y/n] y
aabc01 create
aabc02 create
aabc03 create
aabc04 create
aabc05 create
aabc06 create
aabc07 create
aabc08 create
aabc09 create
aabc10 create

标签:函数,shift,左移,number,echo,sh,nfs2,root
From: https://blog.51cto.com/u_15975105/6196416

相关文章

  • 函数类--递归
    一、问题描述:在主程序中提示输入整数n,编写函数用递归的方法求1+2+3+...+n的值。二、设计思路:  1.设计一个Sum函数,判断所输入的数字,如果为1,则即为1;如果不为1,则n加上函数本身(其中变成n-1),放在sum中。   直到加到1,返回计算得到的值。  2.定义主函数,输入所需要判断的......
  • ArcPy 批处理之 [ hdf转tif ]; [ Con函数 ]; 镶嵌至新栅格 [ Mosaic to New Raster ];
    一、 ArcPy批量将文件夹内的*.hdf文件转为*.tif 文件:#encoding:utf-8 ##hdf2tifimportarcpyimportosinPath=r'E:\Data\S00_DataHdf\\'outPath=r'E:\Data\S01_DataTif\\'fordirpath,dirnames,filenamesinos.walk(inPath):......
  • 函数参数中中括号后面跟逗号是什么意思?
    如:convertScaleAbs(src[,dst[,alpha[,beta]]])->dst函数里面有中括号和逗号,:中括号是可选参数,逗号是参数之间的分隔符[,a[,b]]:嵌套形式表示b是独立于a的可选参数,即在传入a的情况下,b可以自由地选择传入或省略。[,a,b]:扁平形式表示a与b合在一起是一组可选参......
  • 定义函数数组
    interfaceFunctionArrayInterface//定义接口,希望批量执行的函数用统一的名称定义在接口内{voidrunit();}classfuncAimplementsFunctionArrayInterface//函数A{publicvoidrunit(){System.out.println("你运行了函数func......
  • 基于遗传算法的最优潮流 以IEEE30节点的输电网为研究对象 以系统发电成本最小为目标函
    基于遗传算法的最优潮流 以IEEE30节点的输电网为研究对象以系统发电成本最小为目标函数以机组出力为优化变量其中出力与成本的关系是经典的二次函数关系 通过优化求解得到最佳机组出力ID:2550672838253871......
  • 带默认参数值的函数
    1.函数定义时可以预先声名默认的形式参数。调用时,如果给出实参,则用实参初始化形参;如果没有给出实参,则用默认形参值。  intadd(intx=1,inty=2){        //声明默认形参数值     returnx+y;   }   intmain(){     add(23,......
  • Vue3 ref函数处理基本类型或对象类型
    基本类型视频对象类型视频2.ref函数作用:定义一个响应式的数据语法:constxxx=ref(initValue)创建一个包含响应式数据的引用对象(reference对象,简称ref对象)。JS中操作数据:xxx.value模板中读取数据:不需要.value,直接:<div>{{xxx}}</div>备注:接收的数据可以是:基本......
  • C语言函数大全-- j 开头的函数
    C语言函数大全本篇介绍C语言函数大全–j开头的函数1.j0,j0f1.1函数说明函数声明函数功能doublej0(doublex);计算x的第一类0阶贝塞尔函数(double)floatj0f(floatx);计算x的第一类0阶贝塞尔函数(float)【笔者本地windows环境,无此函数】注意:如......
  • postgresql 函数错误捕捉
    CREATEORREPLACEFUNCTION"public"."proc_net_agent_diamond_loss"("dwuserid"int4,"strdate"date)RETURNS"public"."my_returninfo"AS$BODY$DECLAREresultMy_ReturnInfo;v_start_timeTIMESTAMP(......
  • 盘点Python内置函数sorted()高级用法实战
    今日鸡汤清川带长薄,车马去闲闲。大家好,我是Python进阶者。一、前言前几天在Python钻石交流群有个叫【emerson】的粉丝问了一个Python排序的问题,这里拿出来给大家分享下,一起学习下。其实这里【瑜亮老师】、【布达佩斯的永恒】等人讲了很多,只不过对于基础不太好的小伙伴们来说,还是有......