我可以从另一个 shell 脚本调用一个 shell 脚本的函数吗? [英] Can I call a function of a shell script from another shell script?

查看:31
本文介绍了我可以从另一个 shell 脚本调用一个 shell 脚本的函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个 shell 脚本.

I have 2 shell scripts.

第二个shell脚本包含以下函数second.sh

The second shell script contains following functions second.sh

func1 
func2

first.sh 将调用带有一些参数的第二个 shell 脚本和将使用特定于该函数的一些其他参数调用 func1 和 func2.

The first.sh will call the second shell script with some parameters and will call func1 and func2 with some other parameters specific to that function.

这是我在谈论的例子

第二个.sh

val1=`echo $1`
val2=`echo $2`

function func1 {

fun=`echo $1`
book=`echo $2`

}

function func2 {

fun2=`echo $1`
book2=`echo $2`


}

第一个.sh

second.sh cricket football

func1 love horror
func2 ball mystery

我怎样才能实现它?

推荐答案

像这样重构你的 second.sh 脚本:

Refactor your second.sh script like this:

func1 {
   fun="$1"
   book="$2"
   printf "func=%s,book=%s\n" "$fun" "$book"
}

func2 {
   fun2="$1"
   book2="$2"
   printf "func2=%s,book2=%s\n" "$fun2" "$book2"
}

然后像这样从脚本 first.sh 调用这些函数:

And then call these functions from script first.sh like this:

source ./second.sh
func1 love horror
func2 ball mystery

输出:

func=love,book=horror
func2=ball,book2=mystery

这篇关于我可以从另一个 shell 脚本调用一个 shell 脚本的函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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