Bash在读取文件的循环内读取

Sou*_*ing 3 bash while-loop

我正在编写一个脚本,从csv文件中提取数据,操纵数据,然后询问用户更改是否正确.问题是您似乎无法在正在读取文件的while循环中执行读取命令.下面包含一个测试脚本,注意如果没有真正使用它,则需要创建一个in文件.这只是我正在处理的更大脚本的摘录.我正在重新编码它以使用似乎有效的数组,但想知道是否有任何解决方法?我一直在阅读几个bash指南,以及阅读的手册页,但没有找到答案.提前致谢.

#!/bin/bash
#########
file="./in.csv"
OLDIFS=$IFS
IFS=","
#########

while read custdir custuser
do
    echo "Reading within the loop"
    read what
    echo $what
done < $file

IFS=$OLDIFS
Run Code Online (Sandbox Code Playgroud)

pax*_*blo 7

您可以调整文件句柄,以便仍然可以访问旧的标准输入.例如,此文件qq.sh将自行读取并使用您的read循环打印每一行,在每行后问您一个问题:

while read line
do
    echo "    Reading within the loop: [$line]"
    echo -n "    What do you want to say? "
    read -u 3 something
    echo "    You input: [$something]"
done 3<&0 <qq.sh
Run Code Online (Sandbox Code Playgroud)

它通过首先将标准输入(文件句柄0)保存到文件句柄3中3<&0,然后使用read -u <filehandle>变量从文件句柄3中读取来完成此操作.简单的记录:

pax> ./qq.sh
    Reading within the loop: [while read line]
    What do you want to say? a
    You input: [a]
    Reading within the loop: [do]
    What do you want to say? b
    You input: [b]
    Reading within the loop: [echo "Reading within the loop: [$line]"]
    What do you want to say? c
    You input: [c]
    Reading within the loop: [echo -n "What do you want to say? "]
    What do you want to say? d
    You input: [d]
    Reading within the loop: [read -u 3 something]
    What do you want to say? e
    You input: [e]
    Reading within the loop: [echo "You input: [$something]"]
    What do you want to say? f
    You input: [f]
    Reading within the loop: [done 3<&0 <qq.sh]
    What do you want to say? g
    You input: [g]
    Reading within the loop: []
    What do you want to say? h
    You input: [h]
pax> _
Run Code Online (Sandbox Code Playgroud)