【问题标题】:How to execute two function one by one in the list of functions in the script.sh file如何在script.sh文件的函数列表中一个一个地执行两个函数
【发布时间】:2021-11-04 00:29:30
【问题描述】:

我想在一个 bash 命令中一个一个地调用两个函数。

  1. 我尝试调用 bash
  2. ./script run delete_code run stop_node_pool - 但它执行第一个函数并停止。第二个功能没有启动。
  3. ./script delete_code - 如果我们给一个函数命名它对特定函数的作用。但是我需要调用我在 bash 中执行脚本时选择的两个函数。
  4. 如何编写一个程序来执行 script.sh 文件中可用函数列表中的任意两个函数。

我的示例代码

#!/bin/bash
   
    
    delete_code() { 
    echo "code deleted"
    $1 && shift && "@a"
    }
    
    create_code() { 
    echo "code created"
    $1 && shift && "@a"
            }
    
    stop_node_pool() { 
    echo "node pool stopped"
    $1 && shift && "@a"
    }
    
    start_node_pool() { 
    echo "node pool start"
    $1 && shift && "@a"
    }

EXECUTION 
case $1 in
    delete_code) "$@"; exit;;
    create_code) "$@"; exit;;
    stop_node_pool) "$@"; exit;;
    start_node_pool) "$@"; exit;;

esac

delete_code
create_code
stop_node_pool
start_node_pool

【问题讨论】:

  • 不,我的问题很简单,从上面的函数每次任意两个函数都需要执行时调用script.sh文件。
  • ./script delete_code; ./script stop_node_pool 或添加第二个参数($2)并使用它来调用第二个函数:./script delete_code stop_node_pool
  • 恐怕你的问题很不清楚。你能具体说明你想做什么。 “一次只执行两个功能”太混乱了。期望的过程/输出的一个例子会很好。

标签: bash shell loops switch-statement sh


【解决方案1】:

我不确定我是否理解您的问题(您的问题令人困惑)但也许这会解决您的问题:

#!/bin/bash

usage() { echo "Usage: $0 <code_deleted|code_created|node_pool_stopped|node_pool_start>" 1>&2; exit 1; }

# unless there are 2 arguments, print the "usage" and exit
[ ! $# -eq 2 ] && usage

# Functions
delete_code() {
    echo "code deleted test"
}

create_code() {
    echo "code created test"
}

stop_node_pool() {
    echo "node pool stopped test"
}

start_node_pool() {
    echo "node pool start test"
}

# Execution
for i in "$@"
do

    case "$i" in

        code_deleted)
            delete_code &
            ;;

        code_created)
            create_code &
            ;;

        node_pool_stopped)
            stop_node_pool &
            ;;

        node_pool_start)
            start_node_pool &
            ;;

        *)
            usage
            ;;
    esac
done
wait

【讨论】:

  • 这意味着 OP 确实想要同时创建和删除。虽然可能是这种情况(因为 OP “一次”写了),但我们不能肯定地说。整个问题是如此令人困惑和措辞不当,以至于 IMO 无法给出合理的答案,除非问题本身得到改进。
  • 哦,是的-我没有那样想-我真的不知道OP想要什么-尽管我花了10分钟,所以我想我应该发布一个答案以防万一有帮助,但我怀疑除非提供更多详细信息,否则它将关闭。
  • 我的建议:如果我花超过几分钟的时间仅仅理解英语水平的问题的句子,我投票赞成关闭问题。我自己不是英语母语人士,我很同情那些写不出一个可以理解的句子,但至少可以写代码的人,它显示了被调用的脚本,以及脚本是如何被调用的,以及调用的效果如何预计会有。脚本是程序,独立于母语。
  • 很抱歉给您带来不便,下次肯定会更好地改善我的沟通,但以上答案对我有用,感谢 jared_mamrot。
  • 根据上面的代码,两个函数并行运行,所以我做了一个小的修正。现在效果很好,谢谢。
【解决方案2】:
#!/bin/bash

usage() { echo "Usage: $0 <code_deleted|code_created|node_pool_stopped|node_pool_start>" 1>&2; exit 1; }

# unless there are 2 arguments, print the "usage" and exit
[ ! $# -eq 2 ] && usage

# Functions
delete_code() {
    echo "code deleted test"
}

create_code() {
    echo "code created test"
}

stop_node_pool() {
    echo "node pool stopped test"
}

start_node_pool() {
    echo "node pool start test"
}

# Execution
for i in "$@"
do

    case "$i" in

        code_deleted)
            delete_code &
            ;;

        code_created)
            create_code &
            ;;

        node_pool_stopped)
            stop_node_pool &
            ;;

        node_pool_start)
            start_node_pool &
            ;;

        *)
            usage
            ;;
    esac
wait
done

【讨论】:

    猜你喜欢
    • 2021-10-22
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 2014-02-07
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多