{"id":3401,"date":"2014-07-03T17:52:57","date_gmt":"2014-07-03T17:52:57","guid":{"rendered":"http:\/\/really.zonky.org\/?p=3401"},"modified":"2014-07-03T17:52:57","modified_gmt":"2014-07-03T17:52:57","slug":"zsh-graceful-degredation","status":"publish","type":"post","link":"https:\/\/really.zonky.org\/?p=3401","title":{"rendered":"ZSH: Graceful Degredation"},"content":{"rendered":"<p>It is nice to make a shell environment more pleasant to use in many ways, but it is also helpful to ensure that the process degrades gracefully \u2026<\/p>\n<p>For example, I have a section in my\u00a0<em>.zshrc<\/em> which creates an\u00a0<em>ls<\/em> alias to use human-readable values, and to colourise the output :-<\/p>\n<pre>\r\nls --color=auto > \/dev\/null 2>&1\r\n#       Just collect the exit status ...\r\nif [ \"$?\" = 0 ]\r\nthen\r\n  # If there is no error then use the --color option\r\n  alias ls='ls -h --color=auto'\r\nelse\r\n  alias ls='ls -h'\r\nfi\r\n<\/pre>\n<p>Thus when logging into a system that has an <em>ls<\/em> command that lacks the <em>&#8211;color<\/em> option, the alias will not create an <em>ls<\/em> command that immediately fails. Now whilst such systems are getting rather more rare than in the past, this graceful degradation is still useful as a principle. Whether creating shell aliases, or more generally.<\/p>\n<p>As another example, I have a shell alias (<em>page<\/em> or also <em>pg<\/em>) that I use to invoke a &#8220;pager&#8221; like <em>more<\/em>, <em>less<\/em>, <em>pg<\/em>, or my preferred choice, <em>most<\/em>. The relevant section within the <em>zshrc<\/em> file is :-<\/p>\n<pre>\r\nfor candidate in more less most\r\ndo\r\n  p=$(which $candidate)\r\n  if [ \"$?\" = \"0\" ]\r\n  then\r\n    alias pg=$candidate\r\n    alias page=$candidate\r\n    PAGER=$candidate\r\n  fi\r\ndone\r\n<\/pre>\n<p>This repeatedly sets up the two aliases (and sets the PAGER environment variable) <em>if<\/em> the candidate pager is available; otherwise the aliases are left alone. In other words, this works through a list of candidates in order from most available to least available (but preferred) to select one. Once used to using <em>page<\/em> as a command, I no longer need to worry about if <em>most<\/em> is installed on a system.<\/p>\n<p>A very similar loop is used to generate an alias called <em>vim<\/em> which will always work (at least when <em>vi<\/em> is available).<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is nice to make a shell environment more pleasant to use in many ways, but it is also helpful to ensure that the process degrades gracefully \u2026 For example, I have a section in my\u00a0.zshrc which creates an\u00a0ls alias to use human-readable values, and to colourise the output :- ls &#8211;color=auto > \/dev\/null 2>&#038;1 <a href='https:\/\/really.zonky.org\/?p=3401' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_share_on_mastodon":"0"},"categories":[4],"tags":[1222,1223,1221,1212],"class_list":["post-3401","post","type-post","status-publish","format-standard","hentry","category-it","tag-less","tag-ls","tag-most","tag-zsh","category-4-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"share_on_mastodon":{"url":"","error":""},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1f2KI-SR","_links":{"self":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/3401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3401"}],"version-history":[{"count":2,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/3401\/revisions"}],"predecessor-version":[{"id":3403,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/3401\/revisions\/3403"}],"wp:attachment":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}