代码编织梦想

7 函数

shell函数格式:没有返回值类型+参数列表
格式:[function] 函数名[()]{
        statement;
        [return 返回值;]
     }

7.1 函数中涉及的变量

function test1(){
   echo '$0='$0;# 获取执行命令:./function.sh
   echo '$*='$*;# 获取所有参数列表:2 3 4 5 6
   echo '$@='$@;# 获取所有参数列表:2 3 4 5 6
   echo '$#='$#;# 获取参数列表个数:5
   echo '$1='$1;# 获取第一个参数: 2
   echo '$2='$2;# 获取第二个参数: 3
   echo '$$='$$;# 获取进程id
}
test1 2 3 4 5 6;

7.2 练习

function add1(){
   sumab=$(($1+$2));
   return $sumab;
}
add1 3 4;
sum1=$?;#获取上一个命令的结果
echo "3+4="$sum1;


#遍历获取所有的参数
function test2(){
  geShu=1;
  for i in $*;do
      echo '第'$geShu'个参数的值是:'$i;
	  geShu=$((geShu+1));
  done;
}
test2 2 9 7 1 6;


#* 判断一个数是不是质数
function pdZhiShu(){
   if [ $1 -le 1 ]; then
        return 1;
   fi;
   for ((m=2;m<$1;m++));do
        if [ $(($1%m)) -eq 0 ];then
		      return 1;
		fi;
   done;
   return 0;#返回true/false不行
}
#调用
for ((n=0;n<=10;n++));do
   pdZhiShu $n;
   result=$?;
   if [ $result -eq 0 ];then
       echo $n'是质数';
   fi;
done;

#* 判断参数年参数月有多少天
function test3(){
    days=0;
    year=$1;month=$2;
	case $month in
	     4 | 6 | 9 | 11)
           days=30;;	
		 2)
           if [ $((year%4)) -eq 0 -a $((year%100)) -ne 0 -o $((year%400)) -eq 0 ];then
                  days=29;
           else
		          days=28;
           fi;;
         *)
           days=31;;
	esac;
	return $days;	     		   
}
for ((year=2000;year<=2020;year++));do
    for ((month=1;month<=12;month++));do
	      test3 $year $month;
          echo $year'年'$month'月有'$?'天!';		  
	done;
done;
#* 证明所有四位数进入黑洞的次数<=7 
function heiDong(){
   for ((n=1000;n<10000;n++));do
       if [ $((n%1111)) -eq 0 ];then
	       continue;
	   fi;
	   k=$n;
	   ciShu=0;
	   while [ $k -ne 6174 ];do
	       #获取各个位数的值
		   arr=($((k%10)) $((k/10%10)) $((k/100%10)) $((k/1000)));
		   #echo ${arr[*]};
		   #数组排序
		   for ((i=0;i<${#arr[*]}-1;i++));do
		        for ((j=i+1;j<${#arr[*]};j++));do
				      if [ ${arr[i]} -gt ${arr[j]} ];then
		                 temp=${arr[i]};arr[i]=${arr[j]};arr[j]=$temp;
					  fi;	 
		        done;
		   done;
		   #echo ${arr[*]};
		   maxArr=$((${arr[3]}*1000+${arr[2]}*100+${arr[1]}*10+${arr[0]}*1));
		   minArr=$((${arr[0]}*1000+${arr[1]}*100+${arr[2]}*10+${arr[3]}*1));
		   k=$((maxArr-minArr));
		   ciShu=$((ciShu+1));
	   done;
	   echo $n'经过'$ciShu'次运算后得到'$k;
   done;
}
#heiDong;


#* 斐波那契数列:写一个方法获取第n个数: 1 1 2 3 5 8 13 21
function fbnq1(){
   arg=$1;
   #使用数组
   arr=(1 1);
   for ((i=2;i<arg;i++));do
      arr[$i]=$((${arr[i-1]}+${arr[i-2]}));  
   done;
   echo ${arr[*]};
   #echo '${arr[arg-1]}'${arr[arg-1]}'---$((arg-1))'$((arg-1));
   resultFbnq=${arr[arg-1]};
}
for ((n=1;n<=20;n++));do
   fbnq1 $n;
   echo   '第'$n'个元素是:'$resultFbnq;
done;

7.3 注意return的作用

#探究result的返回值:return后面的值 不是返回值是状态码:从0到255
function test3(){
   return 1597;
}
test3;echo '1597='$?;

#设置返回值值的正确方式
function test4(){
   result4=$(($1+1));
}
test4 3;
echo '3+1='$result4;

在这里插入图片描述

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/mtb278750/article/details/130221228

linux shell函数返回值_iteye_11539的博客-爱代码爱编程

Shell函数返回值,一般有3种方式:return,argv,echo 1) return 语句 shell函数的返回值,可以和其他语言的返回值一样,通过return语句返回。 示例: #!/bin/bash - function mytest() { echo "arg1 = $1" if [ $1 = "1" ] ;the

/etc/init.d/functions _lilrq的博客-爱代码爱编程

functions这个脚本是给/etc/init.d里边的文件使用的。提供了一些基础的功能,看看里边究竟有些什么。首先会设置umask,path,还有语言环境,然后会设置 success,failure,warning,normal几种情况下的字体颜色。下面再看看提供的重要方法: checkpid:检查是否已存在pid,如果有一个存在,返回0(通过查看/p

linux shell脚本之函数 function 详解_lee木木的博客-爱代码爱编程_linux脚本函数

Linux shell脚本之函数Function 函数详解函数语法函数的生命周期函数返回值函数参数变量作用域1、本地变量2、局部变量函数变量示例 函数递归递归示例 函数示例 函数详解 在过程式

Shell函数(return问题和参数问题)-爱代码爱编程

linux shell 可以用户定义函数,然后在shell脚本中可以随便调用。 shell中函数的定义格式如下: [ function ] funname () { action [return int] } 返回值return: 1、return的值是int型。 2、return的值通过$?来接收,一般在调用方法时接收,有显示retu

Linux_function-爱代码爱编程

LINUX 函数 目录 int open(const char* pathname, int flags, mode_t mode); int close(int fd); ssize_t read(int fd, void* buf, size_t count); ssize_t write(int fd, const void* buf, s

shell中return-爱代码爱编程

1、return常用在shell脚本函数中,而非命令行。 2、return 也可作用在shell脚本,执行方式为: source xx.sh ,不然报错(不推荐用) return: can only `return' from a function or sourced script 3、return 的作用是退出当前函数,不退出整个脚本 4、函数中

【运维】linux shell 编程之函数使用_逆风飞翔的小叔的博客-爱代码爱编程

前言 使用linux的shell编程,可以说函数是非常重要的内容,也是在编写各类shell脚本的时候经常用到的,本篇将介绍下函数相关的使用。 shell 函数分类 系统函数自定义函数 系统函数 系统函数为linux自带的函数, 可以在shell编写中直接使用。下面介绍几种常用的系统函数 1、basename 用于获取文件名

shell函数:基本语法、函数传参、函数变量、返回值_shell脚本函数返回值-爱代码爱编程

文章目录 函数函数基本语法函数传参函数变量返回值调用 函数 函数基本语法 shell 可以用户定义函数,然后在shell脚本中可以随便调用。 格式 [ function ] fu

linux shell编程之函数(function)详解_linux shell function-爱代码爱编程

Linux shell 支持用户定义的函数,你可以将shell脚本代码放进函数中封装起来,这样就能在脚本中的任何地方多次使用它。 1、基本的shell函数   函数(function)是可以起个名字并在代码中任何位置重用的代码块。你要在脚本中使用该代码块时,只要使用分配的函数名就可以了(这个过程就称为调用函数)。 (1)创建函数 有2种格式可以