本篇文章扣丁学堂Linux培训小编就为读者们分享一下shell for循环、循环变量值付给其余shell脚本的方法,对Linux开发技术感兴趣的小伙伴就随小编来了解一下吧,但愿对你们有所帮助。shell
本文主要将在shell中如何编写for循环,并将循环变量做为下个shell脚本的参数。微信
shell for 循环:函数
for ((i=1; i<=100; i ++))学习
docode
echo $i 视频
done继承
for i in {1..100}教程
do进程
echo $iip
done
for i in seq 1 100
do
echo $i
done
将循环变量赋值到下一个脚本:
在运行shell脚本时候,有三种方式来调用外部的脚本,exec(exec script.sh)、source(source script.sh)、fork(./script.sh)
一、exec(exec /home/script.sh):
使用exec来调用脚本,被执行的脚本会继承当前shell的环境变量。但事实上exec产生了新的进程,他会把主shell的进程资源占用并替换脚本内容,继承了原主shell的PID号,即原主shell剩下的内容不会执行。
二、source(source /home/script.sh)
使用source或者“.”来调用外部脚本,不会产生新的进程,继承当前shell环境变量,并且被调用的脚本运行结束后,它拥有的环境变量和声明变量会被当前shell保留,相似将调用脚本的内容复制过来直接执行。执行完毕后原主shell继续运行。
三、fork(/home/script.sh)
直接运行脚本,会以当前shell为父进程,产生新的进程,而且继承主脚本的环境变量和声明变量。执行完毕后,主脚本不会保留其环境变量和声明变量。
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"
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变量赋值并执行。
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
想要了解更多关于Linux开发方面内容的小伙伴,请关注扣丁学堂Linux培训官网、微信等平台,扣丁学堂IT职业在线学习教育有专业的Linux讲师为您指导,此外扣丁学堂老师精心推出的Linux视频教程定能让你快速掌握Linux从入门到精通开发实战技能。