<?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>Brett Terpstrafunction - Brett Terpstra</title>
	<atom:link href="http://brettterpstra.com/tag/function/feed/" rel="self" type="application/rss+xml" />
	<link>http://brettterpstra.com</link>
	<description>Elegant solutions to complex problems.</description>
	<lastBuildDate>Thu, 09 Feb 2012 15:01:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>fk: redux</title>
		<link>http://brettterpstra.com/fk-redux/</link>
		<comments>http://brettterpstra.com/fk-redux/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 17:49:15 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://brettterpstra.com/2010/03/06/fk-redux/</guid>
		<description><![CDATA[<p>Just a quick change to my post on the bash function fk that I’ve been using. A small modification has greatly improved its usability: make the cancel option always be first in the menu. Just move “Cancel” before the $(fp $1) bit. It’s a little odd that I didn’t do that to begin with… fp () { #find and list&#8230;</p><p>Originally posted on <a href="http://brettterpstra.com" title="BrettTerpstra.com">BrettTerpstra.com</a> at <a href="http://brettterpstra.com/fk-redux/">fk: redux</a></p>]]></description>
			<content:encoded><![CDATA[<p>Just a quick change to <a href="http://brettterpstra.com/2009/11/14/fk-a-useful-bash-function/">my post on the bash function <code>fk</code></a> that I’ve been using. A small modification has greatly improved its usability: make the cancel option always be first in the menu. Just move “Cancel” before the <code>$(fp $1)</code> bit. It’s a little odd that I didn’t do that to begin with…</p>

<div markdown=0>
<pre><code>
fp () { #find and list processes matching a case-insensitive partial-match string
        ps Ao pid,comm|awk &#x27;{match($0,/[^\/]+$/); print substr($0,RSTART,RLENGTH)&quot;: &quot;$1}&#x27;|grep -i $1|grep -v grep
}

fk () { 
    IFS=$&#x27;\n&#x27;
    PS3=&#x27;Kill which process? (1 to cancel): &#x27;
    select OPT in &quot;Cancel&quot; $(fp $1); do
        if [ $OPT != &quot;Cancel&quot; ]; then
            kill $(echo $OPT|awk &#x27;{print $NF}&#x27;)
        fi
        break
    done
    unset IFS
}
</code></pre>
</div>
<p>Related posts:<ol>
<li><a href='http://brettterpstra.com/fk-a-useful-bash-function/' rel='bookmark' title='fk: a useful bash function'>fk: a useful bash function</a></li>
<li><a href='http://brettterpstra.com/oft-bash-function-for-opening-a-specific-filetype/' rel='bookmark' title='oft: Bash function for opening a specific filetype'>oft: Bash function for opening a specific filetype</a></li>
<li><a href='http://brettterpstra.com/bash-auto-complete-for-running-applications/' rel='bookmark' title='Bash auto-complete for running applications'>Bash auto-complete for running applications</a></li>
</ol></p><p>Originally posted on <a href="http://brettterpstra.com" title="BrettTerpstra.com">BrettTerpstra.com</a> at <a href="http://brettterpstra.com/fk-redux/">fk: redux</a></p>]]></content:encoded>
			<wfw:commentRss>http://brettterpstra.com/fk-redux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>fk: a useful bash function</title>
		<link>http://brettterpstra.com/fk-a-useful-bash-function/</link>
		<comments>http://brettterpstra.com/fk-a-useful-bash-function/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 23:59:53 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[function]]></category>

		<guid isPermaLink="false">http://brettterpstra.com/?p=156</guid>
		<description><![CDATA[<p>This is a function from my OS X .bash_profile. ‘fk’ is short for Find and Kill, and it lets you do a quick search of your running processes for a case-insensitive partial match of the first parameter passed to it. It’s useful for quickly finding a process without worrying about its capitalization or full spelling, and without having to sift&#8230;</p><p>Originally posted on <a href="http://brettterpstra.com" title="BrettTerpstra.com">BrettTerpstra.com</a> at <a href="http://brettterpstra.com/fk-a-useful-bash-function/">fk: a useful bash function</a></p>]]></description>
			<content:encoded><![CDATA[<p>This is a function from my OS X .bash_profile. ‘fk’ is short for Find and Kill, and it lets you do a quick search of your running processes for a case-insensitive partial match of the first parameter passed to it. It’s useful for quickly finding a process without worrying about its capitalization or full spelling, and without having to sift through (or manually grep) a long <code>ps ax</code> list.</p>

<p><span id="more-156"></span></p>

<div markdown="0">
<pre><code class="bash">
fp () { #find and list processes matching a case-insensitive partial-match string
        ps Ao pid,comm|awk '{match($0,/[^\/]+$/); print substr($0,RSTART,RLENGTH)": "$1}'|grep -i $1|grep -v grep
}

fk () { # build menu to kill process
    IFS=$'\n'
    PS3='Kill which process? '
    select OPT in $(fp $1) "Cancel"; do
        if [ $OPT != "Cancel" ]; then
            kill $(echo $OPT|awk '{print $NF}')
        fi
        break
    done
    unset IFS
}
</code></pre>
</div>

<p>On OS X (and similar Linux systems), you should have a file in your home folder (<code>cd ~</code>) called .bash_profile. Just copy and paste this code at the bottom of that file, and then run <code>source ~/.bash_profile</code> in Terminal. Now (and every time you log in) you can run <code>fk process</code>, where <em>process</em> is a partial name of a running application or UNIX process. You’ll get a menu with the matches, and you can kill a specific process by typing its number at the prompt. The last option is always “Cancel,” which will terminate the command without taking any action. I’d love to hear about any improvements you make to this code… I’m far from a Bash pro.</p>
<p>Related posts:<ol>
<li><a href='http://brettterpstra.com/fk-redux/' rel='bookmark' title='fk: redux'>fk: redux</a></li>
<li><a href='http://brettterpstra.com/oft-bash-function-for-opening-a-specific-filetype/' rel='bookmark' title='oft: Bash function for opening a specific filetype'>oft: Bash function for opening a specific filetype</a></li>
<li><a href='http://brettterpstra.com/a-bash-function-for-markdown-bloggers/' rel='bookmark' title='A Bash function for Markdown bloggers'>A Bash function for Markdown bloggers</a></li>
</ol></p><p>Originally posted on <a href="http://brettterpstra.com" title="BrettTerpstra.com">BrettTerpstra.com</a> at <a href="http://brettterpstra.com/fk-a-useful-bash-function/">fk: a useful bash function</a></p>]]></content:encoded>
			<wfw:commentRss>http://brettterpstra.com/fk-a-useful-bash-function/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic (Feed is rejected)
Page Caching using disk: enhanced
Database Caching 8/44 queries in 0.103 seconds using xcache
Object Caching 1084/1138 objects using xcache
Content Delivery Network via cdn2.brettterpstra.com

Served from: brettterpstra.com @ 2012-02-09 17:45:10 -->
