gpt4 book ai didi

bash - 从另一个 shell 脚本调用函数

转载 作者:行者123 更新时间:2023-12-04 16:16:49 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Shell: How to call one shell script from another shell script?

(18 个回答)



Importing shell script function

(3 个回答)


6年前关闭。




我已经写了一个脚本,现在我喜欢通过将我的主脚本的一部分移动到其他文件中来使其更具可读性,但不幸的是我不能。

假设现在我在文件 utils.sh 中有以下代码:

#!/bin/bash

sayHello ()
{
echo "Hello World"
}

他们从我的主脚本中尝试以下操作,但不起作用:
#!/bin/bash

./utils.sh

sayHello

所以,问题是,如何从 utils.sh 中调用函数?

最佳答案

您必须使用 . 来获取它或 source :

~$ cat >main.sh
#!/bin/bash
. ./utils.sh #or source ./utils.sh
sayHello

进而
~$ ./main.sh
Hello World

关于bash - 从另一个 shell 脚本调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27704736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com