{"id":1223,"date":"2010-02-13T02:23:31","date_gmt":"2010-02-12T20:53:31","guid":{"rendered":"https:\/\/www.suramya.com\/blog\/?p=1223"},"modified":"2010-02-24T22:59:55","modified_gmt":"2010-02-24T17:29:55","slug":"determine-if-shell-input-is-coming-from-the-terminal-or-from-a-pipe","status":"publish","type":"post","link":"https:\/\/www.suramya.com\/blog\/2010\/02\/determine-if-shell-input-is-coming-from-the-terminal-or-from-a-pipe\/","title":{"rendered":"Determine If Shell Input is Coming From the Terminal or From a Pipe"},"content":{"rendered":"<p>Figuring out if the input to a script is coming from the terminal or from a pipe is not something that I have ever had to use but I found the possibility very interesting so sharing it over here:<\/p>\n<pre class='code'>#!\/bin\/bash\r\n\r\nstdin=\"$(ls -l \/dev\/fd\/0)\"\r\nstdin=\"${stdin\/*-> \/}\"\r\nftype=\"$(stat --printf=%F $stdin)\"\r\n\r\nif   [[ \"$ftype\" == 'character special file' ]]; then \r\n\techo Terminal\r\nelif [[ \"$ftype\" == 'regular file' ]]; then \r\n\techo Pipe: $stdin\r\nelse\r\n\techo Unknown: $stdin\r\nfi\r\n<\/pre>\n<p>&#8211; Suramya<\/p>\n<p>Source: <a href='http:\/\/www.linuxjournal.com\/content\/determine-if-shell-input-coming-terminal-or-pipe'>Linux Journal<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Figuring out if the input to a script is coming from the terminal or from a pipe is not something that I have ever had to use but I found the possibility very interesting so sharing it over here: #!\/bin\/bash stdin=&#8221;$(ls -l \/dev\/fd\/0)&#8221; stdin=&#8221;${stdin\/*-> \/}&#8221; ftype=&#8221;$(stat &#8211;printf=%F $stdin)&#8221; if [[ &#8220;$ftype&#8221; == &#8216;character special file&#8217; [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":3,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":""},"categories":[24,13],"tags":[],"class_list":["post-1223","post","type-post","status-publish","format-standard","hentry","category-knowledgebase","category-linuxunix-related"],"_links":{"self":[{"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/posts\/1223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/comments?post=1223"}],"version-history":[{"count":4,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/posts\/1223\/revisions"}],"predecessor-version":[{"id":1251,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/posts\/1223\/revisions\/1251"}],"wp:attachment":[{"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/media?parent=1223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/categories?post=1223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/tags?post=1223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}