提问人:a.t. 提问时间:11/19/2021 最后编辑:Benjamin W.a.t. 更新时间:11/19/2021 访问量:270
传递多个参数:“run bash -c ...'
Passing multiple arguments through: `run bash -c ...`
问:
在尝试使用一个名为 的函数时,我在传递超过 1 个参数时遇到了一些困难。assert_failure
some_function
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"
}
其中,函数包括:
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"
}
输出显示只有第一个变量成功传递,而第二个到第四个变量则未成功传递:
✗ 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
编辑以响应评论
虽然我感谢卡米尔库克在评论中提供的实际解决方案,但它似乎允许增加具体性。例如,可能是在多个函数中使用的变量,对于对这些函数的不同调用具有不同的值。因此,理想情况下,我不会在每次调用不同的函数时都覆盖“导出”值。相反,我认为最好将特定参数传递给特定函数。variable_one
答:
3赞
Léa Gris
11/19/2021
#1
正常传递参数,但跳过在此上下文中保留的第一个参数:
bash -c 'source src/some_script.sh && some_function "$@"' _ 'argument a' 'argument B' 'This is c' 'Andy'
输出:
variable_one=argument a
variable_two=argument B
variable_three=This is c
variable_four=Andy
评论
export variable_one variable_two ...
. assert_failure做什么,它是如何工作的?这不是一个标准的 bash 命令。While trying to assert_failure on a function
export
您希望子进程看到的变量。