<?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>dan.thoughts &#187; bash</title>
	<atom:link href="http://blog.sosedoff.com/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sosedoff.com</link>
	<description>Web-development, PHP, Ruby, Sinatra, Merb, Rails, MySQL, SQLite, Web Services.</description>
	<lastBuildDate>Wed, 25 Jan 2012 18:54:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Fast and easy password generation</title>
		<link>http://blog.sosedoff.com/2011/01/22/fast-and-easy-password-generation/</link>
		<comments>http://blog.sosedoff.com/2011/01/22/fast-and-easy-password-generation/#comments</comments>
		<pubDate>Sun, 23 Jan 2011 01:21:15 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Shell Scripting]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[passwords]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=316</guid>
		<description><![CDATA[I used to generate a lot of passwords and usually it would be an online service found by googling &#8220;generate password online&#8221;. It worked fine until i got tired of it and decided to find something else, much easier and faster. Something that will give me the results right away from terminal while doing server [...]]]></description>
			<content:encoded><![CDATA[<p>I used to generate a lot of passwords and usually it would be an online service found by googling &#8220;generate password online&#8221;. It worked fine until i got tired of it and decided to find something else, much easier and faster. Something that will give me the results right away from terminal while doing server setup. </p>
<p>Here we go, bash script (found online and modified by my needs):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #007800;">charspool</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'a'</span> <span style="color: #ff0000;">'b'</span> <span style="color: #ff0000;">'c'</span> <span style="color: #ff0000;">'d'</span> <span style="color: #ff0000;">'e'</span> <span style="color: #ff0000;">'f'</span> <span style="color: #ff0000;">'g'</span> <span style="color: #ff0000;">'h'</span> <span style="color: #ff0000;">'i'</span> <span style="color: #ff0000;">'j'</span> <span style="color: #ff0000;">'k'</span> <span style="color: #ff0000;">'l'</span> <span style="color: #ff0000;">'m'</span> <span style="color: #ff0000;">'n'</span> <span style="color: #ff0000;">'o'</span> <span style="color: #ff0000;">'p'</span>
<span style="color: #ff0000;">'q'</span> <span style="color: #ff0000;">'r'</span> <span style="color: #ff0000;">'s'</span> <span style="color: #ff0000;">'t'</span> <span style="color: #ff0000;">'u'</span> <span style="color: #ff0000;">'v'</span> <span style="color: #ff0000;">'w'</span> <span style="color: #ff0000;">'x'</span> <span style="color: #ff0000;">'y'</span> <span style="color: #ff0000;">'z'</span> <span style="color: #ff0000;">'0'</span> <span style="color: #ff0000;">'1'</span> <span style="color: #ff0000;">'2'</span> <span style="color: #ff0000;">'3'</span> <span style="color: #ff0000;">'4'</span> <span style="color: #ff0000;">'5'</span> <span style="color: #ff0000;">'6'</span> <span style="color: #ff0000;">'7'</span>
<span style="color: #ff0000;">'8'</span> <span style="color: #ff0000;">'9'</span> <span style="color: #ff0000;">'0'</span> <span style="color: #ff0000;">'A'</span> <span style="color: #ff0000;">'B'</span> <span style="color: #ff0000;">'C'</span> <span style="color: #ff0000;">'D'</span> <span style="color: #ff0000;">'E'</span> <span style="color: #ff0000;">'F'</span> <span style="color: #ff0000;">'G'</span> <span style="color: #ff0000;">'H'</span> <span style="color: #ff0000;">'I'</span> <span style="color: #ff0000;">'J'</span> <span style="color: #ff0000;">'K'</span> <span style="color: #ff0000;">'L'</span> <span style="color: #ff0000;">'M'</span> <span style="color: #ff0000;">'N'</span> <span style="color: #ff0000;">'O'</span>
<span style="color: #ff0000;">'P'</span> <span style="color: #ff0000;">'Q'</span> <span style="color: #ff0000;">'R'</span> <span style="color: #ff0000;">'S'</span> <span style="color: #ff0000;">'T'</span> <span style="color: #ff0000;">'U'</span> <span style="color: #ff0000;">'V'</span> <span style="color: #ff0000;">'W'</span> <span style="color: #ff0000;">'X'</span> <span style="color: #ff0000;">'Y'</span> <span style="color: #ff0000;">'Z'</span> <span style="color: #ff0000;">'-'</span> <span style="color: #ff0000;">'_'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
&nbsp;
<span style="color: #007800;">len</span>=<span style="color: #800000;">${#charspool[*]}</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #660033;">-lt</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #007800;">num</span>=<span style="color: #000000;">20</span>;
<span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #007800;">num</span>=$<span style="color: #000000;">1</span>;
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">randomnumbers</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">head</span> <span style="color: #660033;">-c</span> <span style="color: #007800;">$num</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>urandom <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">od</span> <span style="color: #660033;">-t</span> u1 <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{for (i = 2; i &lt;= NF; i++) print $i}'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;password: &quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> c <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$randomnumbers</span>; <span style="color: #000000; font-weight: bold;">do</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #800000;">${charspool[$((c % len))]}</span>
<span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #7a0874; font-weight: bold;">echo</span></pre></div></div>

<h3>Installation</h3>
<p>Just create a script called something like &#8220;genpassword&#8221; in your bin dir and make it executable.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">nano</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>genpassword
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chmod</span> +x <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>genpassword</pre></div></div>

<p>Works on any unix-like machine with bash installed.</p>
<h3>Usage</h3>
<p>Usage is pretty straight-forward. Just type &#8220;genpassword&#8221; in terminal and you&#8217;ll get the password.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">genpassword <span style="color: #666666; font-style: italic;"># This will give a default length (20 chars) password</span>
genpassword <span style="color: #000000;">64</span> <span style="color: #666666; font-style: italic;"># This will give you a 64 chars long password</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2011/01/22/fast-and-easy-password-generation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Snippet: Cleanup your Git repository</title>
		<link>http://blog.sosedoff.com/2010/06/15/snippet-cleanup-your-git-repository/</link>
		<comments>http://blog.sosedoff.com/2010/06/15/snippet-cleanup-your-git-repository/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 06:35:56 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Git]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Shell Scripting]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[cleanup]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[repository]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=240</guid>
		<description><![CDATA[Snippet (found on net) for removing files from repository that are no longer present under your project.

$ git rm $&#40;git ls-files -d&#41;

For best use add it to bash alias file: ~/.bashrc or ~/.bash-aliases (under ubuntu):

alias gitclean='git rm $(git ls-files -d)'

]]></description>
			<content:encoded><![CDATA[<p>Snippet (found on net) for removing files from repository that are no longer present under your project.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ git <span style="color: #c20cb9; font-weight: bold;">rm</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span>git ls-files -d<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>For best use add it to bash alias file: ~/.bashrc or ~/.bash-aliases (under ubuntu):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gitclean</span>=<span style="color: #ff0000;">'git rm $(git ls-files -d)'</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2010/06/15/snippet-cleanup-your-git-repository/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Date separated MySQL backups</title>
		<link>http://blog.sosedoff.com/2009/09/18/date-separated-mysql-backups/</link>
		<comments>http://blog.sosedoff.com/2009/09/18/date-separated-mysql-backups/#comments</comments>
		<pubDate>Fri, 18 Sep 2009 14:04:59 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Shell Scripting]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=163</guid>
		<description><![CDATA[Here is the bash shell script that makes archived dumps of your database server. All databases are separated from each other and stored into date based folders. 

#!/bin/bash

MyUSER="root"
MyPASS=""
MyHOST="localhost"
NOW="$(date +"%d-%m-%Y")"
STOREDIR="/home/storage/backup/database/by_dates/$NOW"
DBLIST="$(mysql -u $MyUSER -h $MyHOST -Bse 'show databases')"

[ ! -d $STOREDIR ] &#038;&#038; mkdir -p $STOREDIR &#124;&#124; :

for db in $DBLIST
do
	FILE="$STOREDIR/$db.gz"
	mysqldump -u $MyUSER -h $MyHOST $db &#124; [...]]]></description>
			<content:encoded><![CDATA[<p>Here is the bash shell script that makes archived dumps of your database server. All databases are separated from each other and stored into date based folders. </p>
<pre>
#!/bin/bash

MyUSER="root"
MyPASS=""
MyHOST="localhost"
NOW="$(date +"%d-%m-%Y")"
STOREDIR="/home/storage/backup/database/by_dates/$NOW"
DBLIST="$(mysql -u $MyUSER -h $MyHOST -Bse 'show databases')"

[ ! -d $STOREDIR ] &#038;&#038; mkdir -p $STOREDIR || :

for db in $DBLIST
do
	FILE="$STOREDIR/$db.gz"
	mysqldump -u $MyUSER -h $MyHOST $db | gzip -9 > $FILE
done
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/09/18/date-separated-mysql-backups/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Install Git on CentOS 5.2</title>
		<link>http://blog.sosedoff.com/2009/06/28/install-git-on-centos-52/</link>
		<comments>http://blog.sosedoff.com/2009/06/28/install-git-on-centos-52/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 03:28:19 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Git]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[centos]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=151</guid>
		<description><![CDATA[First, we need to install all dependencies:

# yum install gettext-devel expat-devel curl-devel zlib-devel openssl-devel

Next, get the git 1.6.x sources:

# wget http://kernel.org/pub/software/scm/git/git-1.6.3.3.tar.gz

Then, unpack and cd into git sources folder and install it:

# make &#38;&#38; make install &#38; make clean

That`s it, now you`ll have git system ready to go.
]]></description>
			<content:encoded><![CDATA[<p>First, we need to install all dependencies:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># yum install gettext-devel expat-devel curl-devel zlib-devel openssl-devel</span></pre></div></div>

<p>Next, get the git 1.6.x sources:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># wget http://kernel.org/pub/software/scm/git/git-1.6.3.3.tar.gz</span></pre></div></div>

<p>Then, unpack and cd into git sources folder and install it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># make &amp;&amp; make install &amp; make clean</span></pre></div></div>

<p>That`s it, now you`ll have git system ready to go.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/06/28/install-git-on-centos-52/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Check root user permissions in bash scripts</title>
		<link>http://blog.sosedoff.com/2009/03/09/check-root-user-permissions-in-bash-scripts/</link>
		<comments>http://blog.sosedoff.com/2009/03/09/check-root-user-permissions-in-bash-scripts/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 03:07:33 +0000</pubDate>
		<dc:creator>Dan Sosedoff</dc:creator>
				<category><![CDATA[Shell Scripting]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[permissions]]></category>
		<category><![CDATA[root]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://blog.sosedoff.com/?p=71</guid>
		<description><![CDATA[For example, in your bash shell script you`re going to use some root-specific commands like network operations, mounting devices and so on. There are couple easy ways to check if your script is executing under root privileges.

#!/bin/bash
# ...
if &#91; &#34;$(id -u)&#34; != &#34;0&#34; &#93;; then
   echo &#34;This script must be run as root&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>For example, in your bash shell script you`re going to use some root-specific commands like network operations, mounting devices and so on. There are couple easy ways to check if your script is executing under root privileges.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># ...</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(id -u)</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
   <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;This script must be run as root&quot;</span> <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span>
   <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #666666; font-style: italic;"># ...</span></pre></div></div>

<p>Another way: use EUID. When user account created a user ID is assigned to each user. Bash shell stores the user ID in $UID variable. Your effective user ID is stored in $EUID variable.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># ...</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$EUID</span> <span style="color: #660033;">-ne</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
   <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;This script must be run as root&quot;</span> <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span>
   <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #666666; font-style: italic;"># ...</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.sosedoff.com/2009/03/09/check-root-user-permissions-in-bash-scripts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

