1 2 3 4

4Shell:跳出循环,shift左移,函数

1跳出循环

在我们使用循环语句进行循环的过程中,有时候需要在未达到循环结束条件时强制跳出循环,

那么shell给我们提供了两个命令来实现该功能

break和continue

  • break跳出整个循环
  • continue跳出本次循环,进行下次循环

break概述

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

continue概述

忽略本次循环剩余的代码,直接进行下一次循环
在for,while等循环语句中,用于跳出当前所在的循环体,执行循环体后面的语句
如果后面的数字是1,表示忽略本次条件循环,如果是2的话,忽略下来2次的循环

示例一:写一个菜单,当按数字键4时退出,否则一直循环显示

[root@VM_0_7_centos ~]# cat bc.sh 
#! /bin/bash
while true
do
	echo '**********'
	echo 'please select your operation'
	echo '1 copy'
	echo '2 delect'
	echo '3 backup'
	echo '4 quit'
	echo '**********'
    read op
  case $op in
    1)
      echo "your privileges are insufficient"
      ;;
    2)
      echo "your privileges are insufficient"
      ;;
    3)
      echo "your privileges are insufficient"
      ;;
    4)
      echo "exit ..."
      break
      ;;
    *)
      echo "please output the correct signal"
  esac
done

执行脚本文件

[root@VM_0_7_centos ~]# bash bc.sh 
**********
please select your operation
1 copy
2 delect
3 backup
4 quit
**********
3
your privileges are insufficient
**********
please select your operation
1 copy
2 delect
3 backup
4 quit
**********
4
exit ...

交互创建用户,密码

[root@VM_0_7_centos ~]# cat user.sh 
#! /bin/bash
while :
do
    read -p "please enter prefix & password & num:" pre pass num
    printf "user information:
    **********************
    user prefix: $pre
    user password: $pass
    user number: $num
    **********************
"
read -p "Are you sure? [y/n]" acrion
if [ "$action"=="y" ]; then
  break
fi
done
 
for i in $( seq $num )   #从i=1开始,取到$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 "\033[31m$user\033[0m creat"   #表示颜色,用红色来显示
    fi
  else
    echo "user $user exist"
  fi
done

执行脚本文件

[root@VM_0_7_centos ~]# bash user.sh 
please enter prefix & password & num:xue 123456 2
user information:
    **********************
    user prefix: xue
    user password: 123456
    user number: 2
    **********************
Are you sure? [y/n]y
xue1 creat
xue2 creat

查看passwd文件

[root@VM_0_7_centos ~]# grep xue /etc/passwd
xue1:x:1000:1000::/home/xue1:/bin/bash
xue2:x:1001:1001::/home/xue2:/bin/bash

shift参数左移指令

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

常用于linux中各种程序的启动脚本

加法计算器

[root@localhost ~]# cat 1.sh 
#! /bin/bash
if [ $# -le 0 ]
then
	echo "没有足够的参数"
	exit
fi
sum=0
while [ $# -gt 0 ]
do
	sum=$[ $sum+$1 ]
	shift
done
echo result is $sum

[root@localhost ~]# bash 1.sh 11 11 113 3
result is 138

函数

函数是一个脚本代码块
优点:模块化,代码可读性强

语法

函数创建

方法1
function name(){
	commands
}

方法2
name(){
	commands
}

调用函数语法

函数名 参数1 参数2 ...

调用函数时,可以传递参数
在函数中用$1,$2...来引用传递的参数

[root@localhost ~]# cat fun1.sh 
#! /bin/bash
function n1(){
  echo "aaa"
}
n1
[root@localhost ~]# bash fun1.sh 
aaa

注意:如果在一个脚本中定义了重复的函数名,那么以最后一个为准

返回值

使用return命令
退出函数并返回特定的退出码
后期根据退出码来判断是哪种错误
一般写在最后

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

[root@localhost ~]# cat fun2.sh 
#! /bin/bash
function fun1 {
  echo "this is function"
  return 3
}
fun1
[root@localhost ~]# bash fun2.sh 
this is function
[root@localhost ~]# echo $?
3

exit和return两者的区别?

  • exit 数字
    • exit整个脚本就直接退出,返回数字
  • return 数字
    • return只是在函数最后添加一行,然后返回数字,只能让函数后面的命令不执行无法强制退出整个脚本的

把函数赋值给变量

[root@localhost ~]# cat fun3.sh 
#! /bin/bash
fun1(){
  read -p "input a value:" va
  echo $[ $va+$va ]
}
num=$(fun1)
echo current num is $num

[root@localhost ~]# bash fun3.sh 
input a value:11
current num is 22
[root@localhost ~]# bash fun3.sh 
input a value:1112
current num is 2224

函数的参数传递

一个参数
[root@localhost ~]# cat fun4.sh 
#! /bin/bash
fun1(){
  rm -rf $1
}
fun1 $1

[root@localhost ~]# chmod +x ./fun4.sh 
[root@localhost ~]# ./fun4.sh fun3.sh

[root@localhost ~]# ls fun3.sh 
ls: cannot access fun3.sh: No such file or directory

调用函数时,直接传递参数
[root@localhost ~]# cat fun4.sh 
#! /bin/bash
fun1(){
  rm -rf $1
}
fun1 /root/1.txt
[root@localhost ~]# ./fun4.sh 
[root@localhost ~]# ls 1.txt
ls: cannot access 1.txt: No such file or directory

多个参数传递和使用
[root@localhost ~]# cat fun4.sh 
#! /bin/bash
fun1(){
  echo $[ $1*$1 ]
  echo $[ $2*$2 ]
}
fun1 2 5

[root@localhost ~]# ./fun4.sh 
4
25

函数中变量的处理

函数使用的变量类型有两种

  • 全局变量
    • 默认情况下,你在脚本中定义的变量都是全局变量.因此在函数外面定义的变量在函数内也可以使用
  • 局部变量
    • 在函数内部定义的变量,在函数外部无法使用
[root@localhost ~]# cat fun4.sh 
#! /bin/bash
fun1(){
  num1=$[ $var1*$var1 ]
}
read -p 'input a num:' var1
fun1
echo the new value is:$num1

[root@localhost ~]# bash fun4.sh 
input a num:15
the new value is:225

posted @ 2020-01-07 17:01  多走多看  阅读(225)  评论(0编辑  收藏  举报