击:如何_best_包括其他脚本? [英] Bash: How _best_ to include other scripts?

查看:131
本文介绍了击:如何_best_包括其他脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您通常会包括脚本的方式是源

The way you would normally include a script is with "source"

例如:

main.sh:

#!/bin/bash

source incl.sh

echo "The main script"

incl.sh:

incl.sh:

echo "The included script"

执行./main.sh的输出是:

The output of executing "./main.sh" is:

The included script
The main script

...现在,如果你尝试从另一个位置执行该shell脚本,它无法找到包括,除非它是在你的路径。

... Now, if you attempt to execute that shell script from another location, it can't find the include unless it's in your path.

什么是确保你的脚本可以找到包括脚本,特别是如果例如,脚本需要可移植的好办法?

What's a good way to ensure that your script can find the include script, especially if for instance, the script needs to be portable?

推荐答案

我倾向于让我的脚本都是相对于彼此。
这样,我可以使用目录名称:

I tend to make my scripts all be relative to one another. That way I can use dirname:

#!/bin/sh

my_dir="$(dirname "$0")"

"$my_dir/other_script.sh"

这篇关于击:如何_best_包括其他脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆