shell for循环、循环变量值付给其他shell脚本
来源:互联网 发布:mac删除windows系统 编辑:程序博客网 时间:2024/05/21 21:45
本文主要将在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
阅读全文
0 0
- shell for循环、循环变量值付给其他shell脚本
- shell脚本----for循环
- shell 脚本之 for 循环
- shell脚本for循环知识
- 【Shell脚本学习18】Shell for循环
- shell脚本怎样表示一个for循环
- AIX写shell脚本的for循环
- linux shell 脚本 之for 循环
- shell脚本----for循环报错
- 脚本乐园 shell编程for in 循环
- 脚本乐园 shell编程for in 循环
- shell 脚本中for循环遍历数组
- shell脚本for循环的使用
- shell脚本 for循环迭代文件
- shell 脚本之for while until循环
- Shell脚本基础19-for循环
- shell脚本编程举例--for、while循环
- shell循环1.for循环
- 刷新iframe
- Eclipse项目中内容不保存,项目名称上有一个红叉
- 激光雷达学习笔记(一)数据采集
- Python的Object基类__repr__方法
- android自定义控件--学习
- shell for循环、循环变量值付给其他shell脚本
- Java API一些注意的零碎不定期整理
- 允许远程用户登录访问mysql的方法
- 二叉树遍历
- 安卓开发xm解析
- 用python读取和显示图像
- java-第五章 初始化与清理
- dp——洛谷 P1412 经营与开发
- 小程序开发之旅(持续更新中..)