在 Shell 脚本中调用另一个 Shell 脚本的三种方式以及返回值问题 |
您所在的位置:网站首页 › 如何用shell脚本写调用另一个脚本的方法 › 在 Shell 脚本中调用另一个 Shell 脚本的三种方式以及返回值问题 |
https://blog.csdn.net/simple_the_best/article/details/76285429 脚本调用: 先来说一下主要以下有几种方式: fork: 如果脚本有执行权限的话,path/to/foo.sh。如果没有,sh path/to/foo.sh。新开启子shell,需要在父shell定义环境变量的变量子shell才可以使用可以继承环境变量。在脚本中定义环境 变量export a="111" 或 delcare -x a="aaaa"exec: exec path/to/foo.sh 不开新shell使用当前shell,但是父shell的exec之后就不执行了。需要在父shell定义环境变量的变量子shell才可以使用。在脚本中定义环境 变量export a="111" 或 delcare -x a="aaaa"source: source path/to/foo.sh 不新开shell使用当前shell所以父shell的环境变量子shell都可以直接使用。 forkfork 是最普通的, 就是直接在脚本里面用 path/to/foo.sh 来调用 foo.sh 这个脚本,比如如果是 foo.sh 在当前目录下,就是 ./foo.sh。运行的时候 terminal 会新开一个子 Shell 执行脚本 foo.sh,子 Shell 执行的时候, 父 Shell 还在。子 Shell 执行完毕后返回父 Shell。 子 Shell 从父 Shell 继承环境变量,但是子 Shell 中的环境变量不会带回父 Shell。 execexec 与 fork 不同,不需要新开一个子 Shell 来执行被调用的脚本. 被调用的脚本与父脚本在同一个 Shell 内执行。但是使用 exec 调用一个新脚本以后, 父脚本中 exec 行之后的内容就不会再执行了。这是 exec 和 source 的区别. source与 fork 的区别是不新开一个子 Shell 来执行被调用的脚本,而是在同一个 Shell 中执行. 所以被调用的脚本中声明的变量和环境变量, 都可以在主脚本中进行获取和使用。 其实从命名上可以感知到其中的细微区别,下面通过两个脚本来体会三种调用方式的不同: 第一个脚本,我们命名为 1.sh: #!/usr/bin/env bash A=1 echo "before exec/source/fork: PID for 1.sh = $$" export A echo "In 1.sh: variable A=$A" case $1 in --exec) echo -e "==> using exec…\n" exec ./2.sh ;; --source) echo -e "==> using source…\n" . ./2.sh ;; *) echo -e "==> using fork by default…\n" ./2.sh ;; esac echo "after exec/source/fork: PID for 1.sh = $$" echo -e "In 1.sh: variable A=$A\n"第二个脚本,我们命名为 2.sh: #!/usr/bin/env bash echo "PID for 2.sh = $$" echo "In 2.sh get variable A=$A from 1.sh" A=2 export A echo -e "In 2.sh: variable A=$A\n"注:这两个脚本中的参数 $$ 用于返回脚本的 PID , 也就是进程 ID。这个例子是想通过显示 PID 判断两个脚本是分开执行还是同一进程里执行,也就是是否有新开子 Shell。当执行完脚本 2.sh 后,脚本 1.sh 后面的内容是否还执行。 chmod +x 1.sh 2.sh 给两个脚本加上可执行权限后执行情况: forkfork 方式可以看出,两个脚本都执行了,运行顺序为1-2-1,从两者的PID值(1.sh PID=82266, 2.sh PID=82267),可以看出,两个脚本是分成两个进程运行的。 execexec 方式运行的结果是,2.sh 执行完成后,不再回到 1.sh。运行顺序为 1-2。从pid值看,两者是在同一进程 PID=82287 中运行的。 sourcesource方式的结果是两者在同一进程里运行。该方式相当于把两个脚本先合并再运行。 CommandExplanationfork新开一个子 Shell 执行,子 Shell 可以从父 Shell 继承环境变量,但是子 Shell 中的环境变量不会带回给父 Shell。exec在同一个 Shell 内执行,但是父脚本中 exec 行之后的内容就不会再执行了source在同一个 Shell 中执行,在被调用的脚本中声明的变量和环境变量, 都可以在主脚本中进行获取和使用,相当于合并两个脚本在执行。参考: - 在shell脚本中调用另一个脚本的三种不同方法(fork, exec, source)
返回值: https://blog.csdn.net/hongweigg/article/details/78978295 https://blog.csdn.net/hongweigg/article/details/78978295
Shell函数返回值,一般有3种方式:return,argv,echo 1) return 语句 shell函数的返回值,可以和其他语言的返回值一样,通过return语句返回。 示例: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 #!/bin/bash - function mytest() { echo "arg1 = $1" if [ $1 = "1" ] ;then return 1 else return 0 fi }
echo echo "mytest 1" mytest 1 echo $? # print return result
echo echo "mytest 0" mytest 0 echo $? # print return result
echo echo "mytest 2" mytest 2 echo $? # print return result
echo echo "mytest 1 = "`mytest 1` if mytest 1 ; then echo "mytest 1" fi
echo echo "mytest 0 = "`mytest 0` if mytest 0 ; then echo "mytest 0" fi
echo echo "if fasle" # if 0 is error if false; then echo "mytest 0" fi
echo mytest 1 res=`echo $?` # get return result if [ $res = "1" ]; then echo "mytest 1" fi
echo mytest 0 res=`echo $?` # get return result if [ $res = "0" ]; then echo "mytest 0" fi
echo echo "end" 结果: mytest 1 arg1 = 1 1 mytest 0 arg1 = 0 0 mytest 2 arg1 = 2 0 mytest 1 = arg1 = 1 arg1 = 1 mytest 0 = arg1 = 0 arg1 = 0 mytest 0 if fasle arg1 = 1 mytest 1 arg1 = 0 mytest 0 end 先定义了一个函数mytest,根据它输入的参数是否为1来return 1或者return 0. 获取函数的返回值通过调用函数,或者最后执行的值获得。 另外,可以直接用函数的返回值用作if的判断。 注意:return只能用来返回整数值,且和c的区别是返回为正确,其他的值为错误。 2) argv全局变量 这种就类似于C语言中的全局变量(或环境变量)。 示例: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #!/bin/bash -
g_var= function mytest2() { echo "mytest2" echo "args $1" g_var=$1
return 0 }
mytest2 1 echo "return $?"
echo echo "g_var=$g_var" 结果: mytest2 args 1 return 0 g_var=1 函数mytest2通过修改全局变量的值,来返回结果。 注: 以上两个方法失效的时候 以上介绍的这两种方法在一般情况下都是好使的,但也有例外。 示例: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #!/bin/bash -
function mytest3() { grep "123" test.txt | awk -F: '{print $2}' | while read line ;do echo "$line" if [ $line = "yxb" ]; then return 0 # return to pipe only fi done
echo "mytest3 here " return 1 # return to main process }
g_var= function mytest4() { grep "123" test.txt | awk -F: '{print $2}' | while read line ;do echo "$line" if [ $line = "yxb" ]; then g_var=0 echo "g_var=0" return 0 # return to pipe only fi done
echo "mytest4 here " return 1 }
mytest3 echo $?
echo mytest4 echo $?
echo echo "g_var=$g_var" 其中,test.txt 文件中的内容如下: 456:kkk 123:yxb 123:test 结果: yxb mytest3 here 1 yxb g_var=0 mytest4 here 1 g_var= 可以看到mytest3在return了以后其实没有直接返回,而是执行了循环体后的语句,同时看到mytest4中也是一样,同时,在mytest4中,对全局变量的修改也无济于事,全局变量的值根本就没有改变。这个是什么原因那? 笔者认为,之所以return语句没有直接返回,是因为return语句是在管道中执行的,管道其实是另一个子进程,而return只是从子进程中返回而已,只是while语句结束了。而函数体之后的语句会继续执行。 同理,全局变量在子进程中进行了修改,但是子进程的修改没有办法反应到父进程中,全局变量只是作为一个环境变量传入子进程,子进程修改自己的环境变量,不会影响到父进程。 因此在写shell函数的时候,用到管道(cmd &后台进程也一样)的时候一定要清楚此刻是从什么地方返回。 3) echo 返回值 其实在shell中,函数的返回值有一个非常安全的返回方式,即通过输出到标准输出返回。因为子进程会继承父进程的标准输出,因此,子进程的输出也就直接反应到父进程。因此不存在上面提到的由于管道导致返回值失效的情况。 在外边只需要获取函数的返回值即可。 示例: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #!/bin/bash
############################################## # Author : IT-Homer # Date : 2012-09-06 # Blog : http://blog.csdn.net/sunboy_2050 ##############################################
function mytest5() { grep "123" test.txt | awk -F: '{print $2}' | while read line; do if [ $line = "yxb" ]; then echo "0" # value returned first by this function return 0 fi done
return 1 }
echo '$? = '"$?" result=$(mytest5)
echo "result = $result"
echo if [ -z $result ] # string is null then echo "no yxb. result is empyt" else echo "have yxb, result is $result" fi 结果: $? = 0 result = 0 have yxb, result is 0 这个方式虽然好使,但是有一点一定要注意,不能向标准输出一些不是结果的东西,比如调试信息,这些信息可以重定向到一个文件中解决,特别要注意的是,用到比如grep这样的命令的时候,一定要记得1>/dev/null 2>&1来避免这些命令的输出。 您可能感兴趣的文章: linux shell自定义函数(定义、返回值、变量作用域)介绍shell脚本中执行python脚本并接收其返回值的例子PowerShell函数一次返回多个返回值示例linux shell 自定义函数方法(定义、返回值、变量作用域)Shell中函数返回值超出问题PowerShell函数指定返回值类型实例Shell中关于处理方法返回值问题详解 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |