循环时以字符串连接输入

循环时以字符串连接输入

本文介绍了循环时以字符串连接输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

SOURCES="a b c d e"

我希望用户为每个源输入一个目标,因此我希望将此输入存储到类似于上面的字符串中,但包含目标.如果我要分配a = 1,b = 2 ...等,我将得到以下内容:

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:

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

但是,如果我在 $ DESTINATIONS 上执行了 echo ,我发现它为空.而且,在每个循环中,我的shell都会告诉我:

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

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

知道我在哪里做错了吗?

Any idea where I'm doing wrong?

推荐答案

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

为我工作.

这篇关于循环时以字符串连接输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 04:55