overwrite file and echo to stdout

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have a simple bash function to increment a number and store it in a file:



get_next_int () tee



regarding this line:



echo "$((++my_num))" > "$my_file" | tee


out of curiousity, is there a way to overwrite the file with the incremented integer and echo it to stdout?



this answer about tee doesn't seem to cover my case.



How to redirect output to a file and stdout | Stack Overflow



also, as an aside, regarding this line:



typeset -i my_num=$(cat "$my_file")


that will grab the integer representation of the string from the file. However if the file is empty, I need to default to 1.



Is there a way to do something like:



typeset -i my_num=$(cat "$my_file" | default "1")






share|improve this question


























    up vote
    0
    down vote

    favorite












    I have a simple bash function to increment a number and store it in a file:



    get_next_int () tee



    regarding this line:



    echo "$((++my_num))" > "$my_file" | tee


    out of curiousity, is there a way to overwrite the file with the incremented integer and echo it to stdout?



    this answer about tee doesn't seem to cover my case.



    How to redirect output to a file and stdout | Stack Overflow



    also, as an aside, regarding this line:



    typeset -i my_num=$(cat "$my_file")


    that will grab the integer representation of the string from the file. However if the file is empty, I need to default to 1.



    Is there a way to do something like:



    typeset -i my_num=$(cat "$my_file" | default "1")






    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a simple bash function to increment a number and store it in a file:



      get_next_int () tee



      regarding this line:



      echo "$((++my_num))" > "$my_file" | tee


      out of curiousity, is there a way to overwrite the file with the incremented integer and echo it to stdout?



      this answer about tee doesn't seem to cover my case.



      How to redirect output to a file and stdout | Stack Overflow



      also, as an aside, regarding this line:



      typeset -i my_num=$(cat "$my_file")


      that will grab the integer representation of the string from the file. However if the file is empty, I need to default to 1.



      Is there a way to do something like:



      typeset -i my_num=$(cat "$my_file" | default "1")






      share|improve this question














      I have a simple bash function to increment a number and store it in a file:



      get_next_int () tee



      regarding this line:



      echo "$((++my_num))" > "$my_file" | tee


      out of curiousity, is there a way to overwrite the file with the incremented integer and echo it to stdout?



      this answer about tee doesn't seem to cover my case.



      How to redirect output to a file and stdout | Stack Overflow



      also, as an aside, regarding this line:



      typeset -i my_num=$(cat "$my_file")


      that will grab the integer representation of the string from the file. However if the file is empty, I need to default to 1.



      Is there a way to do something like:



      typeset -i my_num=$(cat "$my_file" | default "1")








      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 11 at 22:38









      Drakonoved

      674518




      674518










      asked Apr 11 at 18:36









      Alexander Mills

      1,885929




      1,885929




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          echo "$(( ++my_num ))" | tee "$my_file"


          Tee writes te the named file(s) and to standard output by default.



          After typeset -i my_num=$(cat "$my_file") (which could be typeset -i my_num=$(<"$my_file") in bash), you could use



          my_num=$(( my_num == 0 ? 1 : my_num ))


          to set it to 1 if it's value is zero. Integer variables in bash evaluate to zero if empty.



          I was going to suggest my_num=$my_num:-1 but since my_num is an integer variable, it will expand to zero if it's empty, not to the empty string, which means that the $variable:-value parameter substitution would not work (this would otherwise expand to value if the variable was unset or empty).




          I would probably have written the function as



          get_next_int () tee "$HOME/next_int.json"



          assuming "$HOME/next_int.json" always contained a single integer. I've left out the touch because the files modification timestamp would be updated by tee (keep touch if you need the access timestamp updated as well).






          share|improve this answer






















          • does that overwrite the file?
            – Alexander Mills
            Apr 11 at 18:41










          • I assume so, and thank you, also if you have thoughts about the aside question, that helps
            – Alexander Mills
            Apr 11 at 18:42






          • 1




            @AlexanderMills It does, unless tee -a is used for appending.
            – Kusalananda
            Apr 11 at 18:42










          • got it, any idea how to use a default string if cat comes up with empty stdout?
            – Alexander Mills
            Apr 11 at 18:43











          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          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: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f437114%2foverwrite-file-and-echo-to-stdout%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          echo "$(( ++my_num ))" | tee "$my_file"


          Tee writes te the named file(s) and to standard output by default.



          After typeset -i my_num=$(cat "$my_file") (which could be typeset -i my_num=$(<"$my_file") in bash), you could use



          my_num=$(( my_num == 0 ? 1 : my_num ))


          to set it to 1 if it's value is zero. Integer variables in bash evaluate to zero if empty.



          I was going to suggest my_num=$my_num:-1 but since my_num is an integer variable, it will expand to zero if it's empty, not to the empty string, which means that the $variable:-value parameter substitution would not work (this would otherwise expand to value if the variable was unset or empty).




          I would probably have written the function as



          get_next_int () tee "$HOME/next_int.json"



          assuming "$HOME/next_int.json" always contained a single integer. I've left out the touch because the files modification timestamp would be updated by tee (keep touch if you need the access timestamp updated as well).






          share|improve this answer






















          • does that overwrite the file?
            – Alexander Mills
            Apr 11 at 18:41










          • I assume so, and thank you, also if you have thoughts about the aside question, that helps
            – Alexander Mills
            Apr 11 at 18:42






          • 1




            @AlexanderMills It does, unless tee -a is used for appending.
            – Kusalananda
            Apr 11 at 18:42










          • got it, any idea how to use a default string if cat comes up with empty stdout?
            – Alexander Mills
            Apr 11 at 18:43















          up vote
          3
          down vote



          accepted










          echo "$(( ++my_num ))" | tee "$my_file"


          Tee writes te the named file(s) and to standard output by default.



          After typeset -i my_num=$(cat "$my_file") (which could be typeset -i my_num=$(<"$my_file") in bash), you could use



          my_num=$(( my_num == 0 ? 1 : my_num ))


          to set it to 1 if it's value is zero. Integer variables in bash evaluate to zero if empty.



          I was going to suggest my_num=$my_num:-1 but since my_num is an integer variable, it will expand to zero if it's empty, not to the empty string, which means that the $variable:-value parameter substitution would not work (this would otherwise expand to value if the variable was unset or empty).




          I would probably have written the function as



          get_next_int () tee "$HOME/next_int.json"



          assuming "$HOME/next_int.json" always contained a single integer. I've left out the touch because the files modification timestamp would be updated by tee (keep touch if you need the access timestamp updated as well).






          share|improve this answer






















          • does that overwrite the file?
            – Alexander Mills
            Apr 11 at 18:41










          • I assume so, and thank you, also if you have thoughts about the aside question, that helps
            – Alexander Mills
            Apr 11 at 18:42






          • 1




            @AlexanderMills It does, unless tee -a is used for appending.
            – Kusalananda
            Apr 11 at 18:42










          • got it, any idea how to use a default string if cat comes up with empty stdout?
            – Alexander Mills
            Apr 11 at 18:43













          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          echo "$(( ++my_num ))" | tee "$my_file"


          Tee writes te the named file(s) and to standard output by default.



          After typeset -i my_num=$(cat "$my_file") (which could be typeset -i my_num=$(<"$my_file") in bash), you could use



          my_num=$(( my_num == 0 ? 1 : my_num ))


          to set it to 1 if it's value is zero. Integer variables in bash evaluate to zero if empty.



          I was going to suggest my_num=$my_num:-1 but since my_num is an integer variable, it will expand to zero if it's empty, not to the empty string, which means that the $variable:-value parameter substitution would not work (this would otherwise expand to value if the variable was unset or empty).




          I would probably have written the function as



          get_next_int () tee "$HOME/next_int.json"



          assuming "$HOME/next_int.json" always contained a single integer. I've left out the touch because the files modification timestamp would be updated by tee (keep touch if you need the access timestamp updated as well).






          share|improve this answer














          echo "$(( ++my_num ))" | tee "$my_file"


          Tee writes te the named file(s) and to standard output by default.



          After typeset -i my_num=$(cat "$my_file") (which could be typeset -i my_num=$(<"$my_file") in bash), you could use



          my_num=$(( my_num == 0 ? 1 : my_num ))


          to set it to 1 if it's value is zero. Integer variables in bash evaluate to zero if empty.



          I was going to suggest my_num=$my_num:-1 but since my_num is an integer variable, it will expand to zero if it's empty, not to the empty string, which means that the $variable:-value parameter substitution would not work (this would otherwise expand to value if the variable was unset or empty).




          I would probably have written the function as



          get_next_int () tee "$HOME/next_int.json"



          assuming "$HOME/next_int.json" always contained a single integer. I've left out the touch because the files modification timestamp would be updated by tee (keep touch if you need the access timestamp updated as well).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 11 at 18:58

























          answered Apr 11 at 18:41









          Kusalananda

          102k13199316




          102k13199316











          • does that overwrite the file?
            – Alexander Mills
            Apr 11 at 18:41










          • I assume so, and thank you, also if you have thoughts about the aside question, that helps
            – Alexander Mills
            Apr 11 at 18:42






          • 1




            @AlexanderMills It does, unless tee -a is used for appending.
            – Kusalananda
            Apr 11 at 18:42










          • got it, any idea how to use a default string if cat comes up with empty stdout?
            – Alexander Mills
            Apr 11 at 18:43

















          • does that overwrite the file?
            – Alexander Mills
            Apr 11 at 18:41










          • I assume so, and thank you, also if you have thoughts about the aside question, that helps
            – Alexander Mills
            Apr 11 at 18:42






          • 1




            @AlexanderMills It does, unless tee -a is used for appending.
            – Kusalananda
            Apr 11 at 18:42










          • got it, any idea how to use a default string if cat comes up with empty stdout?
            – Alexander Mills
            Apr 11 at 18:43
















          does that overwrite the file?
          – Alexander Mills
          Apr 11 at 18:41




          does that overwrite the file?
          – Alexander Mills
          Apr 11 at 18:41












          I assume so, and thank you, also if you have thoughts about the aside question, that helps
          – Alexander Mills
          Apr 11 at 18:42




          I assume so, and thank you, also if you have thoughts about the aside question, that helps
          – Alexander Mills
          Apr 11 at 18:42




          1




          1




          @AlexanderMills It does, unless tee -a is used for appending.
          – Kusalananda
          Apr 11 at 18:42




          @AlexanderMills It does, unless tee -a is used for appending.
          – Kusalananda
          Apr 11 at 18:42












          got it, any idea how to use a default string if cat comes up with empty stdout?
          – Alexander Mills
          Apr 11 at 18:43





          got it, any idea how to use a default string if cat comes up with empty stdout?
          – Alexander Mills
          Apr 11 at 18:43













           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f437114%2foverwrite-file-and-echo-to-stdout%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)