天天看點

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.cn/163974.html原文連結:https://javaforall.cn