{"id":638,"date":"2019-11-13T16:57:06","date_gmt":"2019-11-13T21:57:06","guid":{"rendered":"http:\/\/www.hpux.ws\/?p=638"},"modified":"2019-11-13T17:00:18","modified_gmt":"2019-11-13T22:00:18","slug":"using-file-descriptors-other-than-stdin-stdout-stderr-in-shell-scriptings","status":"publish","type":"post","link":"https:\/\/www.hpux.ws\/?p=638","title":{"rendered":"Using File Descriptors other than stdin\/stdout\/stderr in Shell Scriptings"},"content":{"rendered":"\n<p>For a longish ime, I had to jump through major hoops to script around the issue of my standard input getting clobbered when inside a loop that was iterating over something that coming from stdin.  I don&#8217;t have an exact example that recreates the issue but something like this would generate lots of headaches:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat &lt;some file> | awk '{&lt;some fancy awk-type hingys>}' | \\\n  while read entry; do\n    bla; bla; bla\n    some_command_here_that_would_whack_stdin\n    bla; bla; bla\n  done<\/code><\/pre>\n\n\n\n<p>Sorry I cannot provide an actual snippet of code to recreate the issue but what I would find in these situations is that my loop would end after one iteration (when I knew there should have been a lot more) and I coudn&#8217;t figure out why which made me haz a sad.<\/p>\n\n\n\n<p>Well, I found a way to steer clear of all that by assigning a &lt;some file&gt; to a file descriptor different from stdin.  To wit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>exec 3&lt; \/path\/to\/file\nwhile read entry &lt;&3; do\n   bla; bla; bla\n   some_command_here_that_would_whack_stdin\n   bla; bla; bla\ndone<\/code><\/pre>\n\n\n\n<p>There are no doubt other ways to solve this conundrum but this is the way I have avoided it for quite some time now.  Of course, make sure you do not associate a file with file descriptors 0,1,2 (unless you are quite sure that is what you want to do!).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For a longish ime, I had to jump through major hoops to script around the issue of my standard input getting clobbered when inside a loop that was iterating over something that coming from stdin. I don&#8217;t have an exact example that recreates the issue but something like this would generate lots of headaches: Sorry [&hellip;]<\/p>\n","protected":false},"author":1002497,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"_kadence_starter_templates_imported_post":false,"footnotes":""},"categories":[39],"tags":[],"class_list":["post-638","post","type-post","status-publish","format-standard","hentry","category-scripting"],"_links":{"self":[{"href":"https:\/\/www.hpux.ws\/index.php?rest_route=\/wp\/v2\/posts\/638"}],"collection":[{"href":"https:\/\/www.hpux.ws\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hpux.ws\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hpux.ws\/index.php?rest_route=\/wp\/v2\/users\/1002497"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hpux.ws\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=638"}],"version-history":[{"count":2,"href":"https:\/\/www.hpux.ws\/index.php?rest_route=\/wp\/v2\/posts\/638\/revisions"}],"predecessor-version":[{"id":644,"href":"https:\/\/www.hpux.ws\/index.php?rest_route=\/wp\/v2\/posts\/638\/revisions\/644"}],"wp:attachment":[{"href":"https:\/\/www.hpux.ws\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hpux.ws\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hpux.ws\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}