{"id":2583,"date":"2017-09-11T22:36:35","date_gmt":"2017-09-11T22:36:35","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=2583"},"modified":"2017-09-11T22:36:35","modified_gmt":"2017-09-11T22:36:35","slug":"command-line-fun-temporary-data","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/09\/11\/command-line-fun-temporary-data\/","title":{"rendered":"command line fun &#8211; temporary data"},"content":{"rendered":"<p>Most of the scripts that I need to write don&#8217;t actually need to manipulate a lot of data. \u00a0It is usually enough to pipe that data through a command or two and use the result. \u00a0When this isn&#8217;t acceptable then it is usually a small enough amount of data that I can save it in a temporary file with the same name.<\/p>\n<p>Yet there might be a number of cases where truly unique names are necessary. \u00a0One example might be if the script gets run many times in parallel but still needs to store some temporary data.<\/p>\n<p>There are a few different methods of generating a unique name.<\/p>\n<h2>Process Id<\/h2>\n<p>Each Unix process has a unique id and that can be used as part of the file name. \u00a0There are some special variables that can be accessed and one of them is $$. \u00a0This bash shell variable will be the actual process id of the current shell.<\/p>\n<pre>TEMPFILE=mytempdata.$$<\/pre>\n<p>This actually should be pretty safe for a lot of situations depending on the volume.<\/p>\n<h2>Timestamp<\/h2>\n<p>Another fairly classic method of creating a unique name would be to create a timestamp. \u00a0This is a terrible device for coming up with a unambiguous name unless the script in question is being run in serial.<\/p>\n<pre>TEMPFILE=mytempdata.`date '+%Y%m%d-%H%M%S'`<\/pre>\n<p>This example simply uses the date command to display the current date and time. \u00a0This date and time is redirected using the backtick. \u00a0This can also be done using the more modern method of redirecting the output.<\/p>\n<pre>TEMPFILE=mytempdata.$(date '+%Y%m%d-%H%M%S')<\/pre>\n<h2>Operating system support<\/h2>\n<p>There exists the mktemp command which will create an absolutely unique name based on the names of the files or directories in the destination directory. \u00a0There is one &#8220;little&#8221; side effect of this command and that is that the temporary file or directory is created. The file is a zero length file while the directory is a normal directory.<\/p>\n<p>Using the same technique as in the previous example it is possible to both create a unique name while also capturing the name in a variable.<\/p>\n<pre>TEMPFILE=$(mktemp \/tmp\/mytempdata.XXXX)\r\n\r\nTEMPDIR=$(mktemp -d \/tmp\/mytempdata.XXXX)<\/pre>\n<p>The operating system cannot know how many free files you might need in your directory. The mktemp command will replace the capital letter X with the unique alphanumeric value. \u00a0If more than 999 files might be in use at a time then use four X&#8217;s. \u00a0The exact location of the capital X is not important. \u00a0It can be set as an extension or just a value within the name.<\/p>\n<p id=\"yui_3_16_0_ym19_1_1504812995442_5844\"><span id=\"yui_3_16_0_ym19_1_1504812995442_5873\" lang=\"EN\"><\/span><span lang=\"EN\">\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most of the scripts that I need to write don&#8217;t actually need to manipulate a lot of data. \u00a0It is usually enough to pipe that data through a command or two and use the result. \u00a0When this isn&#8217;t acceptable then &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/09\/11\/command-line-fun-temporary-data\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[17,39],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2583"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=2583"}],"version-history":[{"count":3,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2583\/revisions"}],"predecessor-version":[{"id":2586,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2583\/revisions\/2586"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=2583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=2583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=2583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}