首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >传递多个参数:` `run bash -c ...`

传递多个参数:` `run bash -c ...`
EN

Stack Overflow用户
提问于 2021-11-18 19:11:28
回答 1查看 47关注 0票数 1

当尝试对一个名为some_function的函数执行assert_failure时,我遇到了一些传递多个参数的困难。

代码语言:javascript
复制
load 'libs/bats-support/load'
load 'libs/bats-assert/load'
# https://github.com/bats-core/bats-file#Index-of-all-functions
load 'libs/bats-file/load'
# https://github.com/bats-core/bats-assert#usage
load 'assert_utils'

@test "Perform some test." {
  variable_one="one"
  variable_two="two"
  variable_three="three"
  variable_four="four"
  run bash -c 'source src/some_script.sh && some_function 
  "$variable_one" "$variable_two" "$variable_three"'
  assert_failure
  assert_output "$expected_error_message"
}

其中函数由以下部分组成:

代码语言:javascript
复制
some_function() {
    local variable_one="$1"
    local variable_two="$2"
    local variable_three="$3"
    local variable_four="$4"
    echo "variable_one=$variable_one"
    echo "variable_two=$variable_two"
    echo "variable_three=$variable_three"
    echo "variable_four=$variable_four"
}

输出显示只有第一个变量成功传递,而第二到第四个变量没有成功传递:

代码语言:javascript
复制
 ✗ Verify an error is thrown, if something.
   (from function `assert_failure' in file test/libs/bats-assert/src/assert.bash, line 140,
    in test file test/test_something.bats, line 89)
     `assert_failure' failed
   
   -- command succeeded, but it was expected to fail --
   output (3 lines):
     variable_one=one
     variable_two=
     variable_three=
     variable_four=
   --

如何将多个/四个变量传递给函数,同时仍在函数上运行assert_failure

响应评论进行编辑

虽然我很感谢KamilCuk在评论中提供的实用解决方案,但它似乎允许增加特异性。例如,variable_one可能是在多个函数中使用的变量,对于这些函数的不同调用具有不同的值。因此,理想情况下,我不会在每次调用不同的函数时都覆盖“导出”值。相反,我认为将特定的参数传递给特定的函数会更好。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-18 20:00:56

正常传递参数,但跳过此上下文中保留的第一个参数:

代码语言:javascript
复制
bash -c 'source src/some_script.sh && some_function "$@"' _ 'argument a' 'argument B' 'This is c' 'Andy'

输出:

代码语言:javascript
复制
variable_one=argument a
variable_two=argument B
variable_three=This is c
variable_four=Andy
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70025471

复制
相关文章

相似问题

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