shell for循环、循环变量值付给其他shell脚本

shell for循环、循环变量值付给其他shell脚本本文主要将在shell中如何编写for循环,并将循环变量作为下个shell脚本的参数。

大家好,又见面了,我是你们的朋友全栈君。

本文主要将在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

更多内容请查看 Julywhj的博客期待您的光顾。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163974.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Quartus II 13.0安装和激活成功教程教程

    Quartus II 13.0安装和激活成功教程教程QuartusII软件是学习FPGA非常重要的软件,下面来介绍一下13.0版本的安装及激活成功教程教程:1、安装包介绍大家下载的完整版的QuartusII13.0软件应包含以下三个部分:请大家选择第三个“Quartus-13.0.0.156-windows.tar”压缩文件,即为我们的QuartusII13.0的主安装包,这是一个压缩文件,安装前需要解压。该包包含了开发FP…

    2022年10月16日
  • python计算基尼系数_PHP算法

    python计算基尼系数_PHP算法什么是基尼系数基尼系数是指国际上通用的、用以衡量一个国家或地区居民收入差距的常用指标。是20世纪初意大利经济学家基尼,于1922年提出的定量测定收入分配差异程度的指标。它是根据洛伦兹曲线找出了判断分配平等程度的指标(如下图)。设实际收入分配曲线和收入分配绝对平等曲线之间的面积为A,实际收入分配曲线右下方的面积为B。并以A除以A+B的商表示不平等程度。这个数值被称为基尼系数或称洛伦茨系…

    2022年8月31日
  • Android—Gradle教程(一)「建议收藏」

    Android—Gradle教程(一)「建议收藏」前言在前几篇中,讲解了Groovy的基础语法,学习新语法过程总是枯燥的,但为了更好的掌握Gradle,那就必须经过该过程。当然从这一篇将会从零基础开始,开展对Gradle的讲解。1.掌握Gradle基础1.1环境配置去官网下载gradle或者从本地用户文件夹下的.gradle/wrapper/dists找到本地缓存的gradle开发工具包(注意带bin文件夹的这个grade-x.x)系统属性配置:添加GRADLE_HOME:C:\Users\sheji.gradle\wrapper\

    2022年6月28日
  • 自己动手实现arping

    自己动手实现arping只要接触过网络的人,相信对ping命令并不陌生。该命令可以用来检测本机到目标机的网络是否连通,是一种很常见的网络监测手段。对网络熟悉一点的人可能还知道ping命令的工作原理。让我们看看ping命令的介绍:DESCRIPTIONpingusestheICMPprotocol’smandatoryECHO_REQUESTdatagramtoelicitanICMPECHO_RESPONSEfromahostorgateway….

    2022年6月7日
  • Python 代码风格

    Python 代码风格

    2021年11月26日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号