多变量 For 循环

use*_*244 12 bash command-substitution for

有没有办法在for循环中指定多个变量(不仅仅是整数)bash?我可能有 2 个文件,其中包含我需要处理的任意文本。

我在功能上需要的是这样的:

for i in $(cat file1) and j in $(cat file2); do command $i $j; done
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

orm*_*aaj 11

首先,不要使用 for 阅读行,因为通过分词阅读行有几个不可避免的问题。

假设文件长度相等,或者如果您只想循环直到读取两个文件中较短的文件,则可以使用一个简单的解决方案。

while read -r x && read -r y <&3; do
    ...
done <file1 3<file2
Run Code Online (Sandbox Code Playgroud)

由于 whenread返回 false 和其他几个原因,很难将更通用的解决方案放在一起。此示例可以读取任意数量的流并在最短或最长输入之后返回。

#!/usr/bin/env bash

# Open the given files and assign the resulting FDs to arrName.
# openFDs arrname file1 [file2 ...]
openFDs() {
    local x y i arr=$1

    [[ -v $arr ]] || return 1
    shift

    for x; do
        { exec {y}<"$x"; } 2>/dev/null || return 1
        printf -v "${arr}[i++]" %d "$y"
    done
}

# closeFDs FD1 [FD2 ...]
closeFDs() {
    local x
    for x; do
        exec {x}<&-
    done
}

# Read one line from each of the given FDs and assign the output to arrName.
# If the first argument is -l, returns false only when all FDs reach EOF.
# readN [ -l ] arrName FD1 [FD2 ...]
readN() {
    if [[ $1 == -l ]]; then
        local longest
        shift
    else
        local longest=
    fi

    local i x y status arr=$1
    [[ -v $arr ]] || return 1
    shift

    for x; do
        if IFS= read -ru "$x" "${arr}[i]" || { unset -v "${arr}[i]"; [[ ${longest+_} ]] && return 1; }; then
            status=0
        fi
        ((i++))
    done
    return ${status:-1}
}

# readLines file1 [file2 ...]
readLines() {
    local -a fds lines
    trap 'closeFDs "${fds[@]}"' RETURN
    openFDs fds "$@" || return 1

    while readN -l lines "${fds[@]}"; do
        printf '%-1s ' "${lines[@]}"
        echo
    done
}

{
    readLines /dev/fd/{3..6} || { echo 'error occured' >&2; exit 1; }
} <<<$'a\nb\nc\nd' 3<&0 <<<$'1\n2\n3\n4\n5' 4<&0 <<<$'x\ny\nz' 5<&0 <<<$'7\n8\n9\n10\n11\n12' 6<&0

# vim: set fenc=utf-8 ff=unix ts=4 sts=4 sw=4 ft=sh nowrap et:
Run Code Online (Sandbox Code Playgroud)

所以取决于是否readN得到-l,输出是

a 1 x 7
b 2 y 8
c 3 z 9
d 4 10
5 11
12
Run Code Online (Sandbox Code Playgroud)

或者

a 1 x 7
b 2 y 8
c 3 z 9
Run Code Online (Sandbox Code Playgroud)

必须在循环中读取多个流而不将所有内容保存到多个数组中并不常见。如果您只想读取数组,则应该查看mapfile.