bash trouble assigning to an array index in a loop

后端 未结 1 1704
情书的邮戳
情书的邮戳 2020-12-11 05:16

I can get this to work in ksh but not in bash which is really driving me nuts. Hopefully it is something obvious that I\'m overlooking.

I need to run an external com

相关标签:
1条回答
  • 2020-12-11 05:59

    Because your while loop is in a pipeline, all variable assignments in the loop body are local to the subshell in which the loop is executed. (I believe ksh does not run the command in a subshell, which is why you have the problem in bash.) Do this instead:

    while read line
    do
        array[i]="$line"
        echo "array[$i] = ${array[i]}"
        let i++
    done < junk.txt
    

    Rarely, if ever, do you want to use cat to pipe a single file to another command; use input redirection instead.

    UPDATE: since you need to run from a command and not a file, another option (if available) is process substitution:

    while read line; do
    ...
    done < <( command args ... )
    

    If process substitution is not available, you'll need to output to a temporary file and redirect input from that file.

    If you are using bash 4.2 or later, you can execute these two commands before your loop, and the original pipe-into-the-loop will work, since the while loop is the last command in the pipeline.

    set +m    # Turn off job control; it's probably already off in a non-interactive script
    shopt -s lastpipe
    cat junk.txt | while read line; do ...; done
    

    UPDATE 2: Here is a loop-less solution based on user1596414's comment

    array[0]=hello
    IFS=$'\n' array+=( $(command) )
    

    The output of your command is split into words based solely on newlines (so that each line is a separate word), and appends the resulting line-per-slot array to the original. This is very nice if you are only using the loop to build the array. It can also probably be modified to accomodate a small amount of per-line processing, vaguely similar to a Python list comprehension.

    0 讨论(0)
提交回复
热议问题