国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Linux Shell > 正文

shell for循環、循環變量值付給其他shell腳本的方法

2020-07-27 18:47:31
字體:
來源:轉載
供稿:網友

本文主要將在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腳本的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 城市| 合作市| 图们市| 台东市| 通榆县| 龙里县| 定西市| 咸宁市| 蒙自县| 贵定县| 牙克石市| 平果县| 台南县| 宁安市| 九江县| 丰城市| 巴彦县| 临西县| 元阳县| 茌平县| 苗栗市| 文昌市| 怀仁县| 罗平县| 柳林县| 微山县| 井冈山市| 大化| 长兴县| 南平市| 苏州市| 来安县| 定陶县| 洪洞县| 乌什县| 蚌埠市| 惠东县| 南丹县| 米易县| 云南省| 桐城市|