<?xml version="1.0" encoding="iso-8859-15"?>
<!-- generator="Kukkaisvoima version 15" -->
<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/"
>
<channel>
<title>Tim's Bits and Pieces: Scripting</title>
<link>http://blog.timp.com.au</link>
<description>My personal blog, covering many random topics</description>
<pubDate>Tue, 05 May 2009 23:03:53 +0200</pubDate>
<lastBuildDate>Tue, 05 May 2009 23:03:53 +0200</lastBuildDate>
<generator>http://23.fi/kukkaisvoima/</generator>
<language>en</language>
<item>
<title>Awk and Trac.ini
</title>
<link>http://blog.timp.com.au/awk_and_tracini%3A2009-05-05%3AComputers%2CHowto%2CLinux%2CScripting</link>
<comments>http://blog.timp.com.au/awk_and_tracini%3A2009-05-05%3AComputers%2CHowto%2CLinux%2CScripting#comments</comments>
<pubDate>Tue, 05 May 2009 23:03:53 +0200</pubDate>
<dc:creator>TimP</dc:creator>
<category>Computers</category>
<category>Howto</category>
<category>Linux</category>
<category>Scripting</category>
<guid isPermaLink="false">http://blog.timp.com.au/awk_and_tracini%3A2009-05-05%3AComputers%2CHowto%2CLinux%2CScripting/</guid>
<description><![CDATA[ <p>You'll notice I'm posting late again, for which I blame <a
 [...]]]></description>
<content:encoded><![CDATA[
<p>You'll notice I'm posting late again, for which I blame <a
     href="http://en.wikipedia.org/wiki/Space_Trilogy">C.S. Lewis</a>, and since
 he's dead he can't defend himself from this <strike>unfounded accusation</strike>
 completely serious and non-trivial explanation for my tardiness.</p>
 
 <p>As for actually post, I work as a support guy for a Subversion
 hosting company. We also host <a href="http://trac.edgewall.org/">Trac</a>, so
 part of my job is occasionally setting up <a
     href="http://trac.edgewall.org/wiki/TracTicketsCustomFields">custom ticket
     fields</a> for customers. This involves editing the <a
     href="http://trac.edgewall.org/wiki/TracIni">trac.ini</a>, a simple text
 file. This is fairly easy when you just have to modify one Trac instance, but
 every now and again I have to modify a couple of dozen files. I have tried this
 using <a href="http://en.wikipedia.org/wiki/Sed"><code>sed</code></a> before, but this is a prime example of the the "When
 all you've got is a hammer, every problem is a nail" fallacy. I'd been sort of
 interested in learning more about <a
     href="http://en.wikipedia.org/wiki/AWK"><code>awk</code></a> and this seemed
 like a prime opportunity to "learn by doing", and now I'll reinforce with
 "learning by teaching". So with out further ado here's my short script
 interspersed with an explanation of how it works, and a complete copy of the
 code at the bottom.</p>
 
 <div class="code">
     <font color="#ff1493">BEGIN</font>&nbsp;{ i = <font
         color="#00cd00">0</font>&nbsp;}<br />
 </div>
 <p>The <code>BEGIN</code> section is a special section that is run once before
 processing the file, it can be used for setting variables, as I have done here.
 The <code>i</code> variable, a more descriptive name would have been good,
 stores the state of the script.</p>
 
 <div class="code">
     {<br />
     &nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff"><b>if</b></font>&nbsp;( i ==
     <font color="#00cd00">0</font>&nbsp;) { <font
         color="#0000ff"><b>print</b></font>&nbsp;}<br />
 </div>
 <p>The <code>{</code> starts the main <code>awk</code> script, which is run once
 for each line in the file being processed. This first line simply prints out the
 input line being examined if <code>i</code> equals zero, which it does in the
 initial state of the program.</p>
 
 <div class="code">
     &nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff"><b>else</b></font>&nbsp;<br />
     &nbsp;&nbsp;&nbsp;&nbsp;{<br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font
         color="#0000ff"><b>if</b></font>&nbsp;( i == <font
         color="#00cd00">1</font>&nbsp;) { <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font
         color="#0000ff"><b>print</b></font>&nbsp;<font color="#00cd00">&quot;#
         Ticket Custom Section</font><font color="#ff1493">\n</font><font
         color="#00cd00">&quot;</font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i =
     <font color="#00cd00">2</font>&nbsp;}<br />
 </div>
 <p>The next section is the <code>else</code> clause for the preceding
 <code>if</code> clause, which executes it's own if-else pair. The
 <code>if</code> clause executes if <code>i</code> equals one, which it does if
 we have just passed the "<code>[ticket-custom]</code>" line in the trac.ini
 file. This section prints out the required custom ticket definitions and changes
 <code>i</code> to two, which will prevent anything from being written until it
 gets to the end of the custom ticket section, see below. You need to change the
 <code>print</code> statement to print the the required ticket custom section of
 course; it might be more readable to use a series of individual statements
 rather than just one.</p>
 
 <div class="code">
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font
         color="#0000ff"><b>else</b></font>&nbsp;{ <font
         color="#0000ff"><b>if</b></font>&nbsp;( <font
         color="#ff1493">$0</font>&nbsp;~ <font color="#00cd00">/</font><font
         color="#ff1493">\[</font><font color="#ff1493">.*</font><font
         color="#ff1493">\]</font><font color="#00cd00">/</font>&nbsp;) { <font
         color="#0000ff"><b>print</b></font>&nbsp;<font
         color="#ff1493">;</font>&nbsp;i = <font color="#00cd00">0</font>&nbsp;}
     }<br />
     &nbsp;&nbsp;&nbsp;&nbsp;}<br />
 </div>
 <p>This next line is executed if <code>i</code> doesn't equal zero or one,
 meaning that we are currently in the custom field section. It's a single short
 <code>if</code> statement that checks if the line being processed
 (<code>$0</code>) matches (<code>~</code>) a regular expression, in this case
 <code>/\[.*\]/</code>. This will match any line that contains a "[" followed by
 a series of characters, followed by a "]"; basically a trac.ini section head. So
 if it finds the start of a new section in the file it will print it out, and
 then set <code>i</code> back to zero, which means it will just print out the
 rest of the lines in the file.</p>
 
 <div class="code">
     &nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff"><b>if</b></font>&nbsp;( <font
         color="#ff1493">$0</font>&nbsp;== <font
         color="#00cd00">&quot;[ticket-custom]&quot;</font>&nbsp;) { i = <font
         color="#00cd00">1</font>&nbsp;}<br />
     }<br />
 </div>
 <p>Then here's the final line which is a simple <code>if</code> that is run for each line
 in the file and checks if the line is the start of the custom field section
 (<code>[ticket-custom]</code>), and if so sets <code>i</code> to one; the result
 of which is show above.</p>
 
 <p>Since the code is probably slightly unclear unless you can see the whole
 thing here it is:</p>
  
 <div class="code">
     <font color="#ff1493">BEGIN</font>&nbsp;{ i = <font
         color="#00cd00">0</font>&nbsp;}<br />
     {<br />
     &nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff"><b>if</b></font>&nbsp;( i ==
     <font color="#00cd00">0</font>&nbsp;) { <font
         color="#0000ff"><b>print</b></font>&nbsp;}<br />
     &nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff"><b>else</b></font>&nbsp;<br />
     &nbsp;&nbsp;&nbsp;&nbsp;{<br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font
         color="#0000ff"><b>if</b></font>&nbsp;( i == <font
         color="#00cd00">1</font>&nbsp;) { <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font
         color="#0000ff"><b>print</b></font>&nbsp;<font color="#00cd00">&quot;#
         Ticket Custom Section</font><font color="#ff1493">\n</font><font
         color="#00cd00">&quot;</font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i =
     <font color="#00cd00">2</font>&nbsp;}<br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font
         color="#0000ff"><b>else</b></font>&nbsp;{ <font
         color="#0000ff"><b>if</b></font>&nbsp;( <font
         color="#ff1493">$0</font>&nbsp;~ <font color="#00cd00">/</font><font
         color="#ff1493">\[</font><font color="#ff1493">.*</font><font
         color="#ff1493">\]</font><font color="#00cd00">/</font>&nbsp;) { <font
         color="#0000ff"><b>print</b></font>&nbsp;<font
         color="#ff1493">;</font>&nbsp;i = <font color="#00cd00">0</font>&nbsp;}
     }<br />
     &nbsp;&nbsp;&nbsp;&nbsp;}<br />
     &nbsp;&nbsp;&nbsp;&nbsp;<br />
     &nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff"><b>if</b></font>&nbsp;( <font
         color="#ff1493">$0</font>&nbsp;== <font
         color="#00cd00">&quot;[ticket-custom]&quot;</font>&nbsp;) { i = <font
         color="#00cd00">1</font>&nbsp;}<br />
     }<br />
 </div>
 ]]></content:encoded>
<wfw:commentRss>http://blog.timp.com.au/awk_and_tracini%3A2009-05-05%3AComputers%2CHowto%2CLinux%2CScripting/feed/</wfw:commentRss>
</item>
<item>
<title>The Artificial Load Spike
</title>
<link>http://blog.timp.com.au/the_artificial_load_spike%3A2009-04-27%3AComputers%2COddities%2CScripting</link>
<comments>http://blog.timp.com.au/the_artificial_load_spike%3A2009-04-27%3AComputers%2COddities%2CScripting#comments</comments>
<pubDate>Mon, 27 Apr 2009 23:03:54 +0200</pubDate>
<dc:creator>TimP</dc:creator>
<category>Computers</category>
<category>Oddities</category>
<category>Scripting</category>
<guid isPermaLink="false">http://blog.timp.com.au/the_artificial_load_spike%3A2009-04-27%3AComputers%2COddities%2CScripting/</guid>
<description><![CDATA[ <p>Since I missed posting over the weekend I thought I'd share a little Linux
 [...]]]></description>
<content:encoded><![CDATA[
<p>Since I missed posting over the weekend I thought I'd share a little Linux
 one-liner that I found amusing. For those that don't know "load" in the Unix
 world often refers to the system load averages for one, five, or fifteen
 minutes. The system load average is the average number of processes in the
 "ready" state over the period in question.  Processes in the "ready" state are
 normally waiting for a chance to use the CPU, but it also includes processes
 waiting for <acronym title="Input/Output">I/O</acronym>, which the following
 one-liner takes advantage of to cause an artificial load spike:</p>
 
 <div class="code">
     for i in $(seq 1 10000) ; do (/usr/sbin/smartctl -A /dev/hda &amp;) ; done
 </div>
 
 <p>From the man page smartctl is a "Control and Monitor Utility for <acronym
     title="Self-Monitoring, Analysis, and Reporting Technology">SMART</acronym>
 Disks". The "-A" option tells it to print the vendor specific SMART attribute.
 It's best to run this on an optical drive rather than your main hard disk
 because if you run it on the hard disk your disk will end up getting thrashed
 for the next 45 minutes, making your system noticeably unresponsive. If you run
 it on an optical drive instead though it only takes a few minutes (on my
 relatively recent system; you may need to decrease the number of iterations),
 during which time the system still remains responsive, but your load average
 gets huge. I got it up to 870 load average over one minute. This means that by
 the traditional equation (utilization in percent = load average / number of
 cores * 100) my dual core system was running at about 43,500% utilization. So
 now I can say that Linux is still responsive at over forty <i>thousand</i>
 percent system load. ;-)</p>
 ]]></content:encoded>
<wfw:commentRss>http://blog.timp.com.au/the_artificial_load_spike%3A2009-04-27%3AComputers%2COddities%2CScripting/feed/</wfw:commentRss>
</item>
<item>
<title>Countdown in a Shell Script
</title>
<link>http://blog.timp.com.au/countdown_in_a_shell_script%3A2009-04-07%3AComputers%2CScripting%2CLinux</link>
<comments>http://blog.timp.com.au/countdown_in_a_shell_script%3A2009-04-07%3AComputers%2CScripting%2CLinux#comments</comments>
<pubDate>Tue, 07 Apr 2009 23:03:53 +0200</pubDate>
<dc:creator>TimP</dc:creator>
<category>Computers</category>
<category>Scripting</category>
<category>Linux</category>
<guid isPermaLink="false">http://blog.timp.com.au/countdown_in_a_shell_script%3A2009-04-07%3AComputers%2CScripting%2CLinux/</guid>
<description><![CDATA[ <p>Tuesday's still the <a
 [...]]]></description>
<content:encoded><![CDATA[
<p>Tuesday's still the <a
     href="/archives/2009/04/04/battery_state_monitor_script/index.html">weekend</a> right? ;-)</p>
 
 <p>Anyhow here's a little code snippet for inserting a count down into a shell
 script. Simply put this before whatever you want to delay, and then make sure
 that the first argument you pass to the script is the number of seconds to
 wait. An example of the sort of thing you might want to use this for is taking
 screenshots.</p>
 
 <div class="code">
     <font color="#ee0000">#!/bin/bash</font><br />
 
     <font color="#0000ff"><b>for </b></font>i <font color="#0000ff"><b>in</b></font>
         &nbsp;<font color="#cd00cd">$(</font><font color="#ff1493">seq </font>
         <font color="#cd00cd">$1</font><font color="#ff1493">&nbsp;</font>
         <font color="#00cd00">-1</font><font color="#ff1493">&nbsp;</font>
         <font color="#00cd00">1</font><font color="#cd00cd">)</font>; 
         <font color="#0000ff"><b>do</b></font><br />
     
     &nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff"><b>echo</b></font>
         <font color="#00cd00">&nbsp;</font><font color="#cd00cd">$i</font><br />
     
     &nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff"><b>sleep</b></font>
         &nbsp;<font color="#00cd00">1</font><br />
     
     <font color="#0000ff"><b>done</b></font><br />
     <font color="#0000ff"><b>echo</b></font><font color="#00cd00">&nbsp;0</font><br />
 
     <font color="#cd00cd">$2</font>
 </div>
 
 <p>For example if you run it like this: <code>./countdown.sh 5 "echo Blast
     Off"</code> you'll get the following output:</p>
 
 <div class="code">
     <b>$ ./countdown.sh 5 "echo Blast Off"</b><br />
     5<br />
     4<br />
     3<br />
     2<br />
     1<br />
     0<br />
     Blast Off<br />
     <b>$</b>
 </div>
 ]]></content:encoded>
<wfw:commentRss>http://blog.timp.com.au/countdown_in_a_shell_script%3A2009-04-07%3AComputers%2CScripting%2CLinux/feed/</wfw:commentRss>
</item>
<item>
<title>Battery State Monitor Script
</title>
<link>http://blog.timp.com.au/battery_state_monitor_script%3A2009-04-04%3AComputers%2CScripting%2CLinux</link>
<comments>http://blog.timp.com.au/battery_state_monitor_script%3A2009-04-04%3AComputers%2CScripting%2CLinux#comments</comments>
<pubDate>Sat, 04 Apr 2009 23:03:53 +0200</pubDate>
<dc:creator>TimP</dc:creator>
<category>Computers</category>
<category>Scripting</category>
<category>Linux</category>
<guid isPermaLink="false">http://blog.timp.com.au/battery_state_monitor_script%3A2009-04-04%3AComputers%2CScripting%2CLinux/</guid>
<description><![CDATA[ <p>Oops, completely missed posting last weekend, mostly just because I'm too
 [...]]]></description>
<content:encoded><![CDATA[
<p>Oops, completely missed posting last weekend, mostly just because I'm too
 easily distracted. Such is life, hopefully I'll do two or three posts this weekend
 to even it up.</p> 
 <p>This is a simple script I wrote to notify me whenever my battery is running
 low on power. Basically to use it you just create a new shell script with the
 following in it and then run it using <code>bash ~/bin/battery_monitor.sh &amp;</code>
 in your .xinitrc script. This should then check your battery status every minute
 and if it's below ten minutes remaining it will produce a warning message, after
 generating a warning it will wait five minutes before checking again to make
 sure it doesn't get really annoying.</p>
 <div class="code">
     <font color="#ee0000">#!/bin/bash</font><br />
     <font color="#ee0000"># Simple shell script that runs as a daemon to check the status of a Laptops</font><br />
     <font color="#ee0000"># battery and produce an error message every five minutes if it get's to</font><br />
     <font color="#ee0000"># less than 10 minutes of charge remaining and is not plugged in.</font><br />
     <br />
     <font color="#ee0000"># Copyright (c) 2009, Timothy Pollard</font><br />
     <font color="#ee0000"># </font><br />
     <font color="#ee0000"># Redistribution and use with or without modification, are permitted provided</font><br />
     <font color="#ee0000"># that the above Copyright notification remains complete.</font><br />
     <br />
     <font color="#ee0000"># The file that contains the state of the Battery</font><br />
     <font color="#008b8b">PROC_FILE</font>=/proc/acpi/battery/BAT1/state<br />
     <br />
     <font color="#0000ff"><b>while </b></font><font color="#0000ff"><b>true</b></font><font color="#0000ff"><b>;</b></font><font color="#0000ff"><b>&nbsp;</b></font><font color="#0000ff"><b>do</b></font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;<font color="#ee0000"># Get the charging state from the PROC_FILE, this should be one of &quot;charged&quot;,</font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;<font color="#ee0000"># &quot;charging&quot;, or &quot;discharging&quot;</font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b8b">CHARGING_STATE</font>=<font color="#cd00cd">$(</font><font color="#ff1493">cat </font><font color="#cd00cd">$PROC_FILE</font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>|</b></font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>grep</b></font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>&quot;</b></font><font color="#00cd00">^charging state:</font><font color="#0000ff"><b>&quot;</b></font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>|</b></font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>sed</b></font><font color="#ff1493">&nbsp;-e </font><font color="#0000ff"><b>&quot;</b></font><font color="#00cd00">s/charging state: *//g</font><font color="#0000ff"><b>&quot;</b></font><font color="#cd00cd">)</font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;<br />
     &nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff"><b>if </b></font><font color="#0000ff"><b>[</b></font>&nbsp;<font color="#cd00cd">$CHARGING_STATE</font>&nbsp;<font color="#0000ff"><b>==</b></font>&nbsp;<font color="#00cd00">&quot;discharging&quot;</font>&nbsp;<font color="#0000ff"><b>]</b></font><font color="#0000ff"><b>;</b></font>&nbsp;<font color="#0000ff"><b>then</b></font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ee0000"># Get the current rate and remaining capacity</font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b8b">CURRENT_RATE</font>=<font color="#cd00cd">$(</font><font color="#ff1493">cat </font><font color="#cd00cd">$PROC_FILE</font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>|</b></font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>grep</b></font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>&quot;</b></font><font color="#00cd00">^present rate:</font><font color="#0000ff"><b>&quot;</b></font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>|</b></font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>sed</b></font><font color="#ff1493">&nbsp;-e </font><font color="#0000ff"><b>&quot;</b></font><font color="#00cd00">s/present rate: *//g</font><font color="#0000ff"><b>&quot;</b></font><font color="#ff1493">&nbsp;-e </font><font color="#0000ff"><b>&quot;</b></font><font color="#00cd00">s/ mW//g</font><font color="#0000ff"><b>&quot;</b></font><font color="#cd00cd">)</font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b8b">REMAINING_CAPACITY</font>=<font color="#cd00cd">$(</font><font color="#ff1493">cat </font><font color="#cd00cd">$PROC_FILE</font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>|</b></font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>grep</b></font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>&quot;</b></font><font color="#00cd00">^remaining capacity:</font><font color="#0000ff"><b>&quot;</b></font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>|</b></font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>sed</b></font><font color="#ff1493">&nbsp;-e </font><font color="#0000ff"><b>&quot;</b></font><font color="#00cd00">s/remaining capacity: *//g</font><font color="#0000ff"><b>&quot;</b></font><font color="#ff1493">&nbsp;-e </font><font color="#0000ff"><b>&quot;</b></font><font color="#00cd00">s/ mWh//g</font><font color="#0000ff"><b>&quot;</b></font><font color="#cd00cd">)</font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;<br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ee0000"># Remaining battery life in minutes</font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#008b8b">REMAINING_TIME</font>=<font color="#cd00cd">$(</font><font color="#ff1493">echo </font><font color="#0000ff"><b>&quot;</b></font><font color="#00cd00">scale=4; </font><font color="#cd00cd">$REMAINING_CAPACITY</font><font color="#00cd00">/</font><font color="#cd00cd">$CURRENT_RATE</font><font color="#00cd00">*60</font><font color="#0000ff"><b>&quot;</b></font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>|</b></font><font color="#ff1493">&nbsp;bc </font><font color="#0000ff"><b>|</b></font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>sed</b></font><font color="#ff1493">&nbsp;-e </font><font color="#0000ff"><b>&quot;</b></font><font color="#00cd00">s/\..*//g</font><font color="#0000ff"><b>&quot;</b></font><font color="#cd00cd">)</font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;<br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff"><b>if </b></font><font color="#0000ff"><b>[</b></font>&nbsp;<font color="#cd00cd">$REMAINING_TIME</font>&nbsp;<font color="#0000ff"><b>-lt</b></font>&nbsp;<font color="#00cd00">10</font>&nbsp;<font color="#0000ff"><b>]</b></font><font color="#0000ff"><b>;</b></font>&nbsp;<font color="#0000ff"><b>then</b></font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xmessage -center <font color="#0000ff"><b>&quot;</b></font><font color="#00cd00">There are currently only </font><font color="#cd00cd">$REMAINING_TIME</font><font color="#00cd00">&nbsp;minutes of battery life remaining.</font><font color="#0000ff"><b>&quot;</b></font><br />
     <br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ee0000"># Sleep for 4 minutes (4*60=240), this combined with the default</font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ee0000"># sleep of 60 seconds assures that you are only alerted once every 5</font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#ee0000"># minutes</font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff"><b>sleep</b></font>&nbsp;<font color="#00cd00">240</font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff"><b>fi</b></font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff"><b>fi</b></font><br />
     <br />
     &nbsp;&nbsp;&nbsp;&nbsp;<font color="#ee0000"># Delay the next run for 60 seconds</font><br />
     &nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff"><b>sleep</b></font>&nbsp;<font color="#00cd00">60</font><br />
     <font color="#0000ff"><b>done</b></font><br />
 </div>
 ]]></content:encoded>
<wfw:commentRss>http://blog.timp.com.au/battery_state_monitor_script%3A2009-04-04%3AComputers%2CScripting%2CLinux/feed/</wfw:commentRss>
</item>
<item>
<title>Random Backgrounds Script
</title>
<link>http://blog.timp.com.au/random_backgrounds_script%3A2008-08-06%3ALinux%2CComputers%2CScripting</link>
<comments>http://blog.timp.com.au/random_backgrounds_script%3A2008-08-06%3ALinux%2CComputers%2CScripting#comments</comments>
<pubDate>Wed, 06 Aug 2008 23:03:54 +0200</pubDate>
<dc:creator>TimP</dc:creator>
<category>Linux</category>
<category>Computers</category>
<category>Scripting</category>
<guid isPermaLink="false">http://blog.timp.com.au/random_backgrounds_script%3A2008-08-06%3ALinux%2CComputers%2CScripting/</guid>
<description><![CDATA[ <p>I recently wrote this simple script to allow me to set a random image as my
 [...]]]></description>
<content:encoded><![CDATA[
<p>I recently wrote this simple script to allow me to set a random image as my
 background:</p>
 <blockquote class="code">
     <font color="#ee0000">#!/bin/bash</font><br>
     <font color="#ee0000"># Selects a random jpeg file in the ~/backgrounds/ directory (or it's</font><br>
     <font color="#ee0000"># sub-directories) and makes it the background image.</font><br>
     <font color="#008b8b">file_list</font>=<font color="#cd00cd">$(</font><font color="#0000ff"><b>find</b></font><font color="#ff1493">&nbsp;~/backgrounds/ -name </font><font color="#0000ff"><b>&quot;</b></font><font color="#00cd00">*.jpg</font><font color="#0000ff"><b>&quot;</b></font><font color="#cd00cd">)</font><br>
     <font color="#008b8b">num_files</font>=<font color="#cd00cd">$(</font><font color="#ff1493">echo </font><font color="#cd00cd">$file_list</font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>|</b></font><font color="#ff1493">&nbsp;wc -w</font><font color="#cd00cd">)</font><br>
     <font color="#008b8b">ran_line_number</font>=<font color="#cd00cd">$((</font><font color="#cd00cd">$RANDOM</font><font color="#ff1493">%</font><font color="#cd00cd">$num_files</font><font color="#ff1493">+</font><font color="#00cd00">1</font><font color="#cd00cd">))</font><br>
     <font color="#008b8b">selected_file</font>=<font color="#cd00cd">$(</font><font color="#ff1493">echo </font><font color="#cd00cd">$file_list</font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>|</b></font><font color="#ff1493">&nbsp;tr </font><font color="#0000ff"><b>&quot;</b></font><font color="#00cd00">&nbsp;</font><font color="#0000ff"><b>&quot;</b></font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>&quot;</b></font><font color="#ff1493">\n</font><font color="#0000ff"><b>&quot;</b></font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>|</b></font><font color="#ff1493">&nbsp;</font><font color="#0000ff"><b>sed</b></font><font color="#ff1493">&nbsp;-n </font><font color="#cd00cd">${</font><font color="#cd00cd">ran_line_number</font><font color="#cd00cd">}</font><font color="#ff1493">p</font><font color="#cd00cd">)</font><br>
     fbsetbg <font color="#cd00cd">$selected_file</font><br>
 </blockquote>
 <p>On most light-weight window managers, like <a
 href="http://blackboxwm.sourceforge.net/">Blackbox</a>, simply copy any images
 you want to use as backgrounds into ~/backgrounds/ and you're good to go. Some
 other Window Managers/Desktop environments look after the desktop background
 themselves so you would have to disable that in their configuration first. If
 you wanted to run this every half hour or something you could put it in your
 crontab, or else you can just set it up so it runs when you start your window
 manager.</p>
 <p class="post_edit">-- Edit 09/05/2009 --</p>
 <p>I've modified the code very slightly, to fix a bug which either didn't exist
 when run with the older version of bash/find I was have been using when I wrote
 this. I've also cleaned up the code a little.</p>
 ]]></content:encoded>
<wfw:commentRss>http://blog.timp.com.au/random_backgrounds_script%3A2008-08-06%3ALinux%2CComputers%2CScripting/feed/</wfw:commentRss>
</item>
<item>
<title>Browser Startup
</title>
<link>http://blog.timp.com.au/browser_startup%3A2008-03-19%3AHowto%2CComputers%2CScripting</link>
<comments>http://blog.timp.com.au/browser_startup%3A2008-03-19%3AHowto%2CComputers%2CScripting#comments</comments>
<pubDate>Wed, 19 Mar 2008 23:03:56 +0200</pubDate>
<dc:creator>TimP</dc:creator>
<category>Howto</category>
<category>Computers</category>
<category>Scripting</category>
<guid isPermaLink="false">http://blog.timp.com.au/browser_startup%3A2008-03-19%3AHowto%2CComputers%2CScripting/</guid>
<description><![CDATA[ <p>I've noticed an annoying little problem with a couple of open source web
 [...]]]></description>
<content:encoded><![CDATA[
<p>I've noticed an annoying little problem with a couple of open source web
 browsers namely <a href="http://www.seamonkey-project.org/">Seamonkey</a> and <a
 href="http://kazehakase.sourceforge.jp/">Kazehakase</a>. Both of them have the
 same problem when you run them the normal way, <acronym title="latin: illud est,
 english: that is">ie</acronym> by entering they're name on the command line,
 they fail to do anything if you already have the program open nothing happens.
 I'd prefer if they instead opened a new window, so I've written scripts to "fix"
 this. Basically each of the scripts checks to see if the program is already open
 and if so simply open a new window, otherwise they start the program. Both
 scripts work slightly differently but accomplish nearly the same task (the
 Kazehakase one will not open a link in the new window).</p>
 <p>First we have <code>seamonkey-start</code>:</p>
 <blockquote class="code">
         <font color="#0000ff">#!/bin/bash</font><br />
 
         <font color="#0000ff"># Simple start script for seamonkey</font><br />
 
         <font color="#0000ff"># Tries to open a new window with an already
             running instance, if it can't do</font><br />
 
         <font color="#0000ff"># that it starts a new seamonkey
             instance.</font><br />
 
         <font color="#a52a2a"><b>if</b></font>&nbsp;<font
             color="#a52a2a"><b>[</b></font>&nbsp;<font
             color="#a020f0">$#</font>&nbsp;<font
             color="#a52a2a"><b>==</b></font>&nbsp;<font
             color="#ff00ff">0</font>&nbsp;<font
             color="#a52a2a"><b>]</b></font><font
             color="#a52a2a"><b>;</b></font>&nbsp;<font
             color="#a52a2a"><b>then</b></font><br />
 
         &nbsp;&nbsp;&nbsp;&nbsp;<font
             color="#a52a2a"><b>if</b></font>&nbsp;<font
             color="#a52a2a"><b>!</b></font>&nbsp;seamonkey -remote <font
             color="#a52a2a"><b>&quot;</b></font><font
             color="#ff00ff">xfeDoCommand(openBrowser)</font><font
             color="#a52a2a"><b>&quot;;</b></font>&nbsp;<font
             color="#a52a2a"><b>then</b></font><br />
 
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;seamonkey -splash<font
             color="#a52a2a"><b>;</b></font><br />
 
         &nbsp;&nbsp;&nbsp;&nbsp;<font color="#a52a2a"><b>fi</b></font><br />
 
         <font color="#a52a2a"><b>else</b></font><br />
 
         &nbsp;&nbsp;&nbsp;&nbsp;<font
             color="#a52a2a"><b>if</b></font>&nbsp;<font
             color="#a52a2a"><b>!</b></font>&nbsp;seamonkey -remote <font
             color="#a52a2a"><b>&quot;</b></font><font
             color="#ff00ff">openURL(</font><font color="#a020f0">$1</font><font
             color="#ff00ff">, new-window)</font><font
             color="#a52a2a"><b>&quot;;</b></font>&nbsp;<font
             color="#a52a2a"><b>then</b></font><br />
 
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;seamonkey <font
             color="#a020f0">$1</font><font color="#a52a2a"><b>;</b></font><br />
 
         &nbsp;&nbsp;&nbsp;&nbsp;<font color="#a52a2a"><b>fi</b></font><br />
 
         <font color="#a52a2a"><b>fi</b></font><br />
 </blockquote>
 
 <p>And next up the <code>kazehakase-start</code>:</p>
 <blockquote class="code">
         <font color="#0000ff">#!/bin/bash</font><br />
 
         <font color="#0000ff"># Simple start script for Kazehakase</font><br />
 
         <font color="#0000ff"># Checks for an already running instance, if so opens a
             new window, else starts</font><br />
 
         <font color="#0000ff"># Kazehakase.</font><br />
 
         <font color="#a52a2a"><b>if</b></font>&nbsp;<font
             color="#6a5acd">[[</font>&nbsp;<font color="#6a5acd">`ps -e </font><font
             color="#a52a2a"><b>|</b></font><font color="#6a5acd">&nbsp;</font><font
             color="#a52a2a"><b>grep</b></font><font color="#6a5acd">&nbsp;kazehakase
             </font><font color="#a52a2a"><b>|</b></font><font
             color="#6a5acd">&nbsp;</font><font color="#a52a2a"><b>grep</b></font><font
             color="#6a5acd">&nbsp;-v kazehakase-star`</font>&nbsp;<font
             color="#a52a2a"><b>!=</b></font>&nbsp;<font
             color="#a52a2a"><b>&quot;&quot;</b></font>&nbsp;<font
             color="#6a5acd">]]</font><font color="#a52a2a"><b>;</b></font>&nbsp;<font
             color="#a52a2a"><b>then</b></font><br />
 
         &nbsp;&nbsp;&nbsp;&nbsp;kazehakase --action <font
             color="#a52a2a"><b>&quot;</b></font><font
             color="#ff00ff">KzWindow/NewWindow</font><font
             color="#a52a2a"><b>&quot;;</b></font><br />
 
         <font color="#a52a2a"><b>else</b></font><br />
 
         &nbsp;&nbsp;&nbsp;&nbsp;kazehakase <font color="#a020f0">$1</font><font
             color="#a52a2a"><b>;</b></font><br />
 
         <font color="#a52a2a"><b>fi</b></font><br />
 </blockquote>
 
 <p>I'm placing both scripts in the public domain so your welcome to do what ever
 you like with them. If you want to use them on your own computer just copy and
 paste.</p>
 ]]></content:encoded>
<wfw:commentRss>http://blog.timp.com.au/browser_startup%3A2008-03-19%3AHowto%2CComputers%2CScripting/feed/</wfw:commentRss>
</item>
</channel>
</rss>
