shell如何实现for循环、循环变量值付给其他shell脚本
小编给大家分享一下shell如何实现for循环、循环变量值付给其他shell脚本,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
本文主要将在shell中如何编写for循环,并将循环变量作为下个shell脚本的参数。
shell for 循环:
#!第一种写法 类似C、Javafor ((i=1; i<=100; i ++))do echo $i done#!第二种写法 in应用for i in {1..100} do echo $i done #!第三种写法 seq 使用for i in `seq 1 100` do echo $i done
将循环变量赋值到下一个脚本:
在运行shell脚本时候,有三种方式来调用外部的脚本,exec(exec script.sh)、source(source script.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/sha=mainecho "a is $a"echo "PID for parent before 2.sh:$$"case $1 in exec) echo "using exec" exec ./2.sh ;; *) echo "using sourcing" source ./2.sh ;;esacecho "PID FOR parent after 2.sh :$$"echo "now m"
#!2.sh#!/bin/shecho "PID FOR 2.SH:$$"echo "2.sh get a from main.sh is $a"a=2.shexport ab=3.shecho "now 2.sh a is $a"
执行结果:
a is mainPID for parent before 2.sh:1162using sourcingPID FOR 2.SH:11622.sh get a from main.sh is main`这里写代码片`now 2.sh a is 2.shPID FOR parent after 2.sh :1162now m
通过for循环,循环变量作为2.sh变量赋值并执行。
#!main主函数#!/bin/sha=0for ((i=1; i<=10; i ++))do a=$i echo "a is $a" echo "PID for parent before 2.sh:$$" echo "using sourcing" source ./2.sh echo "PID FOR parent after 2.sh :$$" echo "now a is $a" done
输出结果:
a is 1PID for parent before 2.sh:1339using sourcingPID FOR 2.SH:13392.sh get a from main.sh is 1now 2.sh a is 2.shPID FOR parent after 2.sh :1339now a is 2.sha is 2PID for parent before 2.sh:1339using sourcingPID FOR 2.SH:13392.sh get a from main.sh is 2now 2.sh a is 2.shPID FOR parent after 2.sh :1339now a is 2.sha is 3PID for parent before 2.sh:1339using sourcingPID FOR 2.SH:13392.sh get a from main.sh is 3now 2.sh a is 2.shPID FOR parent after 2.sh :1339now a is 2.sha is 4PID for parent before 2.sh:1339using sourcingPID FOR 2.SH:13392.sh get a from main.sh is 4now 2.sh a is 2.shPID FOR parent after 2.sh :1339now a is 2.sha is 5PID for parent before 2.sh:1339using sourcingPID FOR 2.SH:13392.sh get a from main.sh is 5now 2.sh a is 2.shPID FOR parent after 2.sh :1339now a is 2.sha is 6PID for parent before 2.sh:1339using sourcingPID FOR 2.SH:13392.sh get a from main.sh is 6now 2.sh a is 2.shPID FOR parent after 2.sh :1339now a is 2.sha is 7PID for parent before 2.sh:1339using sourcingPID FOR 2.SH:13392.sh get a from main.sh is 7now 2.sh a is 2.shPID FOR parent after 2.sh :1339now a is 2.sha is 8PID for parent before 2.sh:1339using sourcingPID FOR 2.SH:13392.sh get a from main.sh is 8now 2.sh a is 2.shPID FOR parent after 2.sh :1339now a is 2.sha is 9PID for parent before 2.sh:1339using sourcingPID FOR 2.SH:13392.sh get a from main.sh is 9now 2.sh a is 2.shPID FOR parent after 2.sh :1339now a is 2.sha is 10PID for parent before 2.sh:1339using sourcingPID FOR 2.SH:13392.sh get a from main.sh is 10now 2.sh a is 2.shPID FOR parent after 2.sh :1339now a is 2.sh
看完了这篇文章,相信你对“shell如何实现for循环、循环变量值付给其他shell脚本”有了一定的了解,如果想了解更多相关知识,欢迎关注编程网行业资讯频道,感谢各位的阅读!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341