{"id":687,"date":"2013-10-19T23:24:25","date_gmt":"2013-10-20T04:24:25","guid":{"rendered":"http:\/\/www.triatlantico.org\/blog\/?p=687"},"modified":"2021-11-20T18:22:40","modified_gmt":"2021-11-20T23:22:40","slug":"update-to-a-useful-bash-functionactualizacao-a-uma-funcao-util-em-bash","status":"publish","type":"post","link":"https:\/\/www.triatlantico.org\/blog\/2013\/10\/19\/update-to-a-useful-bash-functionactualizacao-a-uma-funcao-util-em-bash","title":{"rendered":"Update to a useful bash function|Actualiza\u00e7\u00e3o a uma fun\u00e7\u00e3o util em bash"},"content":{"rendered":"<p>[lang_en]Previously&#8230; <a title=\"Useful bash functions|Fun\u00e7\u00f5es \u00fateis em bash\" href=\"http:\/\/www.triatlantico.org\/blog\/2011\/06\/04\/useful-bash-functions\">Useful bash functions<\/a><\/p>\n<p>This new version is a tad more efficient since there are no more echo and pipes involved on the processing. Just one eval and one awk script.<br \/>\n[\/lang_en][lang_pt]Previamente&#8230; <a title=\"Useful bash functions|Fun\u00e7\u00f5es \u00fateis em bash\" href=\"http:\/\/www.triatlantico.org\/blog\/2011\/06\/04\/useful-bash-functions\">Fun\u00e7\u00f5es \u00fateis em bash<\/a><\/p>\n<p>Esta nova vers\u00e3o \u00e9 ligeiramente mais eficiente, porque n\u00e3o existem mais comandos echo e pipes envolvidos no processamento. Apenas um eval e um script awk.<br \/>\n[\/lang_pt]<br \/>\n<\/a><\/p>\n<pre>\n\n<div class=\"codecolorer-container bash vibrant\" style=\"overflow:auto;white-space:nowrap;width:480px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/><\/div><\/td><td><div class=\"bash codecolorer\">unique_path<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; <span class=\"kw3\">eval<\/span> $<span class=\"br0\">&#40;<\/span><span class=\"kw2\">awk<\/span> <span class=\"re5\">-v<\/span> <span class=\"re2\">VAR<\/span>=<span class=\"st0\">&quot;<span class=\"es3\">${1}<\/span>&quot;<\/span> <span class=\"st_h\">'BEGIN { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \\<br \/>\n&nbsp; &nbsp; if((N=split(ENVIRON[VAR],vals,&quot;:&quot;))&lt;2) exit(0); \\<br \/>\n&nbsp; &nbsp; sep = out=&quot;&quot; ; i=split(out,seen); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \\<br \/>\n&nbsp; &nbsp; do { if(!(vals[++i] in seen)) { seen[vals[i]]; &nbsp;\\<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;gsub(&quot;[&quot;]&quot;,&quot;\\\\&quot;&quot;,vals[i]); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \\<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;out=sprintf(&quot;%s%s%s&quot;,out,sep,vals[i]); &nbsp; \\<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sep=&quot;:&quot;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \\<br \/>\n&nbsp; &nbsp; } } while (i&lt;N); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\\<br \/>\n&nbsp; &nbsp; printf &quot;%s=&quot;%s&quot;; export %s;\\n&quot;,VAR,out,VAR; &nbsp; \\<br \/>\n&nbsp; }'<\/span> <span class=\"sy0\">\/<\/span>dev<span class=\"sy0\">\/<\/span>null<span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n<p>[lang_en]Similarly, these also had remove unnecessary uses of sub-shell echo&#8230;[\/lang_en][lang_pt]Estas fun\u00e7\u00f5es tamb\u00e9m foram melhoradas removendo usos desnecess\u00e1rios de sub-shells para echo[\/lang_pt]<\/p>\n<pre>\n\n<div class=\"codecolorer-container bash vibrant\" style=\"overflow:auto;white-space:nowrap;width:480px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/><\/div><\/td><td><div class=\"bash codecolorer\">safe_prepend_path<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; <span class=\"re2\">VAR<\/span>=<span class=\"re4\">$1<\/span> ; <span class=\"kw3\">shift<\/span> ; <span class=\"kw3\">eval<\/span> <span class=\"re2\">VAL<\/span>=\\$<span class=\"co1\">${VAR}<\/span><br \/>\n&nbsp; <span class=\"kw1\">for<\/span> i_;<span class=\"kw1\">do<\/span> <span class=\"br0\">&#91;<\/span> <span class=\"re5\">-d<\/span> <span class=\"st0\">&quot;<span class=\"es2\">$i_<\/span>&quot;<\/span> <span class=\"br0\">&#93;<\/span> <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"re2\">VAL<\/span>=<span class=\"re1\">$i_<\/span><span class=\"co1\">${VAL:+&quot;:$VAL&quot;}<\/span> ;<span class=\"kw1\">done<\/span><br \/>\n&nbsp; <span class=\"kw3\">eval<\/span> <span class=\"kw3\">export<\/span> <span class=\"co1\">${VAR}<\/span>=<span class=\"co1\">${VAL}<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\nsafe_append_path<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; <span class=\"re2\">VAR<\/span>=<span class=\"re4\">$1<\/span> ; <span class=\"kw3\">shift<\/span> ; <span class=\"kw3\">eval<\/span> <span class=\"re2\">VAL<\/span>=\\$<span class=\"co1\">${VAR}<\/span><br \/>\n&nbsp; <span class=\"kw1\">for<\/span> i_;<span class=\"kw1\">do<\/span> <span class=\"br0\">&#91;<\/span> <span class=\"re5\">-d<\/span> <span class=\"st0\">&quot;<span class=\"es2\">$i_<\/span>&quot;<\/span> <span class=\"br0\">&#93;<\/span> <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"re2\">VAL<\/span>=<span class=\"co1\">${VAL:+&quot;$VAL:&quot;}<\/span><span class=\"re1\">$i_<\/span> ;<span class=\"kw1\">done<\/span><br \/>\n&nbsp; <span class=\"kw3\">eval<\/span> <span class=\"kw3\">export<\/span> <span class=\"co1\">${VAR}<\/span>=<span class=\"co1\">${VAL}<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>[lang_en]Previously&#8230; Useful bash functions This new version is a tad more efficient since there are no more echo and pipes involved on the processing. Just one eval and one awk script. [\/lang_en][lang_pt]Previamente&#8230; Fun\u00e7\u00f5es \u00fateis em bash Esta nova vers\u00e3o \u00e9 ligeiramente mais eficiente, porque n\u00e3o existem mais comandos echo e pipes envolvidos no processamento. Apenas [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,62],"tags":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pmzAM-b5","_links":{"self":[{"href":"https:\/\/www.triatlantico.org\/blog\/wp-json\/wp\/v2\/posts\/687"}],"collection":[{"href":"https:\/\/www.triatlantico.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.triatlantico.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.triatlantico.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.triatlantico.org\/blog\/wp-json\/wp\/v2\/comments?post=687"}],"version-history":[{"count":15,"href":"https:\/\/www.triatlantico.org\/blog\/wp-json\/wp\/v2\/posts\/687\/revisions"}],"predecessor-version":[{"id":699,"href":"https:\/\/www.triatlantico.org\/blog\/wp-json\/wp\/v2\/posts\/687\/revisions\/699"}],"wp:attachment":[{"href":"https:\/\/www.triatlantico.org\/blog\/wp-json\/wp\/v2\/media?parent=687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.triatlantico.org\/blog\/wp-json\/wp\/v2\/categories?post=687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.triatlantico.org\/blog\/wp-json\/wp\/v2\/tags?post=687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}