title: Using ps and awk to get pid, then killing it
body: <p>I have written a very short script that gets pid of $1 then kills the process. I have a few questions / requests for improvement. (OSX)</p>
  
  <pre><code>#!/bin/bash
  
  getpid=$(ps aux -O start | grep -i "$1" | grep -v grep | grep -v $0 | awk '{ if (NR==1) {print $2}}')
  
  kill $getpid
  </code></pre>
  
  <p>For example:</p>
  
  <pre><code>$ ~/killpro.sh java
  </code></pre>
  
  <p>Which kills the most recent instance of java.</p>
  
  <p>Or:</p>
  
  <pre><code>$ ~/killpro.sh chrome
  </code></pre>
  
  <p>To kill Google Chrome, obviously.</p>
  
  <p>First, is there any practical way to get this strung together into a single line, using read -p perhaps, so that it can in turn be aliased and become something like:</p>
  
  <pre><code>$ killpro
  $ Enter name: chrome
  $
  </code></pre>
  
  <p>Not that it's significantly easier, in fact it's slightly more work, but it bypasses the need to type a path and shell extension which is desirable.</p>
  
  <p>Second, is there anything fundamentally wrong with my initial approach / am I missing something obvious that negates the need for any this?</p>
  
  <p>Any feedback is appreciated.  </p>
  
  <p>Thanks in advance, I'm (as you can tell) new to this.  </p>