本文主要将在shell中如何编写for循环,并将循环变量作为下个shell脚本的参数。
shell for 循环:
#!第一种写法 类似C、Java for ((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/sh a=main echo "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 ;; esac echo "PID FOR parent after 2.sh :$$" echo "now m"
#!2.sh #!/bin/sh echo "PID FOR 2.SH:$$" echo "2.sh get a from main.sh is $a" a=2.sh export a b=3.sh echo "now 2.sh a is $a"
执行结果:
a is main PID for parent before 2.sh:1162 using sourcing PID FOR 2.SH:1162 2.sh get a from main.sh is main`这里写代码片` now 2.sh a is 2.sh PID FOR parent after 2.sh :1162 now m
通过for循环,循环变量作为2.sh变量赋值并执行。
#!main主函数 #!/bin/sh a=0 for ((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 1 PID for parent before 2.sh:1339 using sourcing PID FOR 2.SH:1339 2.sh get a from main.sh is 1 now 2.sh a is 2.sh PID FOR parent after 2.sh :1339 now a is 2.sh a is 2 PID for parent before 2.sh:1339 using sourcing PID FOR 2.SH:1339 2.sh get a from main.sh is 2 now 2.sh a is 2.sh PID FOR parent after 2.sh :1339 now a is 2.sh a is 3 PID for parent before 2.sh:1339 using sourcing PID FOR 2.SH:1339 2.sh get a from main.sh is 3 now 2.sh a is 2.sh PID FOR parent after 2.sh :1339 now a is 2.sh a is 4 PID for parent before 2.sh:1339 using sourcing PID FOR 2.SH:1339 2.sh get a from main.sh is 4 now 2.sh a is 2.sh PID FOR parent after 2.sh :1339 now a is 2.sh a is 5 PID for parent before 2.sh:1339 using sourcing PID FOR 2.SH:1339 2.sh get a from main.sh is 5 now 2.sh a is 2.sh PID FOR parent after 2.sh :1339 now a is 2.sh a is 6 PID for parent before 2.sh:1339 using sourcing PID FOR 2.SH:1339 2.sh get a from main.sh is 6 now 2.sh a is 2.sh PID FOR parent after 2.sh :1339 now a is 2.sh a is 7 PID for parent before 2.sh:1339 using sourcing PID FOR 2.SH:1339 2.sh get a from main.sh is 7 now 2.sh a is 2.sh PID FOR parent after 2.sh :1339 now a is 2.sh a is 8 PID for parent before 2.sh:1339 using sourcing PID FOR 2.SH:1339 2.sh get a from main.sh is 8 now 2.sh a is 2.sh PID FOR parent after 2.sh :1339 now a is 2.sh a is 9 PID for parent before 2.sh:1339 using sourcing PID FOR 2.SH:1339 2.sh get a from main.sh is 9 now 2.sh a is 2.sh PID FOR parent after 2.sh :1339 now a is 2.sh a is 10 PID for parent before 2.sh:1339 using sourcing PID FOR 2.SH:1339 2.sh get a from main.sh is 10 now 2.sh a is 2.sh PID FOR parent after 2.sh :1339 now a is 2.sh
以上这篇shell for循环、循环变量值付给其他shell脚本的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 马小倩《落花A2HD3-HQCD》头版限量[低速原抓WAV+CUE]
- 343前动画师怒喷343糟糕管理层:纯纯的毒瘤
- 《生化2RE》12.31苹果Mac平台发售:现已登陆商店
- 《使命召唤21:黑色行动6》实机发售预告!体验史诗电影单人战役
- 《张雨生 不朽的歌声 黑胶2CD》[WAV/分轨][860MB]
- 《赵传 你过得还好吗 2017》[FLAC/分轨][350MB]
- 《流行新歌告诉你 36首经典金曲 2CD》[WAV+CUE][1GB]
- 王宏伟.2012-用我的心握你的手【太平洋影音】【WAV+CUE】
- 谢安琪.2006-K.SUS.2(EP)(2015金碟复刻版)【BanBanMusic】【WAV+CUE】
- 谢安琪.2005-KAY.ONE(2015金碟复刻版)【BanBanMusic】【WAV+CUE】
- 《勇敢小骑士》评测:是永无乡,亦是梦幻岛
- 《哈利波特:魁地奇锦标赛》:我们魔法学院自己的足球赛
- 《冰汽时代2》评测:看来这一切都是值得的
- 《蛋仔派对》翻糖壳壳皮肤图鉴
- 《蛋仔派对》斑马乐乐皮肤图鉴