在循环中连接字符串中的输入
Concatenate inputs in string while in loop
我有一个源变量,它基本上是一串以逗号分隔的元素:
SOURCES="a b c d e"
我希望用户为每个源输入一个目的地,因此我想将此输入存储到一个看起来像上面但包含目的地的字符串中。如果我想分配 a=1, b=2... 等等,我会有这样的东西:
echo $DESTINATIONS >>> "1 2 3 4 5"
为了做到以上几点,我这样做了:
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 告诉我:
-bash: = **myInput**: command not found
知道我哪里做错了吗?
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
适合我。
您的代码最明显的问题是这一行:
DESTINATIONS=$DESTINATIONS $dest
相反,上面一行应该写成:
DESTINATIONS="$DESTINATIONS $dest"
问题:您正在执行 $dest 并传递 DESTINATIONS=$DESTINATIONS 的环境。这有望解释您看到的错误消息。
我用我建议的引号试过你的代码,它工作正常。
您应该使用数组,而不是分隔字符串。
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[@]}"
问:怎么了?
A: 在需要的地方不使用引号。
如果您使用未加引号的 space,shell 将使用它来拆分行。
当您使用时:
DESTINATIONS=$DESTINATIONS $dest
shell 将变量 $dest 理解为要执行的命令,这就是您收到错误的原因:
-bash: = **myInput**: command not found
要解决这个问题,只需引用space。
有几种方法可以做到这一点:
DESTINATIONS=$DESTINATIONS" "$dest
DESTINATIONS=$DESTINATIONS' '$dest
DESTINATIONS="$DESTINATIONS"' '"$dest"
DESTINATIONS="$DESTINATIONS $dest"
最后一个选项可能是最简单的,也是最好的。
您也可以使用此语法(因为 bash 3.1-alpha1):
DESTINATIONS+=" $dest"
另外,请!引用您的其他扩展:
echo "$DESTINATIONS"
我有一个源变量,它基本上是一串以逗号分隔的元素:
SOURCES="a b c d e"
我希望用户为每个源输入一个目的地,因此我想将此输入存储到一个看起来像上面但包含目的地的字符串中。如果我想分配 a=1, b=2... 等等,我会有这样的东西:
echo $DESTINATIONS >>> "1 2 3 4 5"
为了做到以上几点,我这样做了:
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 告诉我:
-bash: = **myInput**: command not found
知道我哪里做错了吗?
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
适合我。
您的代码最明显的问题是这一行:
DESTINATIONS=$DESTINATIONS $dest
相反,上面一行应该写成:
DESTINATIONS="$DESTINATIONS $dest"
问题:您正在执行 $dest 并传递 DESTINATIONS=$DESTINATIONS 的环境。这有望解释您看到的错误消息。
我用我建议的引号试过你的代码,它工作正常。
您应该使用数组,而不是分隔字符串。
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[@]}"
问:怎么了?
A: 在需要的地方不使用引号。
如果您使用未加引号的 space,shell 将使用它来拆分行。
当您使用时:
DESTINATIONS=$DESTINATIONS $dest
shell 将变量 $dest 理解为要执行的命令,这就是您收到错误的原因:
-bash: = **myInput**: command not found
要解决这个问题,只需引用space。
有几种方法可以做到这一点:
DESTINATIONS=$DESTINATIONS" "$dest
DESTINATIONS=$DESTINATIONS' '$dest
DESTINATIONS="$DESTINATIONS"' '"$dest"
DESTINATIONS="$DESTINATIONS $dest"
最后一个选项可能是最简单的,也是最好的。
您也可以使用此语法(因为 bash 3.1-alpha1):
DESTINATIONS+=" $dest"
另外,请!引用您的其他扩展:
echo "$DESTINATIONS"