Shell 文件包含

Linux 教程 · 2019-02-13 15:11:44

和其他语言一样,Shell 也可以包含外部脚本。这样可以很方便的封装一些公用的代码作为一个独立的文件。

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

. filename   # 注意点号(.)和文件名中间有一空格

或

source filename

实例

创建两个 shell 脚本文件。

test1.sh 代码如下:

#!/bin/bash
# author:码农教程
# url:www.codercto.com

url="http://www.codercto.com"

test2.sh 代码如下:

#!/bin/bash
# author:码农教程
# url:www.codercto.com

#使用 . 号来引用test1.sh 文件
. ./test1.sh

# 或者使用以下包含文件代码
# source ./test1.sh

echo "码农教程官网地址:$url"

接下来,我们为 test2.sh 添加可执行权限并执行:

$ chmod +x test2.sh 
$ ./test2.sh 
码农教程官网地址:http://www.codercto.com

注:被包含的文件 test1.sh 不需要可执行权限。

点击查看所有 Linux 教程 文章: https://codercto.com/courses/l/14.html

查看所有标签

Producing Masculinity

Producing Masculinity

Michele White / Routledge / 2019-3 / $39.95

Thoughtful, witty, and illuminating, in this book Michele White explores the ways normative masculinity is associated with computers and the Internet and is a commonly enacted online gender practice. ......一起来看看 《Producing Masculinity》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器