首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将bash函数作为命令执行?

如何将bash函数作为命令执行?
EN

Stack Overflow用户
提问于 2021-03-13 19:36:47
回答 1查看 33关注 0票数 0

我有一个CLI工具,需要以以下方式调用:

foobar run -- [command]

例如

foobar run -- script.sh

问题是,我想在同一个shell脚本中安装CLI工具。我如何使用下面的结构做到这一点呢?

代码语言:javascript
复制
# My script I want to run with the CLI tool
perform_actions () {
   # ...
}

# Installing CLI tool from some URL
# ...

# Execute my script with the CLI tool
foobar run -- perform_actions  # This doesn't work because perform_actions is a function
EN

回答 1

Stack Overflow用户

发布于 2021-03-13 20:13:45

我会这样组织脚本:

代码语言:javascript
复制
#!/usr/bin/env bash
  
SCRIPT_SH=$(cd $(dirname $BASH_SOURCE);pwd)/$(basename $BASH_SOURCE)

# My script I want to run with the CLI tool
perform_actions () {
    # ...
}

foobar_run(){
    foobar run -- $SCRIPT_SH perform_actions
}

# Installing CLI tool from some URL
# ...

# Execute my script with the CLI tool
# foobar run -- perform_actions  # This doesn't work because perform_actions is a function

"$@"

要运行该脚本:

代码语言:javascript
复制
./script.sh foobar_run
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66613006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档