欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學(xué)堂Linux培訓(xùn)簡述shell for循環(huán)、循環(huán)變量值付給其他shell腳本的方法

2019-05-09 11:06:07 3696瀏覽

本篇文章扣丁學(xué)堂Linux培訓(xùn)小編就為讀者們分享一下shell for循環(huán)、循環(huán)變量值付給其他shell腳本的方法,對Linux開發(fā)技術(shù)感興趣的小伙伴就隨小編來了解一下吧,希望對大家有所幫助。


扣丁學(xué)堂Linux培訓(xùn)簡述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)變量賦值到下一個腳本:


在運行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)境變量。但事實上exec產(chǎn)生了新的進程,他會把主shell的進程資源占用并替換腳本內(nèi)容,繼承了原主shell的PID號,即原主shell剩下的內(nèi)容不會執(zhí)行。



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


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



3、fork(/home/script.sh)


直接運行腳本,會以當(dāng)前shell為父進程,產(chǎ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

想要了解更多關(guān)于Linux開發(fā)方面內(nèi)容的小伙伴,請關(guān)注扣丁學(xué)堂Linux培訓(xùn)官網(wǎng)、微信等平臺,扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育有專業(yè)的Linux講師為您指導(dǎo),此外扣丁學(xué)堂老師精心推出的Linux視頻教程定能讓你快速掌握Linux從入門到精通開發(fā)實戰(zhàn)技能。扣丁學(xué)堂Linux技術(shù)交流群:422345477。


                          【掃碼進入HTML5VIP免費公開課】  


     【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】        【掃碼進入HTML5前端開發(fā)VIP免費公開課】



查看更多關(guān)于“Linux培訓(xùn)資訊”的相關(guān)文章>>


標簽: Linux培訓(xùn) Linux視頻教程 紅帽Linux視頻 Linux學(xué)習(xí)視頻 Linux入門視頻

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關(guān)村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部