溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

shell如何實(shí)現(xiàn)for循環(huán)、循環(huán)變量值付給其他shell腳本

發(fā)布時(shí)間:2021-08-04 13:53:03 來源:億速云 閱讀:148 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下shell如何實(shí)現(xiàn)for循環(huán)、循環(huán)變量值付給其他shell腳本,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

本文主要將在shell中如何編寫for循環(huán),并將循環(huán)變量作為下個(gè)shell腳本的參數(shù)。

shell for 循環(huán):

#!第一種寫法 類似C、Java
for ((i=1; i<=100; i ++))
do
  echo $i  
done
#!第二種寫法 in應(yīng)用
for i in {1..100} 
do 
  echo $i 
done 
#!第三種寫法 seq 使用
for i in `seq 1 100` 
do 
  echo $i 
done

將循環(huán)變量賦值到下一個(gè)腳本:

在運(yùn)行shell腳本時(shí)候,有三種方式來調(diào)用外部的腳本,exec(exec script.sh)、source(source script.sh)、fork(./script.sh)

1、exec(exec /home/script.sh):

使用exec來調(diào)用腳本,被執(zhí)行的腳本會(huì)繼承當(dāng)前shell的環(huán)境變量。但事實(shí)上exec產(chǎn)生了新的進(jìn)程,他會(huì)把主shell的進(jìn)程資源占用并替換腳本內(nèi)容,繼承了原主shell的PID號,即原主shell剩下的內(nèi)容不會(huì)執(zhí)行。

2、source(source /home/script.sh)

使用source或者“.”來調(diào)用外部腳本,不會(huì)產(chǎn)生新的進(jìn)程,繼承當(dāng)前shell環(huán)境變量,而且被調(diào)用的腳本運(yùn)行結(jié)束后,它擁有的環(huán)境變量和聲明變量會(huì)被當(dāng)前shell保留,類似將調(diào)用腳本的內(nèi)容復(fù)制過來直接執(zhí)行。執(zhí)行完畢后原主shell繼續(xù)運(yùn)行。

3、fork(/home/script.sh)

直接運(yùn)行腳本,會(huì)以當(dāng)前shell為父進(jìn)程,產(chǎn)生新的進(jìn)程,并且繼承主腳本的環(huán)境變量和聲明變量。執(zhí)行完畢后,主腳本不會(huì)保留其環(huán)境變量和聲明變量。

#!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"

執(zhí)行結(jié)果:

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循環(huán),循環(huán)變量作為2.sh變量賦值并執(zhí)行。

#!main主函數(shù)
#!/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

輸出結(jié)果:

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如何實(shí)現(xiàn)for循環(huán)、循環(huán)變量值付給其他shell腳本”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI