<?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>Raim &#187; bash</title>
	<atom:link href="http://raim.codingfarm.de/blog/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://raim.codingfarm.de/blog</link>
	<description>My personal front yard on the web</description>
	<lastBuildDate>Sun, 29 Jan 2012 23:47:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>bash: for-loop with glob patterns</title>
		<link>http://raim.codingfarm.de/blog/2010/09/09/bash-for-loop-with-glob-patterns/</link>
		<comments>http://raim.codingfarm.de/blog/2010/09/09/bash-for-loop-with-glob-patterns/#comments</comments>
		<pubDate>Thu, 09 Sep 2010 14:18:16 +0000</pubDate>
		<dc:creator>Rainer Müller</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://raim.codingfarm.de/blog/?p=233</guid>
		<description><![CDATA[It is common to use a for-loop with glob patterns: for i in *.txt; do mv $i $i.old done But if the glob pattern does not match anything it will be preserved unchanged in the command. This results in command execution of mv *.txt *.txt.old which fails because no file named *.txt (literally!) exists. As [...]]]></description>
			<content:encoded><![CDATA[<p>It is common to use a for-loop with glob patterns:</p>
<pre>
for i in *.txt; do
    mv $i $i.old
done
</pre>
<p>But if the glob pattern does not match anything it will be preserved unchanged in the command. This results in command execution of <code>mv *.txt *.txt.old</code> which fails because no file named <code>*.txt</code> (literally!) exists.</p>
<p>As this is not the desired behavior, here is a way how to do this as expected without forking using the <code>nullglob</code> bash shell option.</p>
<pre>
oldnullglob=$(shopt -p nullglob)
shopt -s nullglob

for i in *.txt; do
    mv $i $i.old
done

eval "$oldnullglob" 2>/dev/null
unset oldnullglob
</pre>
<p>This will silently prevent the execution of the <code>mv</code> command. If you use <code>failglob</code> instead of <code>nullglob</code> bash will interrupt the evaluation of any command if the glob pattern did not match anything.</p>
<p>Disclaimer: Be careful with this option, as this will not be the expected behavior in all cases. Most (in)famously it breaks <code>bash-completion </code> if you set it in your interactive bash session. I suggest to use it temporary only.</p>
]]></content:encoded>
			<wfw:commentRss>http://raim.codingfarm.de/blog/2010/09/09/bash-for-loop-with-glob-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hiding MOTD on bash startup</title>
		<link>http://raim.codingfarm.de/blog/2009/05/03/hiding-motd-on-bash-startup/</link>
		<comments>http://raim.codingfarm.de/blog/2009/05/03/hiding-motd-on-bash-startup/#comments</comments>
		<pubDate>Sun, 03 May 2009 16:08:33 +0000</pubDate>
		<dc:creator>Rainer Müller</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[motd]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://raim.codingfarm.de/blog/?p=49</guid>
		<description><![CDATA[Usually bash displays /etc/motd at the time of last login on opening a new shell. I do not find that information very useful, as I am opening new shells a lot, so the time does not really mean anything for me. Also, the MOTD rarely changes. There is a feature in bash to hide these [...]]]></description>
			<content:encoded><![CDATA[<p>Usually bash displays <code>/etc/motd</code> at the time of last login on opening a new shell. I do not find that information very useful, as I am opening new shells a lot, so the time does not really mean anything for me. Also, the MOTD rarely changes.</p>
<p>There is a feature in bash to hide these messages by creating a file <code>~/.hushlogin</code>. This will make bash jump right to the first prompt without any output before.</p>
<p>But just in case the MOTD changes and includes important messages, I enhanced this setup a little bit. Instead of just touching the <code>.hushlogin</code> file, I am storing the old MOTD in it. At startup the old and current MOTD is compared against each other and will be displayed only if it differs. To avoid accidentally missing the MOTD, bash will also ask for my confirmation that I have read it.</p>
<p>Here is the snippet from my <code>~/.bashrc</code>:</p>
<pre>
# Show motd only if necessary
cmp -s $HOME/.hushlogin /etc/motd
if [ $? != 0 ]; then
    echo -e "\n==> !!! IMPORTANT: /etc/motd changed !!! <==\n"
    cat /etc/motd
    echo -e "\n==> !!! IMPORTANT: /etc/motd changed !!! <==\n"
    read -e -n 1 -p "Show again? (Y/n) " ans
    if [ "$ans" == "n" ]; then
        cat /etc/motd > $HOME/.hushlogin
    fi
fi
</pre>
<p><strong>Please note:</strong> If you are going to use this, make sure it will not get executed on non-interactive shells. Otherwise it can break tools like scp, sftp or rsync. To ensure this will not be used on non-interactive shells, I am using this conditional at the top of my <code>~/.bashrc</code>:</p>
<pre>
if [[ $- != *i* ]] ; then
    # Shell is non-interactive.  Be done now!
    return
fi
</pre>
]]></content:encoded>
			<wfw:commentRss>http://raim.codingfarm.de/blog/2009/05/03/hiding-motd-on-bash-startup/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

