shell 中調用其他的腳本


方法有三種:

1 使用source

2 使用 .

3 使用sh

簡單實驗:

first.sh

#!/bin/bash
echo 'your are in first file'

 

second.sh

#!/bin/bash
echo 'your are in second file'

source first.sh // . first.sh // sh first.sh

 

執行結果:

your are in second file
your are in first file

 

現在討論關於參數傳遞:

first.sh

#!/bin/bash
echo 'your are in first file'
echo "${0} ${1}"

second.sh

#!/bin/bash
echo 'your are in second file'
echo "${0} ${1}"

. first.sh ${2} //source first.sh

執行:./second.sh abc 123

your are in second file
./second.sh abc
your are in first file
./second.sh 123

 

改變second.sh

second.sh

#!/bin/bash
echo 'your are in second file'
echo "${0} ${1}"

sh first.sh ${2} 

執行:

./second.sh abc 123

結果:

your are in second file
./second.sh abc
your are in first file
first.sh 123

所以在調用的那個腳本需要傳遞參數的時候還是要注意選對方法的

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM