如何使用 shell 脚本创建一个空字符串列表并附加它?

如何使用 shell 脚本创建一个空字符串列表并附加它?
A=Book,Pen,Pencil...n

B=Cat,Rat,Bat.....m 

代码是::

Type=()
TEST=`echo $path`
echo $TEST

输出 ::

n/m

异常输出::

Book/Cat/Book/Rat/Book/Bat/..../Book/m/Pen/Cat/Pen/Rat/Pen/Bat/...../Pen/m/Pencil/Cat/Pencil/Rat/Pencil/Bat/..../Pencil/m/......./n/Cat/n/Rat/n/Bat/...../n/m

答案1

更改问题的要求后再次...

bash你可以使用大括号扩展:

$ printf '%s' {Book/,Pen/,Pencil/,n/}{Cat/,Rat/,Bat/,m/}
Book/Cat/Book/Rat/Book/Bat/Book/m/Pen/Cat/Pen/Rat/Pen/Bat/Pen/m/Pencil/Cat/Pencil/Rat/Pencil/Bat/Pencil/m/n/Cat/n/Rat/n/Bat/n/m/

请注意,输出不以换行符结尾。

要从andpath中的两个逗号分隔字符串创建:$A$B

#!/bin/bash

A="Book,Pen,Pencil,n"
B="Cat,Rat,Bat,m"

oldIFS=$IFS
IFS=,
for i in $A; do
    for j in $B; do
        path+="$i/$j/"
    done
done
IFS=$oldIFS

printf '%s\n' "$path"

输出:

$ ./script.sh
Book/Cat/Book/Rat/Book/Bat/Book/m/Pen/Cat/Pen/Rat/Pen/Bat/Pen/m/Pencil/Cat/Pencil/Rat/Pencil/Bat/Pencil/m/n/Cat/n/Rat/n/Bat/n/m/

答案2

尝试这个,

#complete_robotpath=() ### not used in script ... so commented
IFS=','
A=Basic-Call,Call-Hold  ## In shell, we dont prefix with $ while declaring variable and should not have space in value.
B=VoLTE-VoLTE,VoLTE-3G
read -ra ADDR1 <<< "$A" ## In shell, we should pass value of a variable by prefixing with $
read -ra ADDR2 <<< "$B" IFS=',' ## In shell, we should pass value of a variable by prefixing with $
for i in "${ADDR1[@]}";
do
  for j in "${ADDR2[@]}";
  do
  robot_path+=`echo $i/$j/` ## "+=" to concatinate string and sufix by / as expected result
  done
done

pybot_exec_cmd=`echo $robot_path`
echo $pybot_exec_cmd

输出:

Basic-Call/VoLTE-VoLTE/Basic-Call/VoLTE-3G/Call-Hold/VoLTE-VoLTE/Call-Hold/VoLTE-3G/

相关内容