<?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>Fernando Ipar &#187; bash</title>
	<atom:link href="http://fernandoipar.com/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://fernandoipar.com</link>
	<description>I love mankind! Its people I can&#039;t stand!</description>
	<lastBuildDate>Tue, 06 Dec 2011 01:01:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Generating random salts from bash</title>
		<link>http://fernandoipar.com/2009/02/04/generating-random-salts-from-bash/</link>
		<comments>http://fernandoipar.com/2009/02/04/generating-random-salts-from-bash/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 23:19:57 +0000</pubDate>
		<dc:creator>fernando</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://fernandoipar.com/?p=86</guid>
		<description><![CDATA[From the &#8216;just because it can be done&#8217; column, here comes a handy shell script to generate random salts. So, without further ado,  here it goes: #!/bin/bash [ $# -eq 0 ] &#38;&#38; { echo "usage: salt &#60;length&#62;"&#62;&#38;2 exit } strings &#60;/dev/urandom &#124; while read line; do echo $line &#124; tr '\n\t ' $RANDOM:0:1 &#62;&#62; [...]


Related posts:<ol><li><a href='http://fernandoipar.com/2009/08/14/generating-data-with-dbmonster/' rel='bookmark' title='Permanent Link: Generating data with dbmonster'>Generating data with dbmonster</a> <small> In my last post I included some sample data...</small></li><li><a href='http://fernandoipar.com/2009/01/12/running-commands-from-the-shell-with-a-timeout-pt-2/' rel='bookmark' title='Permanent Link: Running commands from the shell with a timeout (pt 2)'>Running commands from the shell with a timeout (pt 2)</a> <small>Here&#8217;s an improved version of the safecmd script. This one...</small></li><li><a href='http://fernandoipar.com/2009/01/10/running-commands-from-the-shell-with-a-timeout/' rel='bookmark' title='Permanent Link: Running commands from the shell with a timeout'>Running commands from the shell with a timeout</a> <small>Sometimes, in a shell script, you need to run a...</small></li></ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>From the &#8216;just because it can be done&#8217; column, here comes a handy shell script to generate random <a title="Cryptographic Salt (Wikipedia)" href="http://en.wikipedia.org/wiki/Salt_(cryptography)">salts</a>.</p>
<p>So, without further ado,  here it goes:</p>
<pre>#!/bin/bash 

[ $# -eq 0 ] &amp;&amp; {
        echo "usage: salt &lt;length&gt;"&gt;&amp;2
        exit
}
strings &lt;/dev/urandom | while read line; do
        echo $line | tr '\n\t ' $RANDOM:0:1 &gt;&gt; /tmp/.salt.$$
        salt=$(cat /tmp/.salt.$$)
        [ ${#salt} -ge $1 ] &amp;&amp; salt=${salt:0:$1} &amp;&amp; echo $salt &amp;&amp; break
done
rm -f /tmp/.salt.$$</pre>
<p>I had to use $1 and not a var, and echo the salt right from inside the while, because the &#8216;|&#8217; creates another shell, so I can&#8217;t pass variables to or from the while in this case.</p>
<p>If you want to keep things simple, you can go perl and just do</p>
<pre>
#!/usr/bin/perl
use Crypt::Salt;
my $length = shift;
print salt($length);
</pre>
<p>But if you ever find yourself in a server with no cpan, the first option might prove useful <img src='http://fernandoipar.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>


<p>Related posts:<ol><li><a href='http://fernandoipar.com/2009/08/14/generating-data-with-dbmonster/' rel='bookmark' title='Permanent Link: Generating data with dbmonster'>Generating data with dbmonster</a> <small> In my last post I included some sample data...</small></li><li><a href='http://fernandoipar.com/2009/01/12/running-commands-from-the-shell-with-a-timeout-pt-2/' rel='bookmark' title='Permanent Link: Running commands from the shell with a timeout (pt 2)'>Running commands from the shell with a timeout (pt 2)</a> <small>Here&#8217;s an improved version of the safecmd script. This one...</small></li><li><a href='http://fernandoipar.com/2009/01/10/running-commands-from-the-shell-with-a-timeout/' rel='bookmark' title='Permanent Link: Running commands from the shell with a timeout'>Running commands from the shell with a timeout</a> <small>Sometimes, in a shell script, you need to run a...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://fernandoipar.com/2009/02/04/generating-random-salts-from-bash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Running commands from the shell with a timeout (pt 2)</title>
		<link>http://fernandoipar.com/2009/01/12/running-commands-from-the-shell-with-a-timeout-pt-2/</link>
		<comments>http://fernandoipar.com/2009/01/12/running-commands-from-the-shell-with-a-timeout-pt-2/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 22:45:33 +0000</pubDate>
		<dc:creator>fernando</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://fernandoipar.com/?p=29</guid>
		<description><![CDATA[Here&#8217;s an improved version of the safecmd script. This one doesn&#8217;t always wait for $timeout seconds even if the command you&#8217;re running exits successfully. In that case, it kills the monitor script and ends at the proper time. Here&#8217;s the code: #!/bin/bash timeout=$1 command=$2 shift 2 check_pid_name() { command=$1 childpid=$2 [ -d /proc/$childpid ] &#124;&#124; [...]


Related posts:<ol><li><a href='http://fernandoipar.com/2009/01/10/running-commands-from-the-shell-with-a-timeout/' rel='bookmark' title='Permanent Link: Running commands from the shell with a timeout'>Running commands from the shell with a timeout</a> <small>Sometimes, in a shell script, you need to run a...</small></li><li><a href='http://fernandoipar.com/2009/02/04/generating-random-salts-from-bash/' rel='bookmark' title='Permanent Link: Generating random salts from bash'>Generating random salts from bash</a> <small>From the &#8216;just because it can be done&#8217; column, here...</small></li></ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s an improved version of the safecmd script. This one doesn&#8217;t always wait for $timeout seconds even if the command you&#8217;re running exits successfully. In that case, it kills the monitor script and ends at the proper time. </p>
<p>Here&#8217;s the code: </p>
<pre>
#!/bin/bash 

timeout=$1
command=$2
shift 2

check_pid_name()
{
        command=$1
        childpid=$2
        [ -d /proc/$childpid ] || return 1
        [ $(grep -c $command /proc/$childpid/cmdline 2>/dev/null) -gt 0 ] &#038;&#038; return 0 || return 1
}

[ -z "$command" ] &#038;&#038; echo "Usage: safecmd <timeout> <command> [args]">&#038;2 &#038;&#038; exit 1

safe_run()
{
        command=$1
        shift
        $command $*
        kill $$
}

safe_run $command $* &#038;
childpid=$!
sleep $timeout 

check_pid_name $command $childpid &#038;&#038; {
        kill $childpid
        sleep 0.1
        check_pid_name $command $childpid &#038;&#038; kill -9 $childpid
        echo "$command $* timed out"
}
</pre>
<p>You can try it how like this, for example: </p>
<pre>
./safecmd 2 sleep 4
</pre>
<p>which will output</p>
<pre>
./safecmd.1: line 34: 11739 Terminated               safe_run $command $*
sleep 4 timed out
</pre>
<pre>
or ./safecmd 2 sleep 1
</pre>
<p>which will output</p>
<pre>
Terminated
</pre>
<p>Indicating that sleep 1 finished successfully</p>
<p>This is still missing something. You can&#8217;t test for the success of the command you pass safecmd, if it exits successfully you&#8217;ll see the same return code than if it would have exited with an error. </p>
<p>In order to improve this, as far as I can tell, you have to use two scripts, one just as the wrapper (to use one separate script instead of the function safe_run()). That&#8217;s the way this is handled in <a href="http://highbase.seriema-systems.com">Highbase</a>, and I&#8217;ll post a full example in my next post on this subject. </p>


<p>Related posts:<ol><li><a href='http://fernandoipar.com/2009/01/10/running-commands-from-the-shell-with-a-timeout/' rel='bookmark' title='Permanent Link: Running commands from the shell with a timeout'>Running commands from the shell with a timeout</a> <small>Sometimes, in a shell script, you need to run a...</small></li><li><a href='http://fernandoipar.com/2009/02/04/generating-random-salts-from-bash/' rel='bookmark' title='Permanent Link: Generating random salts from bash'>Generating random salts from bash</a> <small>From the &#8216;just because it can be done&#8217; column, here...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://fernandoipar.com/2009/01/12/running-commands-from-the-shell-with-a-timeout-pt-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Running commands from the shell with a timeout</title>
		<link>http://fernandoipar.com/2009/01/10/running-commands-from-the-shell-with-a-timeout/</link>
		<comments>http://fernandoipar.com/2009/01/10/running-commands-from-the-shell-with-a-timeout/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 02:21:49 +0000</pubDate>
		<dc:creator>fernando</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://fernandoipar.com/?p=20</guid>
		<description><![CDATA[Sometimes, in a shell script, you need to run a command that might go on forever (or an amount of time long enough to be called forever) due to poor coding of the software and wrong external conditions (network down or overloaded, overloaded system, a changed private key, etc). In order to avoid this situations, [...]


Related posts:<ol><li><a href='http://fernandoipar.com/2009/01/12/running-commands-from-the-shell-with-a-timeout-pt-2/' rel='bookmark' title='Permanent Link: Running commands from the shell with a timeout (pt 2)'>Running commands from the shell with a timeout (pt 2)</a> <small>Here&#8217;s an improved version of the safecmd script. This one...</small></li><li><a href='http://fernandoipar.com/2009/02/04/generating-random-salts-from-bash/' rel='bookmark' title='Permanent Link: Generating random salts from bash'>Generating random salts from bash</a> <small>From the &#8216;just because it can be done&#8217; column, here...</small></li><li><a href='http://fernandoipar.com/2011/03/10/piping-data-to-multiple-processes/' rel='bookmark' title='Permanent Link: Piping data to multiple processes'>Piping data to multiple processes</a> <small>Here&#8217;s a simple shell script to stream data to multiple...</small></li></ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Sometimes, in a shell script, you need to run a command that might go on forever (or an amount of time long enough to be called forever) due to poor coding of the software and wrong external conditions (network down or overloaded, overloaded system, a changed private key, etc).</p>
<p>In order to avoid this situations, here&#8217;s a small shell script that will execute any command with a guaranteed timeout:</p>
<pre>#!/bin/bash

timeout=$1
command=$2

check_pid_name()
{
        command=$1
        childpid=$2
        [ -d /proc/$childpid ] || return 1
        [ $(grep -c $command /proc/$childpid/cmdline 2&gt;/dev/null) -gt 0 ] &amp;&amp; return 0 || return 1
}

[ -z "$command" ] &amp;&amp; echo "I need a command to run"&gt;&amp;2 &amp;&amp; exit 1

[ $# -eq 2 ] &amp;&amp; shift || shift 2

$command $* &amp;
childpid=$!

sleep $timeout 

check_pid_name $command $childpid &amp;&amp; {
        kill $childpid
        sleep 0.1
        check_pid_name $command $childpid &amp;&amp; kill -9 $childpid
        echo "$command $* timed out"
}</pre>
<p>As you can see, it&#8217;s quite simple. You just have to start the desired command in the background, sleep for the timeout, and then kill the process if it&#8217;s still running.</p>
<p>Yes, this script has a defect, all the commands will wait $timeout to run, but that&#8217;s easy to solve. I&#8217;ll post an improved (but slightly more complex) version in my next post.</p>


<p>Related posts:<ol><li><a href='http://fernandoipar.com/2009/01/12/running-commands-from-the-shell-with-a-timeout-pt-2/' rel='bookmark' title='Permanent Link: Running commands from the shell with a timeout (pt 2)'>Running commands from the shell with a timeout (pt 2)</a> <small>Here&#8217;s an improved version of the safecmd script. This one...</small></li><li><a href='http://fernandoipar.com/2009/02/04/generating-random-salts-from-bash/' rel='bookmark' title='Permanent Link: Generating random salts from bash'>Generating random salts from bash</a> <small>From the &#8216;just because it can be done&#8217; column, here...</small></li><li><a href='http://fernandoipar.com/2011/03/10/piping-data-to-multiple-processes/' rel='bookmark' title='Permanent Link: Piping data to multiple processes'>Piping data to multiple processes</a> <small>Here&#8217;s a simple shell script to stream data to multiple...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://fernandoipar.com/2009/01/10/running-commands-from-the-shell-with-a-timeout/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

