shell for循環(huán)、循環(huán)變量值付給其他shell腳本的方法
本文主要將在shell中如何編寫for循環(huán),并將循環(huán)變量作為下個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)變量賦值到下一個腳本:
在運(yùn)行shell腳本時候,有三種方式來調(diào)用外部的腳本,exec(exec script.sh)、source(source script.sh)、fork(./script.sh)
1、exec(exec /home/script.sh):
使用exec來調(diào)用腳本,被執(zhí)行的腳本會繼承當(dāng)前shell的環(huán)境變量。但事實(shí)上exec產(chǎn)生了新的進(jìn)程,他會把主shell的進(jìn)程資源占用并替換腳本內(nèi)容,繼承了原主shell的PID號,即原主shell剩下的內(nèi)容不會執(zhí)行。
2、source(source /home/script.sh)
使用source或者“.”來調(diào)用外部腳本,不會產(chǎn)生新的進(jìn)程,繼承當(dāng)前shell環(huán)境變量,而且被調(diào)用的腳本運(yùn)行結(jié)束后,它擁有的環(huán)境變量和聲明變量會被當(dāng)前shell保留,類似將調(diào)用腳本的內(nèi)容復(fù)制過來直接執(zhí)行。執(zhí)行完畢后原主shell繼續(xù)運(yùn)行。
3、fork(/home/script.sh)
直接運(yùn)行腳本,會以當(dāng)前shell為父進(jìn)程,產(chǎn)生新的進(jìn)程,并且繼承主腳本的環(huán)境變量和聲明變量。執(zhí)行完畢后,主腳本不會保留其環(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 for循環(huán)、循環(huán)變量值付給其他shell腳本的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 詳解shell 變量的高級用法示例
- 處理Shell腳本中帶有空格的變量(bash腳本)
- shell判斷一個變量是否為空方法總結(jié)
- linux中shell的變量的數(shù)值計算
- Shell編程之特殊變量和擴(kuò)展變量詳解
- Shell編程之變量數(shù)值計算方法示例
- 詳解Shell編程之變量數(shù)值計算(二)
- 詳解Shell編程之變量數(shù)值計算(一)
- 淺談linux中shell變量$#,$@,$0,$1,$2的含義解釋
- 判斷Linux Shell環(huán)境變量是否存在
- Linux bash Shell中的變量類型詳解
- Linux Shell腳本系列教程(四):使用函數(shù)添加環(huán)境變量
- Linux Shell腳本系列教程(三):變量和環(huán)境變量
- 詳解shell 變量
相關(guān)文章
Shell $[]對整數(shù)進(jìn)行數(shù)學(xué)運(yùn)算實(shí)現(xiàn)
本文主要介紹了Shell $[]對整數(shù)進(jìn)行數(shù)學(xué)運(yùn)算實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01Linux命令創(chuàng)建日期文件夾或者文件的實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了Linux命令創(chuàng)建日期文件夾或者文件的相關(guān)知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10Shell腳本實(shí)現(xiàn)的基于SVN的代碼提交量統(tǒng)計工具
這篇文章主要介紹了Shell腳本實(shí)現(xiàn)的基于SVN的代碼提交量統(tǒng)計工具,本文直接給出實(shí)現(xiàn)腳本代碼,需要的朋友可以參考下2015-06-06阿里云主機(jī)一鍵安裝lamp、lnmp環(huán)境的shell腳本分享
這篇文章主要介紹了阿里云主機(jī)一鍵安裝lamp、lnmp環(huán)境的shell腳本分享,需要的朋友可以參考下2014-07-07Linux?Shell任務(wù)控制的實(shí)現(xiàn)示例
本文主要介紹了Linux?Shell任務(wù)控制的實(shí)現(xiàn)示例,包括向腳本發(fā)送信號、修改腳本的優(yōu)先級以及在腳本運(yùn)行時從暫停切換到運(yùn)行模式,感興趣的可以了解一下2024-01-01編寫shell腳本將VPS上的數(shù)據(jù)備份到Dropbox網(wǎng)盤的方法
這篇文章主要介紹了編寫shell腳本將VPS上的數(shù)據(jù)備份到Dropbox網(wǎng)盤的方法,注意Dropbox在國內(nèi)訪問的網(wǎng)絡(luò)相關(guān)問題,需要的朋友可以參考下2015-07-07檢查linux網(wǎng)絡(luò)狀態(tài)的兩個腳本
檢查linux網(wǎng)絡(luò)狀態(tài)的兩個腳本,包括通過定時收發(fā)email檢測網(wǎng)絡(luò)連通性、定時檢測網(wǎng)絡(luò)狀態(tài)通過email發(fā)送,有需要的朋友可以參考下2013-02-02