bash 在循环中连接字符串中的输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42934198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:55:15  来源:igfitidea点击:

Concatenate inputs in string while in loop

stringbashshstring-concatenation

提问by Matteo NNZ

I have a variable of sources that is basically a string of comma-separated elements:

我有一个源变量,它基本上是一串逗号分隔的元素:

SOURCES="a b c d e"

I want the user to input one destination for each of this source, and I want hence to store this input into a string looking like the above but containing the destinations. If I want to assign a=1, b=2... etc, I would have something like this:

我希望用户为每个源输入一个目的地,因此我希望将此输入存储到一个类似于上述但包含目的地的字符串中。如果我想分配 a=1, b=2 ... 等等,我会有这样的事情:

echo $DESTINATIONS >>> "1 2 3 4 5"

In order to do the above, I do this:

为了做到以上几点,我这样做:

SOURCES="a b c d e"
DESTINATIONS=""

for src in $SOURCES
do
    echo Input destination to associate to the source $src:
    read dest
    DESTINATIONS=$DESTINATIONS $dest
done

However, if I do an echoon $DESTINATIONS, I find it empty. Moreover, at each loop, my shell tells me:

但是,如果我执行echoon $DESTINATIONS,我会发现它是空的。此外,在每个循环中,我的 shell 都会告诉我:

-bash: = **myInput**: command not found

Any idea where I'm doing wrong?

知道我哪里做错了吗?

采纳答案by dutiona

SOURCES="a b c d e"
DESTINATIONS=""

for src in $SOURCES
do
    echo Input destination to associate to the source $src:
    read dest
    DESTINATIONS+=" ${dest}"
done
echo $DESTINATIONS

works for me.

对我来说有效。

回答by chepner

You should be using an array, not a delimited string.

您应该使用数组,而不是分隔字符串。

sources=(a b c d e)

for src in "${sources[@]}"
do
    read -p "Input destination to associate to the source $src" dest
    destinations+=( "$dest" )
done

printf '%s\n' "${destinations[@]}"

回答by Mark

The most obvious issue with your code is this line:

您的代码最明显的问题是这一行:

DESTINATIONS=$DESTINATIONS $dest

Rather, the above line should be written:

相反,上面的行应该写成:

DESTINATIONS="$DESTINATIONS $dest"

The issue: You are executing $dest and passing an environment of DESTINATIONS=$DESTINATIONS. This hopefully explains the error messages you are seeing.

问题:您正在执行 $dest 并传递 DESTINATIONS=$DESTINATIONS 的环境。这有望解释您看到的错误消息。

I tried your code out with the quotes I've suggested and it works fine.

我用我建议的引号尝试了你的代码,它工作正常。

回答by Isaac

Q: What is wrong?
A: Not using quotes where needed.

问:怎么了?
A:在需要的地方不使用引号。

If you use an space un-quoted it will be used by the shell to split the line.

如果您使用未加引号的空格,shell 将使用它来拆分行。

When you use:

当您使用:

DESTINATIONS=$DESTINATIONS $dest

The variable $dest is understood by the shell as a command to execute, that's why you are getting the error:

shell 将变量 $dest 理解为要执行的命令,这就是您收到错误的原因:

-bash: = **myInput**: command not found

To solve the issue, just quote the space.
There are several ways to do that:

要解决问题,只需引用空格即可。
有几种方法可以做到这一点:

DESTINATIONS=$DESTINATIONS" "$dest
DESTINATIONS=$DESTINATIONS' '$dest
DESTINATIONS="$DESTINATIONS"' '"$dest"
DESTINATIONS="$DESTINATIONS $dest"

The last option is probably the simplest and also the best overall.
You could use also this syntax (since bash 3.1-alpha1):

最后一个选项可能是最简单的,也是整体上最好的。
您也可以使用此语法(自 bash 3.1-alpha1 起):

    DESTINATIONS+=" $dest"

Also, please!, quote your other expansions:

另外,请!引用您的其他扩展:

echo "$DESTINATIONS"