shell for循环、循环变量值付给其他shell脚本的方法
本文主要将在shell中如何编写for循环,并将循环变量作为下个shell脚本的参数。
shellfor循环:
#!第一种写法类似C、Java for((i=1;i<=100;i++)) do echo$i done #!第二种写法in应用 foriin{1..100} do echo$i done #!第三种写法seq使用 foriin`seq1100` do echo$i done
将循环变量赋值到下一个脚本:
在运行shell脚本时候,有三种方式来调用外部的脚本,exec(execscript.sh)、source(sourcescript.sh)、fork(./script.sh)
1、exec(exec/home/script.sh):
使用exec来调用脚本,被执行的脚本会继承当前shell的环境变量。但事实上exec产生了新的进程,他会把主shell的进程资源占用并替换脚本内容,继承了原主shell的PID号,即原主shell剩下的内容不会执行。
2、source(source/home/script.sh)
使用source或者“.”来调用外部脚本,不会产生新的进程,继承当前shell环境变量,而且被调用的脚本运行结束后,它拥有的环境变量和声明变量会被当前shell保留,类似将调用脚本的内容复制过来直接执行。执行完毕后原主shell继续运行。
3、fork(/home/script.sh)
直接运行脚本,会以当前shell为父进程,产生新的进程,并且继承主脚本的环境变量和声明变量。执行完毕后,主脚本不会保留其环境变量和声明变量。
#!main.sh主体 #!/bin/sh a=main echo"ais$a" echo"PIDforparentbefore2.sh:$$" case$1in exec) echo"usingexec" exec./2.sh;; *) echo"usingsourcing" source./2.sh;; esac echo"PIDFORparentafter2.sh:$$" echo"nowm"
#!2.sh #!/bin/sh echo"PIDFOR2.SH:$$" echo"2.shgetafrommain.shis$a" a=2.sh exporta b=3.sh echo"now2.shais$a"
执行结果:
aismain PIDforparentbefore2.sh:1162 usingsourcing PIDFOR2.SH:1162 2.shgetafrommain.shismain`这里写代码片` now2.shais2.sh PIDFORparentafter2.sh:1162 nowm
通过for循环,循环变量作为2.sh变量赋值并执行。
#!main主函数 #!/bin/sh a=0 for((i=1;i<=10;i++)) do a=$i echo"ais$a" echo"PIDforparentbefore2.sh:$$" echo"usingsourcing" source./2.sh echo"PIDFORparentafter2.sh:$$" echo"nowais$a" done
输出结果:
ais1 PIDforparentbefore2.sh:1339 usingsourcing PIDFOR2.SH:1339 2.shgetafrommain.shis1 now2.shais2.sh PIDFORparentafter2.sh:1339 nowais2.sh ais2 PIDforparentbefore2.sh:1339 usingsourcing PIDFOR2.SH:1339 2.shgetafrommain.shis2 now2.shais2.sh PIDFORparentafter2.sh:1339 nowais2.sh ais3 PIDforparentbefore2.sh:1339 usingsourcing PIDFOR2.SH:1339 2.shgetafrommain.shis3 now2.shais2.sh PIDFORparentafter2.sh:1339 nowais2.sh ais4 PIDforparentbefore2.sh:1339 usingsourcing PIDFOR2.SH:1339 2.shgetafrommain.shis4 now2.shais2.sh PIDFORparentafter2.sh:1339 nowais2.sh ais5 PIDforparentbefore2.sh:1339 usingsourcing PIDFOR2.SH:1339 2.shgetafrommain.shis5 now2.shais2.sh PIDFORparentafter2.sh:1339 nowais2.sh ais6 PIDforparentbefore2.sh:1339 usingsourcing PIDFOR2.SH:1339 2.shgetafrommain.shis6 now2.shais2.sh PIDFORparentafter2.sh:1339 nowais2.sh ais7 PIDforparentbefore2.sh:1339 usingsourcing PIDFOR2.SH:1339 2.shgetafrommain.shis7 now2.shais2.sh PIDFORparentafter2.sh:1339 nowais2.sh ais8 PIDforparentbefore2.sh:1339 usingsourcing PIDFOR2.SH:1339 2.shgetafrommain.shis8 now2.shais2.sh PIDFORparentafter2.sh:1339 nowais2.sh ais9 PIDforparentbefore2.sh:1339 usingsourcing PIDFOR2.SH:1339 2.shgetafrommain.shis9 now2.shais2.sh PIDFORparentafter2.sh:1339 nowais2.sh ais10 PIDforparentbefore2.sh:1339 usingsourcing PIDFOR2.SH:1339 2.shgetafrommain.shis10 now2.shais2.sh PIDFORparentafter2.sh:1339 nowais2.sh
以上这篇shellfor循环、循环变量值付给其他shell脚本的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。