<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>${me:-whatever} &#187; bash</title>
	<atom:link href="http://samrowe.com/wordpress/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://samrowe.com/wordpress</link>
	<description></description>
	<lastBuildDate>Tue, 01 May 2012 14:02:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Advancing in the Bash Shell small update</title>
		<link>http://samrowe.com/wordpress/2010/06/16/advancing-in-the-bash-shell-small-update/</link>
		<comments>http://samrowe.com/wordpress/2010/06/16/advancing-in-the-bash-shell-small-update/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 13:42:14 +0000</pubDate>
		<dc:creator>sam</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://samrowe.com/wordpress/?p=229</guid>
		<description><![CDATA[I updated Advancing in the Bash Shell a bit. A new link at the bottom, some very small wording corrections and a syntax highlighter plugin to make the examples a bit easier (I hope!) to read.]]></description>
			<content:encoded><![CDATA[<p>I updated Advancing in the Bash Shell a bit. A new link at the bottom, some very small wording corrections and a syntax highlighter plugin to make the examples a bit easier (I hope!) to read.</p>
]]></content:encoded>
			<wfw:commentRss>http://samrowe.com/wordpress/2010/06/16/advancing-in-the-bash-shell-small-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Type less, smile more</title>
		<link>http://samrowe.com/wordpress/2008/10/12/type-less-smile-more/</link>
		<comments>http://samrowe.com/wordpress/2008/10/12/type-less-smile-more/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 01:59:07 +0000</pubDate>
		<dc:creator>sam</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://samrowe.com/wordpress/?p=157</guid>
		<description><![CDATA[How many times a week do you type /etc/init.d/blah start and then you realize you&#8217;re on HP-UX and it&#8217;s /sbin/init.d/blah Forget it. # Turn on extended globbing and programmable completion shopt -s extglob progcomp export MYOS=$(uname -s) if [[ "${MYOS}" = "Linux" &#124;&#124; "${MYOS}" = "SunOS" ]] ; then INITDIR='/etc/init.d'; else INITDIR='/sbin/init.d'; fi export INITDIR; [...]]]></description>
			<content:encoded><![CDATA[<p>How many times a week do you type <code>/etc/init.d/blah start</code> and then you realize you&#8217;re on HP-UX and it&#8217;s <code>/sbin/init.d/blah</code> Forget it.</p>
<pre>
# Turn on extended globbing and programmable completion
shopt -s extglob progcomp

export MYOS=$(uname -s)

if [[ "${MYOS}" = "Linux" || "${MYOS}" = "SunOS" ]] ; then
    INITDIR='/etc/init.d';
else
    INITDIR='/sbin/init.d';
fi
export INITDIR;

function RCCT () {.
    if [[ -z "${2}" ]] ; then.
        ls $INITDIR;
    elif [[ "${1}" = "Start" ]] ; then
        cd / &#038;&#038; $INITDIR/${2} start ; cd -;
    elif [[ "${1}" = "Restart" ]] ; then
        cd / &#038;&#038; $INITDIR/${2} restart ; cd -;
    elif [[ "${1}" = "Stop" ]] ; then
        cd / &#038;&#038; $INITDIR/${2} stop ; cd -;
    elif [[ "${1}" = "Reload" ]] ; then
        cd / &#038;&#038; $INITDIR/${2} reload ; cd -;
    else
        echo "Something bad happened.";
    fi
}   

function _myservices() {
    local cur
    cur=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=( $( builtin echo $INITDIR/!(*.rpmsave|*.rpmorig|*.dpkg-old|*~|functions)) )
    COMPREPLY=( $( compgen -W '${COMPREPLY[@]#@($INITDIR)/}' -- $cur) )
}

function Start () { RCCT ${FUNCNAME} ${*} ; };
function Restart () { RCCT ${FUNCNAME} ${*} ; };
function Reload () { RCCT ${FUNCNAME} ${*} ; };
function Stop () { RCCT ${FUNCNAME} ${*} ; };
complete -F _myservices Start Restart Reload Stop
</pre>
<p>You might be thinking that RCCT is pretty ugly and it is, but it&#8217;s complete-able, so it all shakes out. It also starts and stops services with / as the working directory which is a good idea if you&#8217;re dealing with Solaris or HP-UX.</p>
]]></content:encoded>
			<wfw:commentRss>http://samrowe.com/wordpress/2008/10/12/type-less-smile-more/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Be yourself, even when you&#8217;re root</title>
		<link>http://samrowe.com/wordpress/2008/09/01/be-yourself-even-when-youre-root/</link>
		<comments>http://samrowe.com/wordpress/2008/09/01/be-yourself-even-when-youre-root/#comments</comments>
		<pubDate>Mon, 01 Sep 2008 19:15:39 +0000</pubDate>
		<dc:creator>sam</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://samrowe.com/wordpress/?p=124</guid>
		<description><![CDATA[Ever run a command only to realize you&#8217;re not root but need to be? Of course you have. What if that command was long and painful to create? There&#8217;s no reason, Dude, to not have access to your bash history even after becoming root vi &#8216;su&#8217;. function su () { local SUUSER=root local ORIGU=$USER local [...]]]></description>
			<content:encoded><![CDATA[<p>Ever run a command only to realize you&#8217;re not root but need to be? Of course you have. What if that command was long and painful to create? There&#8217;s no reason, Dude, to not have access to your bash history even after becoming root vi &#8216;su&#8217;.</p>
<pre>
function su () {
    local SUUSER=root
    local ORIGU=$USER
    local ORIGG=`groups | awk '{print $1}'`
    if [[ $# -gt 0 ]] ; then
        local char=`echo $1 | cut -c 1`
        if [[ "$char" == '-' ]] ; then
            /bin/su $*
            return $?
        else
            local SUUSER=$1
        fi
    fi
    #append recent history to the history file
    history -a
    /bin/su ${SUUSER} -c "env USER=${SUUSER} HOME=${HOME} ${SHELL}; \
          [ -f ${HOME}/.ICEauthority ] \
          &#038;&#038; chown $ORIGU:$ORIGG ${HOME}/.ICEauthority ${HOME}/.viminfo"
    # Clear the history list by deleting all the entries.
    history -c
    # Read the contents of the history file and use them as the current history.
    history -r
}
</pre>
<p>This function honors the &#8216;su -&#8217; syntax in case you need it. If you have special permissions on your ~/.ICEauthority or ~/.viminfo, you&#8217;ll need to make adjustments obviously. Remember that the backslashes need to be the last characters on the line. They&#8217;re only there to make it more readable, so feel free to ditch them in favor of a longer line.</p>
<p>Now when you &#8216;su&#8217; or even &#8216;su someotheruser&#8217; you&#8217;ll get to keep your own history.</p>
]]></content:encoded>
			<wfw:commentRss>http://samrowe.com/wordpress/2008/09/01/be-yourself-even-when-youre-root/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bash SSH Happiness</title>
		<link>http://samrowe.com/wordpress/2008/07/29/bash-ssh-happiness/</link>
		<comments>http://samrowe.com/wordpress/2008/07/29/bash-ssh-happiness/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 03:07:23 +0000</pubDate>
		<dc:creator>sam</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://samrowe.com/wordpress/?p=81</guid>
		<description><![CDATA[So Dennis? (whose name doesn&#8217;t appear anywhere on his blog except for in the URL) wrote a cool bash script to create aliases for every host in your ~/.ssh/known_hosts file. I found some bugs/problems so I spiffed it up a bit. As the comments say, you should pipe the output to sort(1). I&#8217;ve tried to [...]]]></description>
			<content:encoded><![CDATA[<p>So <a href="http://blogs.ubuntu-nl.org/dennis/2008/07/29/traincoding-ssh-aliases/#comment-94888">Dennis</a>? (whose name doesn&#8217;t appear anywhere on his blog except for in the URL) wrote a cool bash script to create aliases for every host in your ~/.ssh/known_hosts file. I found some bugs/problems so I spiffed it up a bit. As the comments say, you should pipe the output to sort(1). I&#8217;ve tried to make the text as small as possible, but it won&#8217;t all display. I tested selecting it and even the parts that don&#8217;t display end up in my clipboard, so there ya go.</p>
<pre>#!/bin/bash
shopt -s extglob
isint(){
    case $1 in
        ?([-+])+([0-9]) )
            return 0;;
        *) return 1;;
            esac
}

if [[ -d ~/.ssh ]]; then

    # Touch files we expect to exist
    if [[ ! -e ~/.ssh/config ]]; then touch ~/.ssh/config; fi
    if [[ ! -e ~/.ssh/known_hosts ]]; then touch ~/.ssh/known_hosts; fi

    # Parse ~/.ssh/known_hosts and ~/.ssh/config to find hosts
    for x in `sed -e 's/[, ].*//' ~/.ssh/known_hosts; awk '/^Host [^*?]+$/{print $2}' ~/.ssh/config`; do

        # Don’t override commands
        type "${x}" &gt; /dev/null 2&gt;&amp;1 &amp;&amp; continue

        # Remove the domainname
        y=${x%%.*}
        # you don't want IP addresses for aliases, trust me.
        isint $y &amp;&amp; continue

        # If it's a short-name, move on
        #z=${x##*.}
        #[[  "${z}" == 'edu' ||  "${z}" == 'com' || "${z}" == 'net' ]] || continue
        # So the above is commented out because you'd be surprised at how much
        # you rely on your search path. You should pipe the output of this script to
        # sort and your fqdn's will override your shorts.
        echo alias "${x}"=”ssh $x”

        if [[ "$y" != "$x" ]]; then
            if ! type $y &gt; /dev/null 2&gt;&amp;1; then
            echo     alias $y=”ssh $x”
            fi
        fi
    done
fi</pre>
<p>Dennis also mentions that you might want to add <code>HashKnownHosts no</code> to your ~/.ssh/config file. If you have some hashed keys in your file, you should remove them before running this script. Sadly I couldn&#8217;t find any way to programmatically convert a hashed file into a non-hashed file. But to make up for it, here&#8217;s a bonus alias!</p>
<pre># removes _exactly what you type_ from ~/.ssh/known_hosts
# meaning 'grapes' gets you the key for 'grapes' vs 'grapes.wrath.com'
alias forget="ssh-keygen -R"</pre>
]]></content:encoded>
			<wfw:commentRss>http://samrowe.com/wordpress/2008/07/29/bash-ssh-happiness/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bash &amp; Screen</title>
		<link>http://samrowe.com/wordpress/2008/07/15/bash_and_screen/</link>
		<comments>http://samrowe.com/wordpress/2008/07/15/bash_and_screen/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 01:59:00 +0000</pubDate>
		<dc:creator>sam</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[screen]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://samrowe.com/wordpress/?p=1</guid>
		<description><![CDATA[You may be familiar with my tutorial on getting your ssh-agent to work inside screen. If not, have a look. There&#8217;s always room for improvements! Here&#8217;s an excerpt from my current .bash_profile: function Attach(){ grabssh if [[ -z "${1}" ]] ; then local n=`screen -wipe &#124; egrep -i 'attached&#124;detached' &#124; wc -l` if [[ "${n}" [...]]]></description>
			<content:encoded><![CDATA[<p>You may be familiar with my <a title="deadman.org tutorial" href="http://deadman.org/sshscreen.html">tutorial</a> on getting your ssh-agent to work inside screen. If not, have a look.</p>
<p>There&#8217;s always room for improvements! Here&#8217;s an excerpt from my current .bash_profile:</p>
<pre>
function Attach(){
    grabssh
    if [[ -z "${1}" ]] ; then
        local n=`screen -wipe | egrep -i 'attached|detached' | wc -l`
        if [[ "${n}" -gt 1 ]]; then
            check_screen
            return
        fi
    fi
    echo screen -d -R ${*}
    screen -d -R ${*}
}

check_screen () {
  # Look in the path?
    type screen > /dev/null 2>&#038;1
    if [[ ${?} = 0 ]]; then
        tmp=0
        echo
        for scr in `screen -wipe | egrep -i 'attached|detached' | awk '{print $1"_"$2}'`
        do
            echo "Screen available: ${scr}"
            if [[ ${tmp} -eq 0 ]] ; then
                myscreen=${scr%_*}
            fi
            tmp=$(($tmp+1))
        done
        if [[ -n "${myscreen}" ]] ; then
            echo
            echo "Enter to attach to ${myscreen},"
            echo "'n' to move on,"
            echo "unique bits to attach elsewhere"
            read eon
            if [[ -z "${eon}" ]] ; then
                [[ -z "${myscreen}" ]] &#038;&#038; return
                eon=${myscreen}
            fi
            if [[ "${eon}" != 'n' ]] ; then
                Attach ${eon}
            fi
        fi
    fi
}

[[ -n "$PS1" ]] &#038;&#038; check_screen
</pre>
<p>If screen(s) exist, I&#8217;m presented with a list of them on login. I&#8217;m then able to hit return for a default, or type in which of them I wish to resume.</p>
<p>If I don&#8217;t want to resume any screen I can simply hit &#8216;n&#8217; and go on about my day.</p>
<p>The Attach and check_screen functions are actually in a different file that is sourced by .bashrc and conditionally by .bash_login. This allows the functions to be used in non-login shells (in local terminal windows for example).</p>
]]></content:encoded>
			<wfw:commentRss>http://samrowe.com/wordpress/2008/07/15/bash_and_screen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

