shell文件包含
shell和其他语言一样,shell也可以包含外部脚本,这样可以很方便的封装一些公用的代码作为一个独立的文件。
格式:
. filename #注意(.)与文件名中间有以空格。
或者
source filename
实例
创建两个shell脚本文件
第一个文件test1.sh
#test1.sh
#!/bin/bash
url="http://www.google.com"
#test2.sh
#!./bin/bash
#使用 . 号来引用test1.sh文件
. ./test1.sh
#或使用source来包含文件代码
#! source ./test1.sh
echo "google的网址: $url"
运行文件:
$ chmod +x test2.sh
$ ./test2.sh
google的网址:$url

本文介绍如何在Shell脚本中包含其他脚本文件,通过使用.或source命令,实现代码复用和模块化。示例展示了如何从一个脚本调用另一个脚本中的变量。
624

被折叠的 条评论
为什么被折叠?



