婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > shell for循環、循環變量值付給其他shell腳本的方法

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

熱門標簽:貴陽400電話到哪里去辦理 申請400電話有什么用 天津智能外呼系統排名 4層電梯外呼控制系統設計 400電話申請找 電銷機器人加盟多少錢 汨羅代理外呼系統 網絡電話外呼系統撥號軟件 宿松高德地圖標注

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

您可能感興趣的文章:
  • 詳解shell 變量的高級用法示例
  • 處理Shell腳本中帶有空格的變量(bash腳本)
  • shell判斷一個變量是否為空方法總結
  • linux中shell的變量的數值計算
  • Shell編程之特殊變量和擴展變量詳解
  • Shell編程之變量數值計算方法示例
  • 詳解Shell編程之變量數值計算(二)
  • 詳解Shell編程之變量數值計算(一)
  • 淺談linux中shell變量$#,$@,$0,$1,$2的含義解釋
  • 判斷Linux Shell環境變量是否存在
  • Linux bash Shell中的變量類型詳解
  • Linux Shell腳本系列教程(四):使用函數添加環境變量
  • Linux Shell腳本系列教程(三):變量和環境變量
  • 詳解shell 變量

標簽:臨沂 昌都 贛州 烏蘭察布 連云港 撫州 廣東 海北

巨人網絡通訊聲明:本文標題《shell for循環、循環變量值付給其他shell腳本的方法》,本文關鍵詞  shell,for,循環,變,量值,付給,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《shell for循環、循環變量值付給其他shell腳本的方法》相關的同類信息!
  • 本頁收集關于shell for循環、循環變量值付給其他shell腳本的方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 海阳市| 澳门| 克东县| 皮山县| 连平县| 陈巴尔虎旗| 剑阁县| 恩平市| 漳平市| 缙云县| 车致| 庆元县| 弋阳县| 大港区| 鹤庆县| 神木县| 通化县| 榆中县| 临潭县| 平安县| 乌拉特前旗| 昭觉县| 临朐县| 涿州市| 得荣县| 衢州市| 凤城市| 土默特右旗| 涪陵区| 乌拉特前旗| 贵南县| 土默特左旗| 罗平县| 八宿县| 克拉玛依市| 清苑县| 莆田市| 隆安县| 东乌珠穆沁旗| 罗江县| 南岸区|