1. 程式人生 > >shell--11、Shell 文件包含

shell--11、Shell 文件包含

shell 文件包含

和其他語言一樣,Shell 也可以包含外部腳本。這樣可以很方便的封裝一些公用的代碼作為一個獨立的文件。

Shell 文件包含的語法格式如下:

. filename   # 註意點號(.)和文件名中間有一空格
或
source filename

實例

創建兩個 shell 腳本文件。

test1.sh 代碼如下:

#!/bin/bash
url="http://www.baidu.com"

test2.sh 代碼如下:

#!/bin/bash
#使用 . 號來引用test1.sh 文件
. ./test1.sh
# 或者使用以下包含文件代碼
# source ./test1.sh
echo "網址:$url"

接下來,我們為 test2.sh 添加可執行權限並執行:

$ chmod +x test2.sh 
$ ./test2.sh 
網址:http://www.baidu.com

註:被包含的文件 test1.sh 不需要可執行權限。


本文出自 “風之痕_雪虎” 博客,請務必保留此出處http://snowtiger.blog.51cto.com/12931578/1942042

shell--11、Shell 文件包含