相关疑难解决方法(0)

bash shell 函数中的循环名称引用,但不在 ksh 中

我正在编写一组希望在 Bash 和 KornShell93 中使用的 shell 函数,但是使用 Bash 我遇到了“循环名称引用”警告。

这是问题的本质:

function set_it {
    typeset -n var="$1"

    var="hello:$var"
}

function call_it {
    typeset -n var="$1"

    set_it var
}

something="boff"
call_it something
echo "$something"
Run Code Online (Sandbox Code Playgroud)

运行它:

$ ksh script.sh
hello:boff

$ bash script.sh
script.sh: line 4: warning: var: circular name reference
hello:
Run Code Online (Sandbox Code Playgroud)

KornShell93 完全符合我的要求,但是 Bash 失败了,并且如果something脚本中的变量被命名,还会在第 2 行警告同样的事情var

我希望var变量是每个函数的局部变量,这就是我使用 的原因typeset,但 Bash 似乎不喜欢将 nameref “取消引用”到与 nameref 本身同名的变量。我不能使用,local -n或者declare -n因为它会在ksh缺少这些的情况下破裂,即使我这样做了,也不能解决问题。

我发现的唯一解决方案是 …

bash ksh function variable typeset

7
推荐指数
1
解决办法
1010
查看次数

bash 数组数组

试图编写一些嵌套循环,但我不知道如何编写它。也许我看错了方向,但我想写的是:

declare -a bar=("alpha" "bravo" "charlie")
declare -a foo=("delta" "echo" "foxtrot" "golf")

declare -a subgroups=("bar" "foo")
Run Code Online (Sandbox Code Playgroud)

那么我想迭代子组(将来会出现更多barfoo),并在它们内部迭代它们,因为它们可以具有不同数量的元素。

所需的输出类似于:

group name: bar with group members: alpha bravo charlie
        working on alpha of the bar group
        working on bravo of the bar group
        working on charlie of the bar group
group name: foo with group members: delta echo foxtrot golf
        working on delta of the foo group
        working on echo of the foo group
        working on foxtrot of …
Run Code Online (Sandbox Code Playgroud)

bash array shell-script indexing

4
推荐指数
1
解决办法
4723
查看次数

标签 统计

bash ×2

array ×1

function ×1

indexing ×1

ksh ×1

shell-script ×1

typeset ×1

variable ×1