Read a file line by line assigning the value to a variable

后端 未结 10 1222
说谎
说谎 2020-11-21 07:37

I have the following .txt file:

Marco
Paolo
Antonio

I want to read it line-by-line, and for each

10条回答
  •  你的背包
    2020-11-21 08:28

    If you need to process both the input file and user input (or anything else from stdin), then use the following solution:

    #!/bin/bash
    exec 3<"$1"
    while IFS='' read -r -u 3 line || [[ -n "$line" ]]; do
        read -p "> $line (Press Enter to continue)"
    done
    

    Based on the accepted answer and on the bash-hackers redirection tutorial.

    Here, we open the file descriptor 3 for the file passed as the script argument and tell read to use this descriptor as input (-u 3). Thus, we leave the default input descriptor (0) attached to a terminal or another input source, able to read user input.

提交回复
热议问题