shell 教程五:文件包含

unadlib

关注

阅读 73

2022-08-30


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

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

linux@ubuntu:~/test_shell$ cat test1.sh
#!/bin/bash
name="Liu Jing"


实例

创建两个 shell 脚本文件。

test1.sh 代码如下:


linux@ubuntu:~/test_shell$ cat test2.sh
#!/bin/bash
#. ./test1.sh
source ./test1.sh
echo "MyName:${name}"


test2.sh 代码如下:

linux@ubuntu:~/test_shell$ cat test2.sh
#!/bin/bash
#. ./test1.sh
source ./test1.sh
echo "MyName:${name}"


输出:


linux@ubuntu:~/test_shell$ chmod +x test2.sh
linux@ubuntu:~/test_shell$ ./test2.sh
MyName:Liu Jing

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



精彩评论(0)

0 0 举报