Is it OK to overwrite a file created by mktemp?

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











up vote
1
down vote

favorite












I'd like to copy a file to a temporary location.



I'd like to make sure I'm not overwriting anything important, and that I know the location of the file while the script is running.



#!/bin/bash

myfile="$(mktemp)"
cp "source" "$myfile"

# work with $myfile

rm "$myfile"


Does this seem OK? Is there anything I'm missing? (Permissions, etc.?)

I have a bad feeling about overwriting that file.










share|improve this question

























    up vote
    1
    down vote

    favorite












    I'd like to copy a file to a temporary location.



    I'd like to make sure I'm not overwriting anything important, and that I know the location of the file while the script is running.



    #!/bin/bash

    myfile="$(mktemp)"
    cp "source" "$myfile"

    # work with $myfile

    rm "$myfile"


    Does this seem OK? Is there anything I'm missing? (Permissions, etc.?)

    I have a bad feeling about overwriting that file.










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'd like to copy a file to a temporary location.



      I'd like to make sure I'm not overwriting anything important, and that I know the location of the file while the script is running.



      #!/bin/bash

      myfile="$(mktemp)"
      cp "source" "$myfile"

      # work with $myfile

      rm "$myfile"


      Does this seem OK? Is there anything I'm missing? (Permissions, etc.?)

      I have a bad feeling about overwriting that file.










      share|improve this question













      I'd like to copy a file to a temporary location.



      I'd like to make sure I'm not overwriting anything important, and that I know the location of the file while the script is running.



      #!/bin/bash

      myfile="$(mktemp)"
      cp "source" "$myfile"

      # work with $myfile

      rm "$myfile"


      Does this seem OK? Is there anything I'm missing? (Permissions, etc.?)

      I have a bad feeling about overwriting that file.







      mktemp






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 23 at 21:01









      user

      1021111




      1021111




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          From the man page




          Create a temporary file or directory, safely, and print its name.




          You could add a check for whether mktemp was successful.



          myfile="$(mktemp)"
          if test $? != 0; then
          exit 1
          fi


          If mktemp succeeds, it has created a file that was not present before. You can safely overwrite that file, that is the purpose for using mktemp. The permissions are set to u+rw, as mentioned in the manual.






          share|improve this answer




















            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%2f464506%2fis-it-ok-to-overwrite-a-file-created-by-mktemp%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
            4
            down vote



            accepted










            From the man page




            Create a temporary file or directory, safely, and print its name.




            You could add a check for whether mktemp was successful.



            myfile="$(mktemp)"
            if test $? != 0; then
            exit 1
            fi


            If mktemp succeeds, it has created a file that was not present before. You can safely overwrite that file, that is the purpose for using mktemp. The permissions are set to u+rw, as mentioned in the manual.






            share|improve this answer
























              up vote
              4
              down vote



              accepted










              From the man page




              Create a temporary file or directory, safely, and print its name.




              You could add a check for whether mktemp was successful.



              myfile="$(mktemp)"
              if test $? != 0; then
              exit 1
              fi


              If mktemp succeeds, it has created a file that was not present before. You can safely overwrite that file, that is the purpose for using mktemp. The permissions are set to u+rw, as mentioned in the manual.






              share|improve this answer






















                up vote
                4
                down vote



                accepted







                up vote
                4
                down vote



                accepted






                From the man page




                Create a temporary file or directory, safely, and print its name.




                You could add a check for whether mktemp was successful.



                myfile="$(mktemp)"
                if test $? != 0; then
                exit 1
                fi


                If mktemp succeeds, it has created a file that was not present before. You can safely overwrite that file, that is the purpose for using mktemp. The permissions are set to u+rw, as mentioned in the manual.






                share|improve this answer












                From the man page




                Create a temporary file or directory, safely, and print its name.




                You could add a check for whether mktemp was successful.



                myfile="$(mktemp)"
                if test $? != 0; then
                exit 1
                fi


                If mktemp succeeds, it has created a file that was not present before. You can safely overwrite that file, that is the purpose for using mktemp. The permissions are set to u+rw, as mentioned in the manual.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 23 at 21:09









                RalfFriedl

                3,7451624




                3,7451624



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f464506%2fis-it-ok-to-overwrite-a-file-created-by-mktemp%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?