<?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>chys&#039;s random notes &#187; shell</title>
	<atom:link href="http://en.chys.info/tag/shell/feed/" rel="self" type="application/rss+xml" />
	<link>http://en.chys.info</link>
	<description>Study more problems; Talk less of isms.</description>
	<lastBuildDate>Tue, 27 Dec 2011 11:56:38 +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>globstar in bash 4 follows directory symlinks</title>
		<link>http://en.chys.info/2009/04/globstar-in-bash-4-follows-symlinks/</link>
		<comments>http://en.chys.info/2009/04/globstar-in-bash-4-follows-symlinks/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 00:24:19 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://en.chys.info/?p=471</guid>
		<description><![CDATA[Globstar is a new feature is bash 4, allowing us to traverse a directory more easily. Unfortunately, it follows directory symlinks and thus can easily cause problems. (bleeding) desktop t # echo ${BASH_VERSINFO[@]} 4 0 17 2 release x86_64-pc-linux-gnu (bleeding) desktop t # shopt -s globstar (bleeding) desktop t # ls -l total 0 lrwxrwxrwx [...]<hr/>
Related posts:<ol>
<li><a href='http://en.chys.info/2008/12/how-bash-changes-terminal-window-title/' rel='bookmark' title='How BASH Changes Terminal Window Title'>How BASH Changes Terminal Window Title</a></li>
<li><a href='http://en.chys.info/2009/02/extglob/' rel='bookmark' title='Extended pattern matching in BASH'>Extended pattern matching in BASH</a></li>
<li><a href='http://en.chys.info/2009/04/dynamic-library-symlinks/' rel='bookmark' title='Dynamic library symlinks'>Dynamic library symlinks</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://bash-hackers.org/wiki/doku.php/bash4#globbing">Globstar</a> is a new feature is bash 4, allowing us to traverse a directory more easily.</p>
<p>Unfortunately, it follows directory symlinks and thus can easily cause problems.</p>
<blockquote style="font-family: monospace;"><p>(bleeding) desktop t # echo ${BASH_VERSINFO[@]}<br />
4 0 17 2 release x86_64-pc-linux-gnu<br />
(bleeding) desktop t # shopt -s globstar<br />
(bleeding) desktop t # ls -l<br />
total 0<br />
lrwxrwxrwx 1 root root 1 2009-04-16 18:58 t -> .<br />
(bleeding) desktop t # find<br />
.<br />
./t<br />
(bleeding) desktop t # echo **<br />
 t t/t t/t/t t/t/t/t t/t/t/t/t t/t/t/t/t/t t/t/t/t/t/t/t t/t/t/t/t/t/t/t t/t/t/t/t/t/t/t/t t/t/t/t/t/t/t/t/t/t t/t/t/t/t/t/t/t/t/t/t t/t/t/t/t/t/t/t/t/t/t/t t/t/t/t/t/t/t/t/t/t/t/t/t t/t/t/t/t/t/t/t/t/t/t/t/t/t t/t/t/t/t/t/t/t/t/t/t/t/t/t/t t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t t/t/t/t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/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t<br />
(bleeding) desktop t # </p></blockquote>
<p>Oh no&#8230;</p>
<p>If you unfortunately tried something like <code>echo /proc/**/meminfo</code>, it probably would make you wait for minutes before dying with &#8220;Insufficient memory.&#8221; (In <code>/proc/fd</code> there resides a <code>root</code> symlink.)</p>
<p>If you use <code><a href="http://www.gnu.org/software/grub/">GRUB</a></code> to boot your Linux system, you are likely to find a symlink in <code>/boot</code> also named <code>boot</code> pointing to the directory itself. Yes, this is going to confuse bash, too. And there surely are many more cases.</p>
<p>So let&#8217;s continue writing <code>find ... | xargs ...</code></p>
<hr/><p>Related posts:<ol>
<li><a href='http://en.chys.info/2008/12/how-bash-changes-terminal-window-title/' rel='bookmark' title='How BASH Changes Terminal Window Title'>How BASH Changes Terminal Window Title</a></li>
<li><a href='http://en.chys.info/2009/02/extglob/' rel='bookmark' title='Extended pattern matching in BASH'>Extended pattern matching in BASH</a></li>
<li><a href='http://en.chys.info/2009/04/dynamic-library-symlinks/' rel='bookmark' title='Dynamic library symlinks'>Dynamic library symlinks</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2009/04/globstar-in-bash-4-follows-symlinks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extended pattern matching in BASH</title>
		<link>http://en.chys.info/2009/02/extglob/</link>
		<comments>http://en.chys.info/2009/02/extglob/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 05:16:00 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://blog.chys.info/?p=271</guid>
		<description><![CDATA[Many features provided by BASH are not widely known or used, but they really can be useful. One example is extglob (extended pattern matching) &#8211; with this, a pattern can be almost as powerful as a regular expression. Use &#8220;shopt -s extglob&#8221; to enable this feature. After that, in addition to the standard asterisks, question [...]<hr/>
Related posts:<ol>
<li><a href='http://en.chys.info/2009/04/globstar-in-bash-4-follows-symlinks/' rel='bookmark' title='globstar in bash 4 follows directory symlinks'>globstar in bash 4 follows directory symlinks</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Many features provided by BASH are not widely known or used, but they really can be useful. One example is <a href="http://www.gnu.org/software/bash/manual/bashref.html#Pattern-Matching"><code>extglob</code></a> (extended pattern matching) &#8211; with this, a pattern can be <acronym title="Almost means nearly, but NOT really...">almost</acronym> as powerful as a <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a>.</p>
<p>Use &#8220;<code>shopt -s extglob</code>&#8221; to enable this feature. After that, in addition to the standard asterisks, question marks and square brackets, we can also use the following five sub-patterns:</p>
<p><code>?(pattern-list)</code>: Matches empty or one of the patterns<br />
<code>*(pattern-list)</code>: Matches empty or any number of occurrences of the patterns<br />
<code>+(pattern-list)</code>: Matches at least one occurrences of the patterns<br />
<code>@(pattern-list)</code>: Matches exactly one of the patterns<br />
<code>!(pattern-list)</code>: Matches anything EXCEPT any of the patterns</p>
<p>The <code>pattern-list</code> represents one or more patterns, which can again contain these extended sub-patterns, delimited by pipe signs (<code>|</code>). Two simple examples:</p>
<ul>
<li><code>rm -rf !(lost+found)</code><br />
Removes everything except <code>lost+found</code></li>
<li><code>for x in *.@(jp?(e)g|gif|png)</code><br />
Loops through all files having extension <code>jpg</code>, <code>jpeg</code>, <code>gif</code>, or <code>png</code></li>
</ul>
<p>The following example is a little more complicated. It prints a list of default/GNU/Intel C/C++ compilers present in directories specified by <code>$PATH</code>:</p>
<blockquote><pre>#!/bin/bash
shopt -s extglob nullglob
x="${PATH//:/,}"
eval "printf '%s\n' {$x}/@(?([ig])cc|[cg]++|icpc)?(-+([0-9])+(\.+([0-9])))"</pre>
</blockquote>
<p>(NOTE: <code>nullglob</code> makes a pattern matching no file <del datetime="2009-02-27T07:40:45+00:00">to</del> expand to nothing instead of unchanged.)</p>
<p>Doesn&#8217;t it look like a regular expression? The output is like this in my system:</p>
<blockquote><pre>/usr/bin/c++
/usr/bin/c++-4.2.4
/usr/bin/c++-4.3.3
/usr/bin/cc
/usr/bin/g++
/usr/bin/g++-4.2.4
/usr/bin/g++-4.3.3
/usr/bin/gcc
/usr/bin/gcc-4.2.4
/usr/bin/gcc-4.3.3
/usr/x86_64-pc-linux-gnu/gcc-bin/4.2.4/c++
/usr/x86_64-pc-linux-gnu/gcc-bin/4.2.4/g++
/usr/x86_64-pc-linux-gnu/gcc-bin/4.2.4/gcc
/opt/intel/cce/10.1.018/bin/icc
/opt/intel/cce/10.1.018/bin/icpc
</pre>
</blockquote>
<p>Unfortunately, we cannot use the following codes:</p>
<blockquote><pre>x="${PATH//:/|}"
printf '%s\n' $x/@(?([ig])cc|[cg]++|icpc)?(-+([0-9])+(\.+([0-9])))</pre>
</blockquote>
<p>This is not surprising, however. No sub-patterns is allowed to expand to a string including forward slashes (path delimiter)<sup>[1]</sup>. (This means a single asterisk won&#8217;t expand to a file in a subdirectory, which is usually desired. Bash 4 has introduced <code>**</code> which matches slashes as well.)</p>
<p>[1] In a <code>case</code> statement or a <code>[[ ]]</code> builtin (using the <code>==</code> operator), sub-patterns indeed match slashes.</p>
<hr/><p>Related posts:<ol>
<li><a href='http://en.chys.info/2009/04/globstar-in-bash-4-follows-symlinks/' rel='bookmark' title='globstar in bash 4 follows directory symlinks'>globstar in bash 4 follows directory symlinks</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2009/02/extglob/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>BASH’s ‘read’ built-in supports &#8216;\0&#8242; as delimiter</title>
		<link>http://en.chys.info/2008/11/bash%e2%80%99s-%e2%80%98read%e2%80%99-built-in-supports-0-as-delimiter/</link>
		<comments>http://en.chys.info/2008/11/bash%e2%80%99s-%e2%80%98read%e2%80%99-built-in-supports-0-as-delimiter/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 02:30:00 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://blog.chys.info/2008/11/bash%e2%80%99s-%e2%80%98read%e2%80%99-built-in-supports-0-as-delimiter/</guid>
		<description><![CDATA[I thought it was impossible to use &#8216;&#8217; as a delimiter in bash, but noticed yesterday that Gentoo’s ebuild.sh had pipelines like this: find &#8230;.. -print0 &#124; while read -r -d $&#8217;&#8217; x; do # Do something with file $x done This makes it possible to handle any strange filenames correctly, even if the filename [...]<hr/>
Related posts:<ol>
<li><a href='http://en.chys.info/2009/02/extglob/' rel='bookmark' title='Extended pattern matching in BASH'>Extended pattern matching in BASH</a></li>
<li><a href='http://en.chys.info/2009/04/globstar-in-bash-4-follows-symlinks/' rel='bookmark' title='globstar in bash 4 follows directory symlinks'>globstar in bash 4 follows directory symlinks</a></li>
<li><a href='http://en.chys.info/2008/12/how-bash-changes-terminal-window-title/' rel='bookmark' title='How BASH Changes Terminal Window Title'>How BASH Changes Terminal Window Title</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I thought it was impossible to use &#8216; &#8217; as a delimiter in bash, but noticed yesterday that Gentoo’s <code>ebuild.sh</code> had pipelines like this:</p>
<blockquote style="font-family: monospace;"><p>find &#8230;.. -print0 |<br />
while read -r -d $&#8217; &#8217; x; do<br />
# Do something with file $x<br />
done</p></blockquote>
<p>
This makes it possible to handle any strange filenames correctly, even if the filename contains newline (<code>'n'</code>) or carriage return (<code>'r'</code>) characters. (Some other commands, including <code>sort</code> and <code>xargs</code>, have options to make null character the delimiter based on the same reason.)</p>
<p>Because BASH internally uses C-style strings, in which <code>' '</code> is the terminator, <code>read -d $' '</code> is essentially equivalent to <code>read -d ''</code>. This is why I believed <code>read</code> did not accept null-delimited strings. However, it turns out that BASH actually handles this correctly.</p>
<p>I checked BASH’s souce code and found the delimiter was simply determined by <code>delim = *list_optarg;</code> (<code>bash-3.2/builtins/read.def</code>, line 296) where <code>list_optarg</code> points to the argument following <code>-d</code>. Therefore, it makes no difference to the value of <code>delim</code> whether <code>$' '</code> or <code>''</code> is used.
<div class="blogger-post-footer">
<hr />
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://creativecommons.org/images/public/somerights20.png" /></a><br />This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License</a>.</div>
<hr/><p>Related posts:<ol>
<li><a href='http://en.chys.info/2009/02/extglob/' rel='bookmark' title='Extended pattern matching in BASH'>Extended pattern matching in BASH</a></li>
<li><a href='http://en.chys.info/2009/04/globstar-in-bash-4-follows-symlinks/' rel='bookmark' title='globstar in bash 4 follows directory symlinks'>globstar in bash 4 follows directory symlinks</a></li>
<li><a href='http://en.chys.info/2008/12/how-bash-changes-terminal-window-title/' rel='bookmark' title='How BASH Changes Terminal Window Title'>How BASH Changes Terminal Window Title</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2008/11/bash%e2%80%99s-%e2%80%98read%e2%80%99-built-in-supports-0-as-delimiter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jumbled Characters after Catting a Binary File</title>
		<link>http://en.chys.info/2008/10/jumbled-characters-after-catting-a-binary-file/</link>
		<comments>http://en.chys.info/2008/10/jumbled-characters-after-catting-a-binary-file/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 02:05:00 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[terminal]]></category>

		<guid isPermaLink="false">http://blog.chys.info/2008/10/jumbled-characters-after-catting-a-binary-file/</guid>
		<description><![CDATA[When this happens, simply press Ctrl-V Ctrl-O Ctrl-M. Or alternatively, type &#8220;reset&#8221; and Return (Enter). A terminal interpretes 0x0e byte as &#8220;activates the G1 character set&#8221;, and 0x0f as &#8220;activates the G0 character set&#8221;. The characters we read are in the G0 set. So, if there is no byte 0x0f after the last 0x0e in [...]<hr/>
Related posts:<ol>
<li><a href='http://en.chys.info/2009/03/utf-8/' rel='bookmark' title='UTF-8'>UTF-8</a></li>
<li><a href='http://en.chys.info/2008/10/determine-the-existence-of-a-file-in-makefile/' rel='bookmark' title='Determine the existence of a file in Makefile'>Determine the existence of a file in Makefile</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>When this happens, simply press Ctrl-V Ctrl-O Ctrl-M. Or alternatively, type &#8220;reset&#8221; and Return (Enter).</p>
<p>A terminal interpretes 0x0e byte as &#8220;activates the G1 character set&#8221;, and 0x0f as &#8220;activates the G0 character set&#8221;. The characters we read are in the G0 set. So, if there is no byte 0x0f after the last 0x0e in a binary file, everything will be shown in the unreadable G1 set, including the next shell prompt.</p>
<p>How does Ctrl-V Ctrl-O Ctrl-M work?<br />Ctrl-V is an &#8216;escape character&#8217; &#8211; the next keystroke will always be interpreted as a literal character; Ctrl-O is 0x0f; Ctrl-M is carriage return. So the shell gets the command &#8220;x0f&#8221; and outputs the error message &#8220;bash: x0f: command not found&#8221;. The byte 0x0f in this message turns the active character back to the readable G0.</p>
<p>G1 character set is not often used these days. Konsole chooses not to implement it at all, so we never have this problem in Konsole.
<div class="blogger-post-footer">
<hr />
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://creativecommons.org/images/public/somerights20.png" /></a><br />This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License</a>.</div>
<hr/><p>Related posts:<ol>
<li><a href='http://en.chys.info/2009/03/utf-8/' rel='bookmark' title='UTF-8'>UTF-8</a></li>
<li><a href='http://en.chys.info/2008/10/determine-the-existence-of-a-file-in-makefile/' rel='bookmark' title='Determine the existence of a file in Makefile'>Determine the existence of a file in Makefile</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2008/10/jumbled-characters-after-catting-a-binary-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

