Bash file descriptor and write printf to a file in a while loop











up vote
1
down vote

favorite












I'm learning bash programming through a book and now I'm learning about opening files and file descriptors but I can't get this code to work(see below), I want to use the file descriptor 4 to write the output of the lower while loop to a file somefile24.log but somefile24.log is empty after I run the code.



Here is the code:



#!/bin/bash
#
# openFile.sh: print the contents of orders.txt

shopt -s -o nounset

declare LINE

exec 3< orders.txt
while read LINE <&3
do
printf "%sn" "$LINE"
done


echo
echo "Here is the new part of the program!"


exec 4> somefile24.log
count=1
while read LINE <&3
do
printf "%s-----count=%dn" "$LINE" "$count" >&4
let count++
done
exit 0


Here is the output:



(something78@notemDEB78)-(01:45:03)-(~/Bash_Programming_2018)
$./openFile.sh
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something

Here is the new part of the program!


Expected output is:



The script writes the contents of orders.txt to somefile24.log





I've tried to check the code in shellcheck but it doesn't see the problem and only gives me this:



$ shellcheck myscript

Line 10:
while read LINE <&3
^-- SC2162: read without -r will mangle backslashes.

Line 22:
while read LINE <&3
^-- SC2162: read without -r will mangle backslashes.

Line 25:
let count++
^-- SC2219: Instead of 'let expr', prefer (( expr )) .


I've tried to make a test on the command line:



(something78@notemDEB78)-(01:59:41)-(~/Bash_Programming_2018)
$exec 3>somefile24.log
(something78@notemDEB78)-(01:59:59)-(~/Bash_Programming_2018)
$echo testing >&3
(something78@notemDEB78)-(02:00:03)-(~/Bash_Programming_2018)
$cat somefile24.log
testing


I know that I can just do:



while
printf "%sn" "$LINE" >> somefile24.log


QUESTION:



Is it possible to use a file descriptor to write to a file like this in a loop and if so how and what am I doing wrong?



And why is somefile24.log alway's empty after I run the script?










share|improve this question




























    up vote
    1
    down vote

    favorite












    I'm learning bash programming through a book and now I'm learning about opening files and file descriptors but I can't get this code to work(see below), I want to use the file descriptor 4 to write the output of the lower while loop to a file somefile24.log but somefile24.log is empty after I run the code.



    Here is the code:



    #!/bin/bash
    #
    # openFile.sh: print the contents of orders.txt

    shopt -s -o nounset

    declare LINE

    exec 3< orders.txt
    while read LINE <&3
    do
    printf "%sn" "$LINE"
    done


    echo
    echo "Here is the new part of the program!"


    exec 4> somefile24.log
    count=1
    while read LINE <&3
    do
    printf "%s-----count=%dn" "$LINE" "$count" >&4
    let count++
    done
    exit 0


    Here is the output:



    (something78@notemDEB78)-(01:45:03)-(~/Bash_Programming_2018)
    $./openFile.sh
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something
    something and something and something

    Here is the new part of the program!


    Expected output is:



    The script writes the contents of orders.txt to somefile24.log





    I've tried to check the code in shellcheck but it doesn't see the problem and only gives me this:



    $ shellcheck myscript

    Line 10:
    while read LINE <&3
    ^-- SC2162: read without -r will mangle backslashes.

    Line 22:
    while read LINE <&3
    ^-- SC2162: read without -r will mangle backslashes.

    Line 25:
    let count++
    ^-- SC2219: Instead of 'let expr', prefer (( expr )) .


    I've tried to make a test on the command line:



    (something78@notemDEB78)-(01:59:41)-(~/Bash_Programming_2018)
    $exec 3>somefile24.log
    (something78@notemDEB78)-(01:59:59)-(~/Bash_Programming_2018)
    $echo testing >&3
    (something78@notemDEB78)-(02:00:03)-(~/Bash_Programming_2018)
    $cat somefile24.log
    testing


    I know that I can just do:



    while
    printf "%sn" "$LINE" >> somefile24.log


    QUESTION:



    Is it possible to use a file descriptor to write to a file like this in a loop and if so how and what am I doing wrong?



    And why is somefile24.log alway's empty after I run the script?










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm learning bash programming through a book and now I'm learning about opening files and file descriptors but I can't get this code to work(see below), I want to use the file descriptor 4 to write the output of the lower while loop to a file somefile24.log but somefile24.log is empty after I run the code.



      Here is the code:



      #!/bin/bash
      #
      # openFile.sh: print the contents of orders.txt

      shopt -s -o nounset

      declare LINE

      exec 3< orders.txt
      while read LINE <&3
      do
      printf "%sn" "$LINE"
      done


      echo
      echo "Here is the new part of the program!"


      exec 4> somefile24.log
      count=1
      while read LINE <&3
      do
      printf "%s-----count=%dn" "$LINE" "$count" >&4
      let count++
      done
      exit 0


      Here is the output:



      (something78@notemDEB78)-(01:45:03)-(~/Bash_Programming_2018)
      $./openFile.sh
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something

      Here is the new part of the program!


      Expected output is:



      The script writes the contents of orders.txt to somefile24.log





      I've tried to check the code in shellcheck but it doesn't see the problem and only gives me this:



      $ shellcheck myscript

      Line 10:
      while read LINE <&3
      ^-- SC2162: read without -r will mangle backslashes.

      Line 22:
      while read LINE <&3
      ^-- SC2162: read without -r will mangle backslashes.

      Line 25:
      let count++
      ^-- SC2219: Instead of 'let expr', prefer (( expr )) .


      I've tried to make a test on the command line:



      (something78@notemDEB78)-(01:59:41)-(~/Bash_Programming_2018)
      $exec 3>somefile24.log
      (something78@notemDEB78)-(01:59:59)-(~/Bash_Programming_2018)
      $echo testing >&3
      (something78@notemDEB78)-(02:00:03)-(~/Bash_Programming_2018)
      $cat somefile24.log
      testing


      I know that I can just do:



      while
      printf "%sn" "$LINE" >> somefile24.log


      QUESTION:



      Is it possible to use a file descriptor to write to a file like this in a loop and if so how and what am I doing wrong?



      And why is somefile24.log alway's empty after I run the script?










      share|improve this question















      I'm learning bash programming through a book and now I'm learning about opening files and file descriptors but I can't get this code to work(see below), I want to use the file descriptor 4 to write the output of the lower while loop to a file somefile24.log but somefile24.log is empty after I run the code.



      Here is the code:



      #!/bin/bash
      #
      # openFile.sh: print the contents of orders.txt

      shopt -s -o nounset

      declare LINE

      exec 3< orders.txt
      while read LINE <&3
      do
      printf "%sn" "$LINE"
      done


      echo
      echo "Here is the new part of the program!"


      exec 4> somefile24.log
      count=1
      while read LINE <&3
      do
      printf "%s-----count=%dn" "$LINE" "$count" >&4
      let count++
      done
      exit 0


      Here is the output:



      (something78@notemDEB78)-(01:45:03)-(~/Bash_Programming_2018)
      $./openFile.sh
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something
      something and something and something

      Here is the new part of the program!


      Expected output is:



      The script writes the contents of orders.txt to somefile24.log





      I've tried to check the code in shellcheck but it doesn't see the problem and only gives me this:



      $ shellcheck myscript

      Line 10:
      while read LINE <&3
      ^-- SC2162: read without -r will mangle backslashes.

      Line 22:
      while read LINE <&3
      ^-- SC2162: read without -r will mangle backslashes.

      Line 25:
      let count++
      ^-- SC2219: Instead of 'let expr', prefer (( expr )) .


      I've tried to make a test on the command line:



      (something78@notemDEB78)-(01:59:41)-(~/Bash_Programming_2018)
      $exec 3>somefile24.log
      (something78@notemDEB78)-(01:59:59)-(~/Bash_Programming_2018)
      $echo testing >&3
      (something78@notemDEB78)-(02:00:03)-(~/Bash_Programming_2018)
      $cat somefile24.log
      testing


      I know that I can just do:



      while
      printf "%sn" "$LINE" >> somefile24.log


      QUESTION:



      Is it possible to use a file descriptor to write to a file like this in a loop and if so how and what am I doing wrong?



      And why is somefile24.log alway's empty after I run the script?







      bash logging file-descriptor io-redirection






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 22:59

























      asked Nov 11 at 2:16









      somethingSomething

      29321128




      29321128
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          You already read the contents of the file in your first loop. Hence the file descriptor 3 is already at EOF. You can reset the file descriptor 3 before you read on it again. E.g. before your second loop, run this again exec 3< orders.txt






          share|improve this answer

















          • 1




            Personally, I prefer to attach the redirect directly to the while loop (rather than using exec before the loop), i.e. use while read ... done 3<orders.txt. This idiom automatically solves the problem by reopening the file for each loop.
            – Gordon Davisson
            Nov 11 at 7:34










          • I also prefer that. This only answers why he’s not getting the behavior he expects.
            – ssemilla
            Nov 11 at 7:37


















          up vote
          1
          down vote













          After the first loop the file descriptor is at EOF. So you need to reset the file descriptor before going in to the second loop.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245279%2fbash-file-descriptor-and-write-printf-to-a-file-in-a-while-loop%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote



            accepted










            You already read the contents of the file in your first loop. Hence the file descriptor 3 is already at EOF. You can reset the file descriptor 3 before you read on it again. E.g. before your second loop, run this again exec 3< orders.txt






            share|improve this answer

















            • 1




              Personally, I prefer to attach the redirect directly to the while loop (rather than using exec before the loop), i.e. use while read ... done 3<orders.txt. This idiom automatically solves the problem by reopening the file for each loop.
              – Gordon Davisson
              Nov 11 at 7:34










            • I also prefer that. This only answers why he’s not getting the behavior he expects.
              – ssemilla
              Nov 11 at 7:37















            up vote
            4
            down vote



            accepted










            You already read the contents of the file in your first loop. Hence the file descriptor 3 is already at EOF. You can reset the file descriptor 3 before you read on it again. E.g. before your second loop, run this again exec 3< orders.txt






            share|improve this answer

















            • 1




              Personally, I prefer to attach the redirect directly to the while loop (rather than using exec before the loop), i.e. use while read ... done 3<orders.txt. This idiom automatically solves the problem by reopening the file for each loop.
              – Gordon Davisson
              Nov 11 at 7:34










            • I also prefer that. This only answers why he’s not getting the behavior he expects.
              – ssemilla
              Nov 11 at 7:37













            up vote
            4
            down vote



            accepted







            up vote
            4
            down vote



            accepted






            You already read the contents of the file in your first loop. Hence the file descriptor 3 is already at EOF. You can reset the file descriptor 3 before you read on it again. E.g. before your second loop, run this again exec 3< orders.txt






            share|improve this answer












            You already read the contents of the file in your first loop. Hence the file descriptor 3 is already at EOF. You can reset the file descriptor 3 before you read on it again. E.g. before your second loop, run this again exec 3< orders.txt







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 3:22









            ssemilla

            2,362421




            2,362421








            • 1




              Personally, I prefer to attach the redirect directly to the while loop (rather than using exec before the loop), i.e. use while read ... done 3<orders.txt. This idiom automatically solves the problem by reopening the file for each loop.
              – Gordon Davisson
              Nov 11 at 7:34










            • I also prefer that. This only answers why he’s not getting the behavior he expects.
              – ssemilla
              Nov 11 at 7:37














            • 1




              Personally, I prefer to attach the redirect directly to the while loop (rather than using exec before the loop), i.e. use while read ... done 3<orders.txt. This idiom automatically solves the problem by reopening the file for each loop.
              – Gordon Davisson
              Nov 11 at 7:34










            • I also prefer that. This only answers why he’s not getting the behavior he expects.
              – ssemilla
              Nov 11 at 7:37








            1




            1




            Personally, I prefer to attach the redirect directly to the while loop (rather than using exec before the loop), i.e. use while read ... done 3<orders.txt. This idiom automatically solves the problem by reopening the file for each loop.
            – Gordon Davisson
            Nov 11 at 7:34




            Personally, I prefer to attach the redirect directly to the while loop (rather than using exec before the loop), i.e. use while read ... done 3<orders.txt. This idiom automatically solves the problem by reopening the file for each loop.
            – Gordon Davisson
            Nov 11 at 7:34












            I also prefer that. This only answers why he’s not getting the behavior he expects.
            – ssemilla
            Nov 11 at 7:37




            I also prefer that. This only answers why he’s not getting the behavior he expects.
            – ssemilla
            Nov 11 at 7:37












            up vote
            1
            down vote













            After the first loop the file descriptor is at EOF. So you need to reset the file descriptor before going in to the second loop.






            share|improve this answer

























              up vote
              1
              down vote













              After the first loop the file descriptor is at EOF. So you need to reset the file descriptor before going in to the second loop.






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                After the first loop the file descriptor is at EOF. So you need to reset the file descriptor before going in to the second loop.






                share|improve this answer












                After the first loop the file descriptor is at EOF. So you need to reset the file descriptor before going in to the second loop.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 3:34









                Sandalu Kalpanee

                266




                266






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245279%2fbash-file-descriptor-and-write-printf-to-a-file-in-a-while-loop%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine