Bash 在for循环中迭代有序路径变量
我有以下txt文档:Bash 在for循环中迭代有序路径变量,bash,shell,for-loop,Bash,Shell,For Loop,我有以下txt文档: deviceIDs.txt UDID0=LGH811dec0bfd6 UDID1=41006289e4b2a179 UDID2=d9a7aa45 PORT0=4567 PORT1=4568 PORT2=4569 BOOTPORT0=5556 BOOTPORT1=5557 BOOTPORT2=5558 我希望能够编写以下bash脚本: #!/bin/bash source /path/deviceIDs.txt for ((i=0;i<=2;i++)) do
deviceIDs.txt
UDID0=LGH811dec0bfd6
UDID1=41006289e4b2a179
UDID2=d9a7aa45
PORT0=4567
PORT1=4568
PORT2=4569
BOOTPORT0=5556
BOOTPORT1=5557
BOOTPORT2=5558
我希望能够编写以下bash脚本:
#!/bin/bash
source /path/deviceIDs.txt
for ((i=0;i<=2;i++))
do
echo $UDID$i
echo $PORT$i
echo $BOOTPORT$i
done
#/bin/bash
source/path/deviceIDs.txt
对于((i=0;i我不知道John1024对!y的解
如果您无法修改DeviceID.txt,因为它是由您无法控制的人生成的,您也可以使用如下数组:
#!/bin/bash
source /path/deviceIDs.txt
u=($UDID{0..2});p=($PORT{0..2});b=($BOOTPORT{0..2})
for i in {0..2}; do echo -e ${u[i]}"\n"${p[i]}"\n"${b[i]}"\n"; done
LGH811dec0bfd6
4567
5556
41006289e4b2a179
4568
5557
d9a7aa45
4569
5558
您可以使用bash的间接寻址形式:
可供替代的
如果允许更改数据文件格式,请考虑将变量定义为这样的数组:
$ cat IDarrays.txt
UDID=(LGH811dec0bfd6 41006289e4b2a179 d9a7aa45)
PORT=(4567 4568 4569)
BOOTPORT=(5556 5557 5558)
这样,就可以编写脚本:
#!/bin/bash
source IDarrays.txt
for ((i=0;i<=2;i++))
do
echo ${UDID[$i]}
echo ${PORT[$i]}
echo ${BOOTPORT[$i]}
done
!/bin/bash
源IDarrays.txt
对于((i=0;isource
文件,然后使用@EtanReisner建议的方法。正如John1024的答案和来自Etan show的链接所示,实际上在bash中也有另一种方法
$ cat IDarrays.txt
UDID=(LGH811dec0bfd6 41006289e4b2a179 d9a7aa45)
PORT=(4567 4568 4569)
BOOTPORT=(5556 5557 5558)
#!/bin/bash
source IDarrays.txt
for ((i=0;i<=2;i++))
do
echo ${UDID[$i]}
echo ${PORT[$i]}
echo ${BOOTPORT[$i]}
done