{"id":744,"date":"2018-08-09T17:01:38","date_gmt":"2018-08-09T15:01:38","guid":{"rendered":"https:\/\/blogs.gentoo.org\/mgorny\/?p=744"},"modified":"2020-09-09T10:04:58","modified_gmt":"2020-09-09T08:04:58","slug":"inlining-path_exists","status":"publish","type":"post","link":"https:\/\/blogs.gentoo.org\/mgorny\/2018\/08\/09\/inlining-path_exists\/","title":{"rendered":"Inlining path_exists"},"content":{"rendered":"<p>The\u00a0<kbd>path_exists<\/kbd> function in\u00a0eutils was meant as\u00a0a\u00a0simple hack to\u00a0check for existence of\u00a0files matching a\u00a0wildcard.  However, it was kinda ugly and\u00a0never became used widely.  At\u00a0this very moment, it is used correctly in\u00a0three\u00a0packages, semi-correctly in\u00a0one package and\u00a0totally misused in\u00a0two\u00a0packages.  Therefore, I think it&#8217;s time to\u00a0replace it with something nicer.<\/p>\n<p><!--more--><\/p>\n<p>The\u00a0replacement snippet is\u00a0rather trivial (from the\u00a0original consumer, eselect-opengl):<\/p>\n<pre>local shopt_saved=$(shopt -p nullglob)\r\nshopt -s nullglob\r\nlocal opengl_dirs=( \"${EROOT%\/}\"\/usr\/lib*\/opengl )\r\n${shopt_saved}\r\n\r\nif [[ -n ${opengl_dirs[@]} ]]; then\r\n\t# ...\r\nfi<\/pre>\n<p>Through using nullglob, you disable the\u00a0old POSIX default of\u00a0leaving the\u00a0wildcard unexpanded when it does not match anything.  Instead, you either simply get an\u00a0empty array or\u00a0a\u00a0list of\u00a0matched files\/directories.  If your code requires at\u00a0least one match, you check for the\u00a0array being empty; if\u00a0it handles empty argument lists just fine (e.g.\u00a0<kbd>for<\/kbd> loops), you can avoid any conditionals.  As\u00a0a\u00a0side effect, you get the\u00a0expanded match in\u00a0an\u00a0array, so you don&#8217;t have to repeat the\u00a0wildcard multiple times.<\/p>\n<p>Also note using shopt directly instead of\u00a0estack.eclass that <a rel='external' href='https:\/\/bugs.gentoo.org\/662586'>is broken and\u00a0does not restore options correctly<\/a>.  You can read more on\u00a0option handling in\u00a0<a rel='external' href='https:\/\/blogs.gentoo.org\/mgorny\/2016\/01\/25\/mangling-shell-options-in-ebuilds\/'>Mangling shell options in\u00a0ebuilds<\/a>.<\/p>\n<h2>Update: one-liner from Ulrich M\u00fcller<\/h2>\n<pre><code>if ( shopt -s failglob; : \/foo\/bar\/baz* ) 2&gt;\/dev\/null; then\r\n    # ...\r\nfi<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The\u00a0path_exists function in\u00a0eutils was meant as\u00a0a\u00a0simple hack to\u00a0check for existence of\u00a0files matching a\u00a0wildcard. However, it was kinda ugly and\u00a0never became used widely. At\u00a0this very moment, it is used correctly in\u00a0three\u00a0packages, semi-correctly in\u00a0one package and\u00a0totally misused in\u00a0two\u00a0packages. Therefore, I think it&#8217;s time to\u00a0replace it with something nicer.<\/p>\n","protected":false},"author":137,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[11],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/posts\/744"}],"collection":[{"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/users\/137"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/comments?post=744"}],"version-history":[{"count":7,"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/posts\/744\/revisions"}],"predecessor-version":[{"id":1190,"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/posts\/744\/revisions\/1190"}],"wp:attachment":[{"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/media?parent=744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/categories?post=744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/tags?post=744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}