{"id":53,"date":"2011-09-09T10:16:22","date_gmt":"2011-09-09T10:16:22","guid":{"rendered":"http:\/\/wiki.davelevy.info\/?p=53"},"modified":"2025-12-28T11:52:08","modified_gmt":"2025-12-28T11:52:08","slug":"shell-programming","status":"publish","type":"post","link":"https:\/\/davelevy.info\/wiki\/shell-programming\/","title":{"rendered":"Shell Programming"},"content":{"rendered":"\n<p>At some point, I wrote. &#8220;This page never seems to age, &#8230;&#8221; it just contains some idioms I like to use in my .login and\/or .bashrc files. I don&#8217;t do this so often as when I started the page but the MAC keeps it in the house, and I have installed cygwin on nearly all my windows systems. However, Microsoft implemented bash on windows 10 and so things finally changed, although my main use of shell is on virtual machines and my web sites. &#8230;<\/p>\n\n\n\n<!--more-->\n\n\n\n<h3 class=\"wp-block-heading\">C:<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">cd \/mnt\/c<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Aliases<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">alias whence=\"type -p \"\nexport PS1=\"B$ \"<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Paths<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">contains()\n{\n# $1 must be set and grep must be in the PATH\n    echo $1 | grep $2 &gt; \/dev\/null\n    code=$?\n    return $code\n}\n\nfor directory in \/usr\/local\/bin \/usr\/java\/bin\ndo\ncontains $PATH $directory\nif [ $? ! = 0 ]\nthen\n    PATH=$directory:$PATH\nfi\ndone<\/pre>\n\n\n\n<p>Fashions change<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for directory in \/usr\/local\/bin \/usr\/java\/bin;do\n    contains $PATH $directory\n    [ $? ! = 0 ] &amp;&amp; PATH=$directory:$PATH\ndone<\/pre>\n\n\n\n<p>This can be done for any PATH like variable such as MANPATH &amp; LD_LIBRARY_PATH. The code above doesn&#8217;t test for an empty variable because it is manipulating the $PATH, which is unlikely to be unset. Here is some code that performs the test. NB If we want to add to the MANPATH, we need to capture the default values, or do with the Cobalt Linux, since ti treats the man search path differently depending upon if $MANPATH is set or not.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if [ -z $MANPATH ]\nthen\n# if MANPATH is unset get it\n    MANPATH=`cat \/etc\/man.config | \\\n    awk '$1 == path {print $2;}' path=MANPATH | \\\n    tr '\\n' \":\"\nfi\n\ncontains $MANPATH ${POSTGRES_HOME}\/man\nif [ $? != 0 ]\nthen\n    MANPATH=${POSTGRES_HOME}\/man\nfi\nexport MANPATH<\/pre>\n\n\n\n<p>I should check that BASH implements the -z test in the same way as the Korn shell.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ksh<\/h3>\n\n\n\n<p>If I am going to use it<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ENV=${HOME}\/.kshrc\nEDITOR=vi<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">File Scanner<\/h3>\n\n\n\n<p>A file scanner<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/usr\/bin\/ksh<br>typeset -u input<br>while read  input<br>do<br>     print \"$input\"<br>     # if you like to add line to some file, then<br>     # print some &gt;&gt; somefile<br>     # single &gt; is overwrite = file include only the lastline<br>done<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Strings and scripts<\/h3>\n\n\n\n<p>I need to find the reference I used to solve this, as it&#8217;s quite difficult to pass quote delimited strings to programs in a script. Here is my example code, &#8230;<\/p>\n\n\n\n<p>m1=&#8217;&#8211;text=&#8221;Updates available, please run apt&#8221;&#8216;<br>t1=&#8217;&#8211;title=&#8221;Packages available&#8221;&#8216;<br>i1=&#8217;&#8211;info&#8217;; w1=&#8217;&#8211;width=200&#8242;; to=&#8217;&#8211;timeout=20&#8242;<br>args=&#8221;&#8216;${i1}&#8217; &#8216;${t1}&#8217; &#8216;${m1}&#8217; &#8216;${w1}&#8217; &#8216;${to}'&#8221;<br>eval &#8220;zenity $args&#8221;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Links<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"http:\/\/www.gnu.org\/s\/bash\/manual\/html_node\/Arrays.html\">gnu.org on Arrays<\/a>, bash now does arrays<\/li>\n\n\n\n<li><a href=\"http:\/\/linux.die.net\/man\/3\/basename\">man basename at linux.die.org<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/tldp.org\/LDP\/abs\/html\/string-manipulation.html\">String Manipulation<\/a>, I always forget this<\/li>\n\n\n\n<li>How to <a href=\"https:\/\/stackoverflow.com\/questions\/4037170\/force-tee-to-run-for-every-command-in-a-shell-script\">group commands into a pipe<\/a> using {}<\/li>\n\n\n\n<li>On <a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/define-function\/\">functions<\/a>, I forgot.<\/li>\n<\/ul>\n\n\n\n<p>This was originally posted to davelevy.dyndns.info in 2010.<\/p>\n\n\n\n<p>Much of this is now on github in <a href=\"https:\/\/github.com\/dfl1955\/ubuntu-tools\">my ubuntu-tools repo<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At some point, I wrote. &#8220;This page never seems to age, &#8230;&#8221; it just contains some idioms I like to use in my .login and\/or .bashrc files. I don&#8217;t do this so often as when I started the page but the MAC keeps it in the house, and I have installed cygwin on nearly all&#8230;<\/p>\n","protected":false},"author":1,"featured_media":9872,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","_share_on_mastodon":"0"},"categories":[27,3],"tags":[34,35,916,36,920,911,40],"class_list":["post-53","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","category-technology","tag-bash","tag-ksh","tag-linux","tag-programming","tag-software","tag-technology","tag-unix"],"share_on_mastodon":{"url":"","error":""},"jetpack_featured_media_url":"https:\/\/davelevy.info\/wiki\/wp-content\/uploads\/2011\/09\/nautilus-giuliamay-unsplash-w1080-cropped.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/posts\/53","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/comments?post=53"}],"version-history":[{"count":19,"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/posts\/53\/revisions"}],"predecessor-version":[{"id":13665,"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/posts\/53\/revisions\/13665"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/media\/9872"}],"wp:attachment":[{"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/media?parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/categories?post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/tags?post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}