<?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>Web Development &#38; Stuff &#187; Bash scripting</title>
	<atom:link href="http://web-development-blog.co.uk/tag/bash-scripting/feed/" rel="self" type="application/rss+xml" />
	<link>http://web-development-blog.co.uk</link>
	<description>Some interesting findings from web-dev land...</description>
	<lastBuildDate>Mon, 02 Apr 2012 15:10:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Loop over svn stat output deleting files</title>
		<link>http://web-development-blog.co.uk/2010/01/14/loop-over-svn-stat-output-deleting-files/</link>
		<comments>http://web-development-blog.co.uk/2010/01/14/loop-over-svn-stat-output-deleting-files/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 14:43:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Linux servers]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[Bash scripting]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Shell scripting]]></category>

		<guid isPermaLink="false">http://web-development-blog.co.uk/?p=274</guid>
		<description><![CDATA[I had the issue where my MacBook creates files prefixed with ._ while working on a Samba share. I&#8217;ve fixed this once before but it was so long ago, I&#8217;ve forgotten how!! Anyway, I had a stack of these files in a checked-out svn project and needed to delete them. Rather than do each one [...]]]></description>
			<content:encoded><![CDATA[<p>I had the issue where my MacBook creates files prefixed with ._ while working on a Samba share. I&#8217;ve fixed this once before but it was so long ago, I&#8217;ve forgotten how!! Anyway, I had a stack of these files in a checked-out svn project and needed to delete them. Rather than do each one individually I put together this Bash command that loops over each line outputted by svn stat, cleans it up and then removes the file.</p>
<div class="codecolorer-container bash " style="overflow:auto;white-space:nowrap;width:100%"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br /></div></td><td><div class="bash codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw2">svn</span> <span class="kw2">stat</span> <span class="sy0">|</span> <span class="kw2">grep</span> <span class="re5">-F</span> ._ <span class="sy0">|</span> <span class="kw2">sed</span> <span class="st_h">'s/? //'</span> <span class="sy0">|</span> <span class="kw1">while</span> <span class="kw2">read</span> line; <span class="kw1">do</span> <span class="kw2">rm</span> <span class="re5">-f</span> <span class="re1">$line</span>; <span class="kw1">done</span></div></td></tr></tbody></table></div>
<p>The grep -F looks for a literal period (.) in each line. In this case it&#8217;s a period underscore (._) string but we still need the -F.<br />
The sed &#8217;s/? //&#8217; looks for the &#8220;? &#8221; prefix and strips it.<br />
The while loop does the rm -f on each line.</p>
<p>Here&#8217;s another method using xargs instead of a while loop. Also, cut is trimming 9 chars (the file status flag and the whitespace) from the front of outputted line.</p>
<div class="codecolorer-container bash " style="overflow:auto;white-space:nowrap;width:100%"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br /></div></td><td><div class="bash codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw2">svn</span> <span class="kw2">stat</span> <span class="sy0">|</span> <span class="kw2">grep</span> <span class="re5">-F</span> ._ <span class="sy0">|</span> <span class="kw2">cut</span> <span class="re5">-c9-</span> <span class="sy0">|</span> <span class="kw2">xargs</span> <span class="kw2">rm</span> <span class="re5">-f</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://web-development-blog.co.uk/2010/01/14/loop-over-svn-stat-output-deleting-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bash &#8211; Match exact string in directory listing</title>
		<link>http://web-development-blog.co.uk/2009/04/27/bash-match-exact-string-in-directory-listing/</link>
		<comments>http://web-development-blog.co.uk/2009/04/27/bash-match-exact-string-in-directory-listing/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 16:38:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Linux servers]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Bash scripting]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[OSX]]></category>
		<category><![CDATA[Shell scripting]]></category>

		<guid isPermaLink="false">http://web-development-blog.co.uk/?p=150</guid>
		<description><![CDATA[Been doing some bash scripting of late, been meaning to for ages. Really enjoying it too!
Anyway, today I needed to match an exact string in a directory listing (ls). To check if a volume on my Mac Book was mounted, so I could backup to it. Or any other drive for that matter. I ended [...]]]></description>
			<content:encoded><![CDATA[<p>Been doing some bash scripting of late, been meaning to for ages. Really enjoying it too!</p>
<p>Anyway, today I needed to match an exact string in a directory listing (ls). To check if a volume on my Mac Book was mounted, so I could backup to it. Or any other drive for that matter. I ended up using this code:</p>
<div class="codecolorer-container bash " style="overflow:auto;white-space:nowrap;width:100%"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="bash codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="co0">#!/bin/bash</span><br />
<span class="kw1">if</span> <span class="br0">&#91;</span> <span class="st0">&quot;$1&quot;</span> <span class="br0">&#93;</span>; <span class="kw1">then</span><br />
&nbsp; <span class="co0"># Look for an exact match of the first passed argument in /Volumes. </span><br />
&nbsp; <span class="co0"># The -x switch matches the string ($1) exactly. The -c switch counts the outputted lines.</span><br />
&nbsp; <span class="kw1">if</span> <span class="br0">&#91;</span> $<span class="br0">&#40;</span><span class="kw2">ls</span> <span class="sy0">/</span>Volumes<span class="sy0">/</span> <span class="sy0">|</span> <span class="kw2">grep</span> <span class="re5">-c</span> <span class="re5">-x</span> <span class="st0">&quot;$1&quot;</span><span class="br0">&#41;</span> == <span class="nu0">1</span> <span class="br0">&#93;</span>; <span class="kw1">then</span><br />
&nbsp; &nbsp; <span class="co0"># Do stuff...</span><br />
&nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; <span class="kw3">echo</span> <span class="st_h">'Sorry, but no volume with that name not found.'</span><br />
&nbsp; <span class="kw1">fi</span><br />
<span class="kw1">else</span><br />
&nbsp; <span class="kw3">echo</span> <span class="st_h">'Please pass a destination volume to this backup script as the first command line argument.'</span><br />
<span class="kw1">fi</span><br />
<br />
<span class="co0"># Usage</span><br />
<span class="kw2">bash</span>$ backup.sh <span class="st0">&quot;Volume name&quot;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://web-development-blog.co.uk/2009/04/27/bash-match-exact-string-in-directory-listing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

