繁体   English   中英

Bash以声明方式定义要循环的列表

[英]Bash declaratively defining a list to loop on

在bash中,我经常创建脚本,循环遍历我定义的字符串列表。

例如

for a in 1 2 3 4; do echo $a; done

但是我想定义列表(在循环之前保持它干净),以便它包含空格并且没有单独的文件:

例如(但这不起作用)

read -r VAR <<HERE
list item 1
list item 2
list item 3
...
HERE

for a in $VAR; do echo $a; done

上面的预期输出(我想):

list item 1
list item 2
list item 3
etc...

但你会得到:

list
item
1

我可以使用数组,但我必须索引数组中的每个元素( 编辑读取下面的答案,因为你可以附加到数组..我不知道你可以 )。

其他人如何在bash中声明性地定义列表而不使用单独的文件?

对不起,我忘了提到我想在for循环逻辑之前定义文件顶部的列表

您可以像这样使用“HERE Document”:

while read a ; do echo "Line: $a" ; done <<HERE
123 ab c
def aldkfgjlaskdjf lkajsdlfkjlasdjf
asl;kdfj ;laksjdf;lkj asd;lf sdpf -aa8
HERE

数组不是那么难用:

readarray <<HERE
this is my first line
this is my second line
this is my third line
HERE

# Pre bash-4, you would need to build the array more explicity
# Just like readarray defaults to MAPFILE, so read defaults to REPLY
# Tip o' the hat to Dennis Williamson for pointing out that arrays
# are easily appended to.
# while read ; do
#    MAPFILE+=("$REPLY")
# done

for a in "${MAPFILE[@]}"; do
    echo "$a"
done

如果您有这种需要,这还有一个额外的好处,即允许每个列表项包含空格。

while read -r line
do
    var+=$line$'\n'
done <<EOF
foo bar
baz qux
EOF

while read -r line
do
    echo "[$line]"
done <<<"$var"

为什么需要索引数组? 您可以附加到数组并迭代它们而不使用索引。

array+=(value)
for item in "${array[@]}"
do
    something with "$item"
done

这里有更好的答案,但您也可以分别对\\n进行读取,并使用IFS环境变量临时更改变量以分隔换行而不是for循环中的空格。

read -d \n -r VAR <<HERE
list item 1
list item 2
list item 3
HERE

IFS_BAK=$IFS
IFS="\n"
for a in $VAR; do echo $a; done
IFS=$IFS_BAK

如果你可以使用while循环而不是for循环,你可以使用while read结构和“here document”:

#!/bin/bash

while read LINE; do
    echo "${LINE}"
done << EOF
list item 1
list item 2
list item 3
EOF

ref: `cat << EOF`如何在bash中运行?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM