{"id":6,"date":"2010-06-04T21:34:23","date_gmt":"2010-06-04T21:34:23","guid":{"rendered":"http:\/\/jowisoftware.de\/wp\/?p=6"},"modified":"2014-12-30T15:00:09","modified_gmt":"2014-12-30T14:00:09","slug":"take-your-bash_aliases-with-you","status":"publish","type":"post","link":"https:\/\/jowisoftware.de\/wp\/2010\/06\/take-your-bash_aliases-with-you\/","title":{"rendered":"Take your bash_aliases with you"},"content":{"rendered":"<p>I love aliases. There are many useful shortcuts, e.g. &#8222;..&#8220; as an alias for &#8222;cd ..&#8220;. If I got shell access to a system, I first install my version of vimrc, bashrc, bash_alias, and so on. I keep those files in a subversion repository, so I can synchronize them.<\/p>\n<p>But what to do, when aliases are &#8222;system-sensitive&#8220;?<\/p>\n<p><!--more--><\/p>\n<p>Some commands only work when required programs are installed or if a special directory exists. Most of the times, this is not a problem: if the program is not installed, the alias simply does not work. But what, if you want to install &#8222;colormake&#8220; as &#8222;make&#8220;. Okay, this seems to be a bad idea, but I think, you understand the point <img decoding=\"async\" src=\"\/blog\/smileys\/wink_smile.gif\" alt=\"\" \/>.<\/p>\n<p>A workaround would be to test weather a desired program is installed before installing an alias. This worked nicely. Until I got access to a Router and a very slow PC: the login needed 5 seconds. I decided to write a simple <abbr title=\"domain specific language\">DSL<\/abbr>, which creates my bash_alias based on a series of checks. The resulting file is than static. Here is the first part of the script:<\/p>\n<pre class=\"lang:sh decode:true \">#!\/bin\/sh\n \ndebug=0\n#######\n \nskip=1\nsupported=1\n \ndebug() {\n    [ $debug -ne 0 ] &amp;&amp; echo $* &gt;&amp;2\n}\n \nwhile read -r line; do\n    debug -n \"\\\"$line\\\" -&gt; \"\n    if [ $skip -eq 0 ]; then\n        if [ \"${line#::#}\" != \"$line\" ]; then\n            debug \"comment skipped\"\n        elif [ \"${line#::}\" != \"$line\" ]; then\n            debug -n \"control command: \"\n            case $line in\n                ::c)\n                    supported=1\n                    debug \"supported cleared\"\n                    ;;\n                ::e:*)\n                    cmd=${line#::e:}\n                    if [ \"$supported\" -eq 0 ]; then\n                        debug \"section already skipped\"\n                    else\n                        which $cmd &gt;\/dev\/null 2&gt;&amp;1\n                        if [ $? -eq 0 ]; then\n                            debug \"file exists\" &gt;&amp;2\n                        else\n                            supported=0\n                            debug \"file does not exist. section skipped.\"\n                        fi\n                    fi\n                    ;;\n                ::p:*)\n                    cmd=${line#::p:}\n                    if [ $supported -eq 0 ]; then\n                        debug \"section already skipped\"\n                    else\n                        perl -e \"eval 'use $cmd'; exit (length \\$@ == 0 ? 0 : 1);\" &gt;\/dev\/null 2&gt;&amp;1\n                        if [ $? -eq 0 ]; then\n                            debug \"perl module exists\"\n                        else\n                            supported=0\n                            debug \"perl module does not exist. section skipped.\"\n                        fi\n                    fi\n                    ;;\n                ::d:*)\n                    cmd=${line#::d:}\n                    if [ $supported -eq 0 ]; then\n                        debug \"section already skipped\"\n                    else\n                        if [ -d \"$cmd\" ]; then\n                            debug \"directory exists\"\n                        else\n                            supported=0\n                            debug \"directory does not exist. section skipped.\"\n                        fi\n                    fi\n                    ;;\n \n                ::r:*)\n                    cmd=${line#::r:}\n                    if [ $supported -eq 0 ]; then\n                        debug \"section already skipped\"\n                    else\n                        if [ -r \"$cmd\" ]; then\n                            debug \"readable\"\n                        else\n                            supported=0\n                            debug \"not readable. section skipped.\"\n                        fi\n                    fi\n                    ;;\n                ::n)\n                    debug \"inverting skip status\"\n                    if [ $supported -eq 0]; then\n                        supported=1\n                    else\n                        supported=0\n                    fi\n                    ;;\n                *)\n                    echo \"unsupported control command: $line\" &gt;&amp;2\n            esac\n        else\n            if [ $supported -eq 1 ]; then\n                \/bin\/echo \"$line\"\n                debug \"supported\"\n            else\n                debug \"not supported\"\n            fi\n        fi\n    else\n        if [ \"$line\" = \"###END###\" ]; then\n            skip=0\n            debug \"skippping disabled\"\n        else\n            debug \"skipped\"\n        fi\n    fi\ndone &lt; $0\n \nexit 0\n###END###\n#!\/bin\/bash\n#WARNING - THIS FILE IS GENERATED AUTOMATICALLY<\/pre>\n<p>The file opens itself, seeks &#8222;###END###&#8220; and processes everything after it. It defines several commands:<\/p>\n<ul>\n<li>::e:myfile &#8211; skip lines until next ::c if myfile does not exist<\/li>\n<li>::d:mydir &#8211; skip lines until next ::c if directory mydir does not exist<\/li>\n<li>::r:myfile &#8211; skip lines until next ::c if file myfile is not readable<\/li>\n<li>::n &#8211; inverts skipping state (imagine this as &#8222;else)<\/li>\n<li>::p:module &#8211; skip lines until next ::c if the perl module module is not installed<\/li>\n<li>::c &#8211; clear skip state<\/li>\n<\/ul>\n<p>This is freaky to read, but simple to use and it can be easily extended. Note the explicit call of <tt>\/bin\/echo<\/tt> in line 97. This was the only way I found to get this script working witch both dash and bash (their &#8222;echo&#8220; words differently if they find a backslash).<\/p>\n<p>Okay, last but not least some examples for the second part (most parts are collected from several websites):<\/p>\n<pre class=\"lang:sh decode:true\">alias ..='cd ..'\nalias ...='cd ..\/..'\nalias 2..='cd ..\/..'\nalias 3..='cd ..\/..\/..'\nalias 4..='cd ..\/..\/..\/..'\nalias 5..='cd ..\/..\/..\/..\/..'\n \nalias path='echo -e ${PATH\/\/:\/\\\\n}'\n \nalias ls='ls $LS_OPTIONS'\nalias l='ls $LS_OPTIONS -l'\nalias ll='ls $LS_OPTIONS -lA'\nalias lg='ls | grep -i'\nalias llg='ll |grep -i'\n \nalias grep='grep --color=auto'\n \nalias rm='rm -i'\nalias cp='cp -i'\nalias mv='mv -i'\n \nalias p='ps aux'\nalias psg='ps aux | grep'\n \n::e:tree\nalias tree='tree -Csu'\n::c\n \n::e:screen\n# starts screen in the background\nalias bgscreen='screen -d -m'\n::c\n \n::e:ccze\nalias c='ccze -m ansi'\n::e:less\nalias lless='c |less -NiR' # cat ... | lless\n::c\n \n::e:colordiff\nalias diff=colordiff\n::c\n \n::p:Smart::Comments\nalias cperl=\"perl -MSmart::Comments\"\n::c<\/pre>\n<p>To create a customized bash_aliases just run shomething like &#8222;<tt>sh mkalias.sh &gt; ~\/.bash_aliases<\/tt>&#8222;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I love aliases. There are many useful shortcuts, e.g. &#8222;..&#8220; as an alias for &#8222;cd ..&#8220;. If I got shell access to a system, I first install my version of vimrc, bashrc, bash_alias, and so on. I keep those files &#8230; <a class=\"more-link\" href=\"https:\/\/jowisoftware.de\/wp\/2010\/06\/take-your-bash_aliases-with-you\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,4,2],"tags":[],"class_list":["post-6","post","type-post","status-publish","format-standard","hentry","category-6-bash","category-7-english","category-3-linux"],"_links":{"self":[{"href":"https:\/\/jowisoftware.de\/wp\/wp-json\/wp\/v2\/posts\/6","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jowisoftware.de\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jowisoftware.de\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jowisoftware.de\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jowisoftware.de\/wp\/wp-json\/wp\/v2\/comments?post=6"}],"version-history":[{"count":2,"href":"https:\/\/jowisoftware.de\/wp\/wp-json\/wp\/v2\/posts\/6\/revisions"}],"predecessor-version":[{"id":90,"href":"https:\/\/jowisoftware.de\/wp\/wp-json\/wp\/v2\/posts\/6\/revisions\/90"}],"wp:attachment":[{"href":"https:\/\/jowisoftware.de\/wp\/wp-json\/wp\/v2\/media?parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jowisoftware.de\/wp\/wp-json\/wp\/v2\/categories?post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jowisoftware.de\/wp\/wp-json\/wp\/v2\/tags?post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}