Skip to main content
Became Hot Network Question
deleted 1 character in body
Source Link
ilkkachu
  • 148.1k
  • 16
  • 268
  • 441

I found out I don't totally understand how bash file descriptors work. I would like to have some guidance here.

I have a scenario where I need to read the content of a file, and for each line of the file, execute another read to get the user's selection. So I need an extra fd, and help to do that.

I got the idea from this answer and came out with this solution:

#!/bin/bash
    while read content <&4; do   
        echo
        read  -p 'Do you want to proceed [y|n] ?' choice 
        case "$choice" in
            ([yY]) bash $content ;;
            ([nN]) continue ;;
        esac
    done 4< a_text_file

What I understand is, the file a_text_file is assigned to file descriptor 4, and then bash uses the read built-in to read the content of text file a_text_file by using the file descriptor `44.

But now I realize my understanding is incorrect, as in the bash GNU docs, it says:

[n]<&word

"is used to duplicate input file descriptors. If word expands to one or more digits, file descriptor n is made to be a copy of that file descriptor. It is a redirection error if the digits in word do not specify a file descriptor open for input. If n is not specified, this uses the standard input (file descriptor 0)."

So there is duplicating of file descriptors happening. My question is:

  1. What does the above operation have to do with 'duplicate input file descriptor'?
  2. Which file descriptor is being duplicated?
  3. Why does bash need to do that to read the file content using the file descriptor 4?
  4. And please also point out which concept of fd I have gotten totally wrong.

Also, I would be very grateful if anyone could recommend any good online resource that could explain the concept of fd well, as I really can't grasp it by just reading the GNU docs.

I found out I don't totally understand how bash file descriptors work. I would like to have some guidance here.

I have a scenario where I need to read the content of a file, and for each line of the file, execute another read to get the user's selection. So I need an extra fd, and help to do that.

I got the idea from this answer and came out with this solution:

#!/bin/bash
    while read content <&4; do   
        echo
        read  -p 'Do you want to proceed [y|n] ?' choice 
        case "$choice" in
            ([yY]) bash $content ;;
            ([nN]) continue ;;
        esac
    done 4< a_text_file

What I understand is, the file a_text_file is assigned to file descriptor 4, and then bash uses the read built-in to read the content of text file a_text_file by using the file descriptor `4.

But now I realize my understanding is incorrect, as in the bash GNU docs, it says:

[n]<&word

"is used to duplicate input file descriptors. If word expands to one or more digits, file descriptor n is made to be a copy of that file descriptor. It is a redirection error if the digits in word do not specify a file descriptor open for input. If n is not specified, this uses the standard input (file descriptor 0)."

So there is duplicating of file descriptors happening. My question is:

  1. What does the above operation have to do with 'duplicate input file descriptor'?
  2. Which file descriptor is being duplicated?
  3. Why does bash need to do that to read the file content using the file descriptor 4?
  4. And please also point out which concept of fd I have gotten totally wrong.

Also, I would be very grateful if anyone could recommend any good online resource that could explain the concept of fd well, as I really can't grasp it by just reading the GNU docs.

I found out I don't totally understand how bash file descriptors work. I would like to have some guidance here.

I have a scenario where I need to read the content of a file, and for each line of the file, execute another read to get the user's selection. So I need an extra fd, and help to do that.

I got the idea from this answer and came out with this solution:

#!/bin/bash
    while read content <&4; do   
        echo
        read  -p 'Do you want to proceed [y|n] ?' choice 
        case "$choice" in
            ([yY]) bash $content ;;
            ([nN]) continue ;;
        esac
    done 4< a_text_file

What I understand is, the file a_text_file is assigned to file descriptor 4, and then bash uses the read built-in to read the content of text file a_text_file by using the file descriptor 4.

But now I realize my understanding is incorrect, as in the bash GNU docs, it says:

[n]<&word

"is used to duplicate input file descriptors. If word expands to one or more digits, file descriptor n is made to be a copy of that file descriptor. It is a redirection error if the digits in word do not specify a file descriptor open for input. If n is not specified, this uses the standard input (file descriptor 0)."

So there is duplicating of file descriptors happening. My question is:

  1. What does the above operation have to do with 'duplicate input file descriptor'?
  2. Which file descriptor is being duplicated?
  3. Why does bash need to do that to read the file content using the file descriptor 4?
  4. And please also point out which concept of fd I have gotten totally wrong.

Also, I would be very grateful if anyone could recommend any good online resource that could explain the concept of fd well, as I really can't grasp it by just reading the GNU docs.

minor fixes to language and spelling
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

Why does bash need File Descriptors duplicationgfile descriptor duplicating?

I found out I don't totally understand how bash file descriptors workswork. WouldI would like to have some guidance here.

I have a scenario where I need to read the content of a file, and for each line of the file, execute another readread to get user choicethe user's selection. So I need an extra fd, and help to do that.

I getgot the idea from this answer and comecame out with this solution:

#!/bin/bash
    while read content <&4; do   
        echo
        read  -p 'Do you want to proceed [y|n] ?' choice 
        case "$choice" in
            ([yY]) bash $content ;;
            ([nN]) continue ;;
        esac
    done 4< a_text_file

What I understand is, the file a_text_file assign itselfis assigned to file descriptor 44, and then the bash useuses the read built-in to read the content of text file a_text_file by using the file descriptor 4`4.

But now I realize my understandunderstanding is incorrect, as fromin the bash GNU docs, it says:

[n]<&word

"is used to duplicate input file descriptors. If word expands to one or more digits, file descriptor n is made to be a copy of that file descriptor. It is a redirection error if the digits in word do not specify a file descriptor open for input. If n is not specified, this uses the standard input (file descriptor 0)."

So there is duplicating of file descriptors happenedhappening. My question is:

  1. how isWhat does the above operation hashave to do with 'duplicate input file descriptor' descriptor'?
  2. whichWhich file descriptor is being duplicated  ?
  3. whyWhy does bash needsneed to do that to read the file content using the file descriptor 4 4?
  4. andAnd please also point out also which concept of fd that I get ithave gotten totally wrong.

Also, I would be very grateful if anyone could recommend me any good online resource that could explain thisthe concept of fd well, as the I really can't grasp it by just reading the GNU docs.

Why does bash need File Descriptors duplicationg?

I found out I don't totally understand how bash file descriptors works. Would like to have some guidance here.

I have a scenario where I need to read the content of a file, and for each line of the file, execute another read to get user choice. So I need an extra fd help to do that.

I get the idea from this answer and come out with this solution:

#!/bin/bash
    while read content <&4; do   
        echo
        read  -p 'Do you want to proceed [y|n] ?' choice 
        case "$choice" in
            ([yY]) bash $content ;;
            ([nN]) continue ;;
        esac
    done 4< a_text_file

What I understand is, the file a_text_file assign itself to file descriptor 4, and then the bash use read built-in to read the content of text file a_text_file by using the file descriptor 4.

But now I realize my understand is incorrect, as from the bash GNU docs, it says:

[n]<&word

"is used to duplicate input file descriptors. If word expands to one or more digits, file descriptor n is made to be a copy of that file descriptor. It is a redirection error if the digits in word do not specify a file descriptor open for input. If n is not specified, this uses the standard input (file descriptor 0)."

So there is duplicating of file descriptors happened. My question is:

  1. how is the above operation has to do with 'duplicate input file descriptor' ?
  2. which file descriptor is being duplicated  ?
  3. why bash needs to do that to read the file content using the file descriptor 4 ?
  4. and please point out also which concept of fd that I get it totally wrong

Also I would be very grateful if anyone could recommend me any good online resource that could explain this concept of fd well, as the I really can't grasp it by just reading the GNU docs.

Why does bash need file descriptor duplicating?

I found out I don't totally understand how bash file descriptors work. I would like to have some guidance here.

I have a scenario where I need to read the content of a file, and for each line of the file, execute another read to get the user's selection. So I need an extra fd, and help to do that.

I got the idea from this answer and came out with this solution:

#!/bin/bash
    while read content <&4; do   
        echo
        read  -p 'Do you want to proceed [y|n] ?' choice 
        case "$choice" in
            ([yY]) bash $content ;;
            ([nN]) continue ;;
        esac
    done 4< a_text_file

What I understand is, the file a_text_file is assigned to file descriptor 4, and then bash uses the read built-in to read the content of text file a_text_file by using the file descriptor `4.

But now I realize my understanding is incorrect, as in the bash GNU docs, it says:

[n]<&word

"is used to duplicate input file descriptors. If word expands to one or more digits, file descriptor n is made to be a copy of that file descriptor. It is a redirection error if the digits in word do not specify a file descriptor open for input. If n is not specified, this uses the standard input (file descriptor 0)."

So there is duplicating of file descriptors happening. My question is:

  1. What does the above operation have to do with 'duplicate input file descriptor'?
  2. Which file descriptor is being duplicated?
  3. Why does bash need to do that to read the file content using the file descriptor 4?
  4. And please also point out which concept of fd I have gotten totally wrong.

Also, I would be very grateful if anyone could recommend any good online resource that could explain the concept of fd well, as I really can't grasp it by just reading the GNU docs.

Source Link
sylye
  • 718
  • 2
  • 7
  • 15

Why does bash need File Descriptors duplicationg?

I found out I don't totally understand how bash file descriptors works. Would like to have some guidance here.

I have a scenario where I need to read the content of a file, and for each line of the file, execute another read to get user choice. So I need an extra fd help to do that.

I get the idea from this answer and come out with this solution:

#!/bin/bash
    while read content <&4; do   
        echo
        read  -p 'Do you want to proceed [y|n] ?' choice 
        case "$choice" in
            ([yY]) bash $content ;;
            ([nN]) continue ;;
        esac
    done 4< a_text_file

What I understand is, the file a_text_file assign itself to file descriptor 4, and then the bash use read built-in to read the content of text file a_text_file by using the file descriptor 4.

But now I realize my understand is incorrect, as from the bash GNU docs, it says:

[n]<&word

"is used to duplicate input file descriptors. If word expands to one or more digits, file descriptor n is made to be a copy of that file descriptor. It is a redirection error if the digits in word do not specify a file descriptor open for input. If n is not specified, this uses the standard input (file descriptor 0)."

So there is duplicating of file descriptors happened. My question is:

  1. how is the above operation has to do with 'duplicate input file descriptor' ?
  2. which file descriptor is being duplicated ?
  3. why bash needs to do that to read the file content using the file descriptor 4 ?
  4. and please point out also which concept of fd that I get it totally wrong

Also I would be very grateful if anyone could recommend me any good online resource that could explain this concept of fd well, as the I really can't grasp it by just reading the GNU docs.