{"id":5031,"date":"2015-01-06T17:19:11","date_gmt":"2015-01-06T16:19:11","guid":{"rendered":"http:\/\/www.b.shuttle.de\/hayek\/hayek\/jochen\/wp\/blog-en\/?p=5031"},"modified":"2015-01-06T17:19:11","modified_gmt":"2015-01-06T16:19:11","slug":"shell-scripting-for-i-in-or-while-read","status":"publish","type":"post","link":"https:\/\/wp.jochen.hayek.name\/blog-en\/2015\/01\/06\/shell-scripting-for-i-in-or-while-read\/","title":{"rendered":"shell scripting: &#8220;for i in $( \u2026 )&#8221; or &#8220;\u2026 | while read&#8221; ?"},"content":{"rendered":"<ul>\n<li><a href=\"https:\/\/lists.gnu.org\/archive\/html\/bug-coreutils\/2009-09\/msg00291.html\">https:\/\/lists.gnu.org\/archive\/html\/bug-coreutils\/2009-09\/msg00291.html<\/a><\/li>\n<\/ul>\n<p>The link refers to some code like the one I am working on right now:<\/p>\n<pre>$ for i in $(seq -w $n); do \u2026; done<\/pre>\n<p>And the guy replying on that <a href=\"https:\/\/en.wikipedia.org\/wiki\/Mailing_list\">ml<\/a> thread suggests to better write it this way:<\/p>\n<pre>$ seq -w $n\u00a0| while read i; do \u2026; done<\/pre>\n<p>His reason:<\/p>\n<blockquote><p>to prevent all of seq&#8217;s output having to be buffered at once.<\/p><\/blockquote>\n<p>I actually second that, and that reminds me of a rather snobby programmer I met last year. That guy&#8217;s code was like that, and I was close to a heart failure because of the many times I had struggled with code like this during the last 30 years:<\/p>\n<pre>$ for i in $(\/bin\/ls); do \u2026; done<\/pre>\n<p>People even do it with &#8220;<em>find<\/em>&#8220;, and then they wonder, what &#8220;<em>command line too long<\/em>&#8221; would mean.<\/p>\n<p>Why not simply feed it into a &#8220;<em>\u2026 | while read \u2026<\/em>&#8220;?<\/p>\n<p>And if that&#8217;s not good enough, have you come across &#8220;<em>xargs<\/em>&#8221; and esp. &#8220;<em>xargs -0<\/em>&#8220;?<\/p>\n<p><span style=\"line-height: 1.6471\">He said, he had found, that all the <em>read<\/em>-s would get started in their own process (WTF? what a crappy, crappy thought!), and he allowed no doubting at all. T<\/span>he guy (&#8220;JF&#8221;) was the customer&#8217;s guru shell and perl script programmer, so why struggle on this issue? The life-time I had spent there wasn&#8217;t paid bad, but that snobbish programmer guy was of a rather discouraging kind. That cost me quite some productivity in our intersecting area, but then: energies not spent in one area are usually available in other areas, which isn&#8217;t that bad after all.<\/p>\n<p>Having said all that I still like my current code, as I think it reads well and 12 will not really exhaust the command line buffer:<\/p>\n<pre>$ for i in $(seq -w 1 12); do \u2026; done<\/pre>\n<p>I am creating place holder files resp. directories for 2015 this way. This has actually long been in place, but coreutils&#8217; seq is buggy on one of my NAS-s, and I had to find a way around the bug, and I finally resorted to BusyBox&#8217;s seq, as the NAS is operating BusyBox anyway. Now I am calling <em>\/usr\/bin\/seq<\/em> instead of simply calling &#8220;<em>seq<\/em>&#8221; through PATH \u2013 there are worse things than that.\t\t\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/lists.gnu.org\/archive\/html\/bug-coreutils\/2009-09\/msg00291.html The link refers to some code like the one I am working on right now: $ for i in $(seq -w $n); do \u2026; done And the guy replying on that ml thread suggests to better write it this way: $ seq -w $n\u00a0| while read i; do \u2026; done His reason: to prevent [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false,"_share_on_mastodon":"0"},"categories":[666],"tags":[1306,1397],"class_list":["post-5031","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-snob-programmers","tag-wtf"],"share_on_mastodon":{"url":"","error":""},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paO0kP-1j9","jetpack_likes_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/posts\/5031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/comments?post=5031"}],"version-history":[{"count":0,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/posts\/5031\/revisions"}],"wp:attachment":[{"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/media?parent=5031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/categories?post=5031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.jochen.hayek.name\/blog-en\/wp-json\/wp\/v2\/tags?post=5031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}