<?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>Jtanium's Notebook &#187; Bash</title>
	<atom:link href="http://www.jtanium.com/category/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jtanium.com</link>
	<description>I jot things down, in hopes of finding them later...</description>
	<lastBuildDate>Wed, 21 Dec 2011 23:21:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Bash Prompt for RVM and Git</title>
		<link>http://www.jtanium.com/2011/12/21/bash-prompt-for-rvm-and-git/</link>
		<comments>http://www.jtanium.com/2011/12/21/bash-prompt-for-rvm-and-git/#comments</comments>
		<pubDate>Wed, 21 Dec 2011 23:21:13 +0000</pubDate>
		<dc:creator>jtanium</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[rvm]]></category>

		<guid isPermaLink="false">http://www.jtanium.com/?p=236</guid>
		<description><![CDATA[I&#8217;ve seen a lot of examples where people have a cool Bash prompt showing their Ruby version, RVM gemset, and/or their Git branch. What I couldn&#8217;t find is a complete example that helped me understand what was going on and worked properly on OS X. Here&#8217;s what I did, and maybe you&#8217;ll find it useful. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve seen a lot of examples where people have a cool Bash prompt showing their Ruby version, RVM gemset, and/or their Git branch.</p>
<p>What I couldn&#8217;t find is a complete example that helped me understand what was going on and worked properly on OS X.</p>
<p>Here&#8217;s what I did, and maybe you&#8217;ll find it useful.</p>
<pre>
ESC="\033" # This is the escape sequence
NO_COLOR="$ESC[0m"
IRED="$ESC[1;31m" # ANSI color code for intense/bold red
IGRN="$ESC[1;32m" # ANSI color code for intense/bold green

# From http://railstips.org/blog/archives/2009/02/02/bedazzle-your-bash-prompt-with-git-info/
# I had to change 'git-symbolic-ref' to 'git symbolic-ref'
function parse_git_branch {
  ref=$(git symbolic-ref HEAD 2> /dev/null) || return
  echo " ["${ref#refs/heads/}"]" # I wanted my branch wrapped in [], use () or <> or whatever
}

# from http://ariejan.net/2010/04/25/ruby-version-and-gemset-in-your-bash-prompt-yes-sir
function rvm_version {
  local gemset=$(echo $GEM_HOME | awk -F'@' '{print $2}')
  [ "$gemset" != "" ] &#038;&#038; gemset="@$gemset"
  local version=$(echo $MY_RUBY_HOME | awk -F'-' '{print $2}')
  [ "$version" != "" ] &#038;&#038; version="$version"
  local full="$version$gemset"
  [ "$full" != "" ] &#038;&#038; echo "${full}:" # the colon at the end is a delimiter, you could use a space instead
}

#PS1="\h:\W \u\$" # For reference, here's the default OS X prompt
#export PS1="\$(rvm_version)\W \$(parse_git_branch)\$ " # Without the colors

# I had to put the \[ and \] down here, as opposed to $IRED, to avoid wrapping funkiness.
export PS1="\[$IRED\]\$(rvm_version)\[$NO_COLOR\]\W\[$IGRN\]\$(parse_git_branch)\[$NO_COLOR\] \$ "
</pre>
<p>I encountered all kinds of weirdness getting the colors to work. The problems all centered around getting the <code>\[</code> and <code>\]</code> correct.  If you&#8217;re having problems where the wrapping is wrong, or arrow keys or the backspace causes your prompt to disappear, check your <code>\[\]</code>&#8216;s. The rule of thumb is the characters you&#8217;re escaping should be wrapped in them.  Keep in mind, the color codes don&#8217;t seem to include a <code>]</code> at the end.</p>
<p>I got my color codes from <a href="http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html" title="Bash Prompt HOWTO: Chapter 6. ANSI Escape Sequences: Colours and Cursor Movement" target="_blank">here</a>. At the bottom of that page is a dandy script which prints out all the colors and backgrounds making it easy to find a good color combination for your terminal.</p>
<p>References:<br />
<a href="http://ariejan.net/2010/04/25/ruby-version-and-gemset-in-your-bash-prompt-yes-sir" target="_blank">Ruby version and gemset in your Bash prompt? Yes sir!</a><br />
<a href="http://railstips.org/blog/archives/2009/02/02/bedazzle-your-bash-prompt-with-git-info/" target="_blank">Bedazzle Your Bash Prompt with Git Info</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jtanium.com/2011/12/21/bash-prompt-for-rvm-and-git/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enforcing Executing User on Bash Scripts</title>
		<link>http://www.jtanium.com/2009/11/04/enforcing-executing-user-on-bash-scripts/</link>
		<comments>http://www.jtanium.com/2009/11/04/enforcing-executing-user-on-bash-scripts/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 17:04:01 +0000</pubDate>
		<dc:creator>jtanium</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[root]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://www.jtanium.com/2009/11/04/enforcing-executing-user-on-bash-scripts/</guid>
		<description><![CDATA[Sometimes I want to make sure that a script is *not* being run by the root user: #!/bin/bash if [[ $EUID -eq 0 ]]; then echo "Don't run as root!" exit 1 fi]]></description>
			<content:encoded><![CDATA[<p>Sometimes I want to make sure that a script is *not* being run by the root user:</p>
<pre>#!/bin/bash
if [[ $EUID -eq 0 ]]; then
  echo "Don't run as root!"
  exit 1
fi</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jtanium.com/2009/11/04/enforcing-executing-user-on-bash-scripts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bash Launcher Loop</title>
		<link>http://www.jtanium.com/2008/01/03/bash-launcher-loop/</link>
		<comments>http://www.jtanium.com/2008/01/03/bash-launcher-loop/#comments</comments>
		<pubDate>Thu, 03 Jan 2008 18:02:14 +0000</pubDate>
		<dc:creator>jtanium</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.jtanium.com/blog/?p=59</guid>
		<description><![CDATA[Have you written a bash script that executes a command in a loop, maybe to relaunch a process after it dies? What do you do when that process keeps dying? Enter the Bash Launcher Loop: #!/bin/bash # maximum number of attempts to launch max_launches=2 # maximum number of launches allowed in $reset seconds runaway_max=8 # [...]]]></description>
			<content:encoded><![CDATA[<p>Have you written a bash script that executes a command in a loop, maybe to relaunch a process after it dies?  What do you do when that process keeps dying?</p>
<p>Enter the Bash Launcher Loop:</p>
<pre>#!/bin/bash

# maximum number of attempts to launch
max_launches=2
# maximum number of launches allowed in $reset seconds
runaway_max=8
# minimum
min_seconds=1
# number of seconds to let process run before reset
reset_seconds=10

# number of attempts to launch
launch_attempts_count=0
start_time=`date +%s`

while true; do

  echo "Executing ($launch_attempts_count)..."
#  sleep 1

  # increment the number of launch attempts
  launch_attempts_count=$(($launch_attempts_count+1))
  # get the number of seconds since the start time...
  time=$((`date +%s`-$start_time))
  # if we launched more than $max_launches in less than $min_seconds, then
  # for some reason the process can't start, so exit
  if [ $launch_attempts_count -gt $max_launches -a $time -lt $min_seconds ]; then
    echo "Too many launches!"
    exit 1
  fi
  # If we haven't exceeded the threshold for failed starts, but
  # the process isn't running long enough to reset the counters,
  # we can guess there's a problem that keeps causing the process
  # to crash, so we should probably give up for now
  if [ $launch_attempts_count -gt $runaway_max ]; then
    echo "Process keeps crashing"
    exit 1
  fi
  # If the process has been running for more than $max_seconds, then
  # we're going to assume it was working properly, so reset the counters.
  # Normally, $max_seconds would be set to something like 3600 (1 hour).
  if [ $time -gt $reset_seconds ]; then
    launch_attempts_count=0
    start_time=`date +%s`
  fi

done</pre>
<p>The script above will stop trying to launch the process if:</p>
<ul>
<li>if there are more than 2 launch attempts in 1 second (printing &#8220;Too many launches!)</li>
<li>if there are more than 8 launch attempts in 10 seconds (printing &#8220;Process keeps crashing!)</li>
</ul>
<p>If the process is relaunched less than 8 times in 10 seconds, the <code>$start_time</code> and <code>$launch_attempts_count</code> will be reset, thus starting over.</p>
<p>To see this in action just run the script like it is, and the output will be:</p>
<pre>Executing (0)...
Executing (1)...
Executing (2)...
Too many launches!</pre>
<p>Now uncomment line 19 (<code>#  sleep 1</code>), and the output will be:</p>
<pre>Executing (0)...
Executing (1)...
Executing (2)...
Executing (3)...
Executing (4)...
Executing (5)...
Executing (6)...
Executing (7)...
Executing (8)...
Process keeps crashing</pre>
<p>Finally, change line 19 from <code>sleep 1</code> to <code>sleep 2</code>, and the output will be:</p>
<pre>Executing (0)...
Executing (1)...
Executing (2)...
Executing (3)...
Executing (4)...
Executing (5)...
Executing (0)...
Executing (1)...
Executing (2)...
Executing (3)...
Executing (4)...
Executing (5)...
Executing (0)...</pre>
<p>And so on, until you hit ctrl+c.</p>
<p>Obviously those values are just for demo purposes.  Better production values would be:</p>
<pre>max_launches=2
runaway_max=2
min_seconds=1
reset_seconds=3600</pre>
<p>This tells the script to exit if there are more than two launch attempts in one second or more than two in one hour, otherwise it will just keep running.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jtanium.com/2008/01/03/bash-launcher-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OS X md5sum Hack</title>
		<link>http://www.jtanium.com/2008/01/02/os-x-md5sum-hack/</link>
		<comments>http://www.jtanium.com/2008/01/02/os-x-md5sum-hack/#comments</comments>
		<pubDate>Thu, 03 Jan 2008 04:56:42 +0000</pubDate>
		<dc:creator>jtanium</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.jtanium.com/blog/?p=58</guid>
		<description><![CDATA[I found this nifty little shell script that roughly duplicates the md5sum command on OS X. The problem is, it doesn&#8217;t work anymore, something about the syntax for the expr command being invalid. Anywho, I modified it like so: #!/usr/bin/env bash output=`md5 "$1"` echo `echo $output &#124; sed -e 's/MD5 (\(.*\)) = \(.*\)/\2 \1/'` I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>I found this <a href="http://www.franklinmint.fm/blog/archives/000831.html">nifty little shell script</a> that roughly duplicates the md5sum command on OS X.  The problem is, it doesn&#8217;t work anymore, something about the syntax for the <code>expr</code> command being invalid.</p>
<p>Anywho, I modified it like so:</p>
<pre>#!/usr/bin/env bash
output=`md5 "$1"`
echo `echo $output | sed -e 's/MD5 (\(.*\)) = \(.*\)/\2  \1/'`</pre>
<p>I&#8217;m sure the <code>openssl md5</code> command would work still as well as the <code>md5</code> command.  They both output something similar to:</p>
<pre>MD5 (my_file.txt) = caff1120584b95432eed1e84cb99a593</pre>
<p>That&#8217;s where the <code>sed</code> thing comes in handy, to convert it to:</p>
<pre>caff1120584b95432eed1e84cb99a593  my_file.txt</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jtanium.com/2008/01/02/os-x-md5sum-hack/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Bash: Get current script name and absolute paths</title>
		<link>http://www.jtanium.com/2007/12/21/bash-get-current-script-name-and-relative-and-absolute-paths/</link>
		<comments>http://www.jtanium.com/2007/12/21/bash-get-current-script-name-and-relative-and-absolute-paths/#comments</comments>
		<pubDate>Fri, 21 Dec 2007 20:45:34 +0000</pubDate>
		<dc:creator>jtanium</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.jtanium.com/blog/?p=57</guid>
		<description><![CDATA[#!/usr/bin/env bash echo " \\$0: $0" _my_name=`basename $0` if [ "`echo $0 &#124; cut -c1`" = "/" ]; then _my_path=`dirname $0` else _my_path=`pwd`/`echo $0 &#124; sed -e s/$_my_name//` fi echo " Filename: $_my_name" echo "Absolute path: $_my_path"]]></description>
			<content:encoded><![CDATA[<pre>#!/usr/bin/env bash

echo "           \\$0: $0"

_my_name=`basename $0`
if [ "`echo $0 | cut -c1`" = "/" ]; then
  _my_path=`dirname $0`
else
  _my_path=`pwd`/`echo $0 | sed -e s/$_my_name//`
fi

echo "     Filename: $_my_name"
echo "Absolute path: $_my_path"</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jtanium.com/2007/12/21/bash-get-current-script-name-and-relative-and-absolute-paths/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

