本篇文章扣丁學堂Linux培訓小編就爲讀者們分享一下shell for循環、循環變量值付給其餘shell腳本的方法,對Linux開發技術感興趣的小夥伴就隨小編來了解一下吧,但願對你們有所幫助。shell
本文主要將在shell中如何編寫for循環,並將循環變量做爲下個shell腳本的參數。微信
shell for 循環:函數
for ((i=1; i<=100; i ++))學習
docode
echo $i 視頻
done繼承
for i in {1..100}教程
do進程
echo $iip
done
for i in seq 1 100
do
echo $i
done
將循環變量賦值到下一個腳本:
在運行shell腳本時候,有三種方式來調用外部的腳本,exec(exec script.sh)、source(source script.sh)、fork(./script.sh)
一、exec(exec /home/script.sh):
使用exec來調用腳本,被執行的腳本會繼承當前shell的環境變量。但事實上exec產生了新的進程,他會把主shell的進程資源佔用並替換腳本內容,繼承了原主shell的PID號,即原主shell剩下的內容不會執行。
二、source(source /home/script.sh)
使用source或者「.」來調用外部腳本,不會產生新的進程,繼承當前shell環境變量,並且被調用的腳本運行結束後,它擁有的環境變量和聲明變量會被當前shell保留,相似將調用腳本的內容複製過來直接執行。執行完畢後原主shell繼續運行。
三、fork(/home/script.sh)
直接運行腳本,會以當前shell爲父進程,產生新的進程,而且繼承主腳本的環境變量和聲明變量。執行完畢後,主腳本不會保留其環境變量和聲明變量。
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"
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變量賦值並執行。
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
想要了解更多關於Linux開發方面內容的小夥伴,請關注扣丁學堂Linux培訓官網、微信等平臺,扣丁學堂IT職業在線學習教育有專業的Linux講師爲您指導,此外扣丁學堂老師精心推出的Linux視頻教程定能讓你快速掌握Linux從入門到精通開發實戰技能。