<?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>dopefish.de &#187; Programming</title>
	<atom:link href="http://www.dopefish.de/archives/category/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://www.dopefish.de</link>
	<description>Headquaters of the evil genius</description>
	<lastBuildDate>Mon, 23 Jan 2012 13:34:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>How to increase Fraps performance with a ramdisk</title>
		<link>http://www.dopefish.de/archives/1065</link>
		<comments>http://www.dopefish.de/archives/1065#comments</comments>
		<pubDate>Thu, 10 Nov 2011 02:30:54 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Gaming]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[battlefield 3]]></category>
		<category><![CDATA[fraps]]></category>
		<category><![CDATA[IO]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[ramdisk]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=1065</guid>
		<description><![CDATA[I recently started playing Battlefield 3 and remembered that I have a Fraps license so I installed it and started recording some stuff. Unsurprisingly the performance made a big dip when I recorded. A glance at my PC told me the harddrive was at fault, probably bringing the whole system down due to IO.
Since my PC has [...]]]></description>
			<content:encoded><![CDATA[<p>I recently started playing <a title="Battlefield 3" href="http://www.battlefield.com/battlefield3" target="_blank">Battlefield 3</a> and remembered that I have a <a title="Fraps" href="http://fraps.com/" target="_blank">Fraps</a> license so I installed it and started recording some stuff. Unsurprisingly the performance made a big dip when I recorded. A glance at my PC told me the harddrive was at fault, probably bringing the whole system down due to IO.</p>
<p>Since my PC has more than enough RAM I decided to set up a 5Gb <a href="http://www.ltr-data.se/opencode.html/#ImDisk" target="_blank">Ramdisk</a> to see if that helped. It did, when writing the video files to the ramdisk I hardly had any performance hit. Unfortunately 5GB isn&#8217;t going to last long while recording 1920&#215;1080 @ 40FPS (a few minutes footage at most).</p>
<p>Here is my little cmd file to create a 5GB ramdisk as drive J: and format it for usage:</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="winbatch" style="font-family:monospace;">imdisk <span style="color: #66cc66;">-</span>d <span style="color: #66cc66;">-</span>m J<span style="color: #FF1010; font-weight: bold;">:</span>
imdisk <span style="color: #66cc66;">-</span>a <span style="color: #66cc66;">-</span>t vm <span style="color: #66cc66;">-</span>s 5G <span style="color: #66cc66;">-</span>o rw <span style="color: #66cc66;">-</span>m J<span style="color: #FF1010; font-weight: bold;">:</span>
format J<span style="color: #FF1010; font-weight: bold;">: /A:64k /V:Ramdisk /FS:NTFS /Q</span></pre></td></tr></table></div></div>

<p>So my next thought was to see if I could write a script to move files off the ramdisk when they were done being written to by Fraps. This obviously was going to cause IO load &#8230; the reason we were having performance issues in the first place, so I was skeptical about if this was going to help any. Especially since I also had to move the files away quick enough so that the drive wouldn&#8217;t fill up completely with the next file Fraps was writing. I wrote a little <a href="http://en.wikipedia.org/wiki/Windows_PowerShell" target="_blank">powershell</a> script for this (yeah, a *nix Sysadmin writing scripts in powershell &#8230;)</p>
<p>Here is my little powershell script to copy the finished files from my ramdisk to a normal HDD (please excuse  possible ugliness, I&#8217;m a powershell noob):</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$source</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;j:\*.avi&quot;</span>
<span style="color: #800080;">$target_dir</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;f:\fraps\movies\&quot;</span>
&nbsp;
<span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #800080;">$files</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">gci</span> <span style="color: #800080;">$source</span>
    <span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$file</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$files</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> 
        <span style="color: #800080;">$srcfile</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">echo</span> <span style="color: #800080;">$file</span>.fullname
        <span style="color: #800080;">$dstfile</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">echo</span> <span style="color: #800080;">$file</span>.basename
        <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$srcfile</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            try <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-weight: bold;">move-item</span> <span style="color: #800000;">&quot;$srcfile&quot;</span> <span style="color: #800000;">&quot;$target_dir$dstfile.avi&quot;</span> <span style="color: pink;">-</span>ea stop
                <span style="color: #800000;">&quot;$srcfile Moved&quot;</span>
            <span style="color: #000000;">&#125;</span>
            catch <span style="color: #000000;">&#123;</span>
                <span style="color: #800000;">&quot;$srcfile in use, cannot be copied right now&quot;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #008080; font-weight: bold;">sleep</span> <span style="color: #804000;">5</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div></div>

<p>The last little problem I noticed is that the 5GB ramdrive wasn&#8217;t big enough (Fraps seems to create some dummy files and fills them up). Forcing Fraps to make smaller files by toggeling the recording fixed that though -&gt; pressing F9 twice fast will drop a few frames though. I used my Logitech G13 for that, just had a key mapped to press F9 quickly every 60 seconds. The shortest gap I could get working reliably is 50ms.</p>
<p><img class="aligncenter size-full wp-image-1073" title="fraps settings" src="http://www.dopefish.de/wp-content/uploads/2011/11/20111110_fraps.jpg" alt="" width="400" height="533" /></p>
<p>This all probably sounds awfully complicated, but it works and solves my problem. Fraps is great software, but it would be immensly helpful if you could set the file size in the settings (instead of it defaulting to 4GB). Or, even better, if Fraps could rework their IO system to work more efficiently.</p>
<p>So to sum everything up:<br />
- create ramdrive<br />
- start script that copies files from the ramdrive to a normal HDD<br />
- set fraps to store videos on the ramdrive<br />
- start game, press F9 to start recording and then press the G13 key to toggle the F9 periodically</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/1065/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to build an efficient GeoIP SQL table</title>
		<link>http://www.dopefish.de/archives/1037</link>
		<comments>http://www.dopefish.de/archives/1037#comments</comments>
		<pubDate>Mon, 22 Aug 2011 19:17:48 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Internet Stuff]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[GeoIP]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=1037</guid>
		<description><![CDATA[This here is a very handy little script I threw together to generate a geoip.sql table for quickly determining which country a IP is from. I already hear you saying &#8220;Just convert the IP to an INT and use BETWEEN, how hard can it be&#8221;. And you are right, that works. And it may even [...]]]></description>
			<content:encoded><![CDATA[<p>This here is a very handy little script I threw together to generate a geoip.sql table for quickly determining which country a IP is from. I already hear you saying &#8220;Just convert the IP to an INT and use BETWEEN, how hard can it be&#8221;. And you are right, that works. And it may even be your easiest solution, but it just isn&#8217;t fast. And if you are planning on hammering the table with thousands of queries you are going to end up looking for something fast.</p>
<p>A while back I found a very interesting posting at www.<a href="http://jcole.us/blog/archives/2007/11/24/on-efficiently-geo-referencing-ips-with-maxmind-geoip-and-mysql-gis/" target="_blank">jcole.us</a>&nbsp;that described how to use <a href="http://dev.mysql.com/doc/refman/5.0/en/creating-spatial-indexes.html" target="_blank">Spacial Indexes</a>&nbsp;together with <a href="http://dev.mysql.com/doc/refman/5.0/en/spatial-extensions.html" target="_blank">MySQL&#8217;s GIS</a> to speed up the queries. The posting has been online for a while and both it and the replies are worth reading.</p>
<p>All I did was make a small bash script to download the current &#8220;lite&#8221; version of GeoIP CSV file from maxmind.com, use the information from the posting to throw/transform it into a local database table and dump out a .sql file that can be easily imported into any other database. The script isn&#8217;t failproof though, it expects your user to be able to use mysql and have permission to create databases/tables and &#8220;load data local infile&#8221;.</p>
<p><strong><a href="http://dopefish.de/bash/generate_geoip_sql.sh" target="_blank">generate_geoip_sql.sh</a></strong></p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;">#===============================================================================</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#          FILE: generate_geoip_sql.sh</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#         USAGE: ./generate_geoip_sql.sh</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#   DESCRIPTION: Generates a optimized sql dump with geoipdata from maxmind.com</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#         USAGE: import sql into a database</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#  REQUIREMENTS: mysql database rights to create databases and LOAD DATA LOCAL INFILE</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#       EXAMPLE:</span>
<span style="color: #666666; font-style: italic;">#                 SELECT country_code,country_name</span>
<span style="color: #666666; font-style: italic;">#                 FROM   geo_ip</span>
<span style="color: #666666; font-style: italic;">#                 WHERE  MBRCONTAINS(ip_poly, POINTFROMWKB(POINT(INET_ATON('1.2.3.4'), 0)))</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#                 SELECT country_code,country_name</span>
<span style="color: #666666; font-style: italic;">#                 FROM   geo_ip</span>
<span style="color: #666666; font-style: italic;">#                 WHERE  INET_ATON('1.2.3.4') </span>
<span style="color: #666666; font-style: italic;">#                 BETWEEN ip_from AND ip_to;</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#        AUTHOR:  Ryan Schulze (rs), ryan@dopefish.de</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#===============================================================================</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">set</span> <span style="color: #660033;">-o</span> nounset                              <span style="color: #666666; font-style: italic;"># Treat unset variables as an error</span>
&nbsp;
<span style="color: #007800;">CSVFile</span>=<span style="color: #ff0000;">&quot;GeoIPCountryWhois.csv&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-e</span> <span style="color: #800000;">${CSVFile}</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
  <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-en</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #007800;">${CSVFile}</span> already exists, download a newer version? [Y/n]: &quot;</span>
  <span style="color: #c20cb9; font-weight: bold;">read</span> answer
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(echo $answer|tr &quot;N&quot; &quot;n&quot;)</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;n&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
  <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #800000;">${CSVFile}</span>
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-e</span> <span style="color: #800000;">${CSVFile}</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #007800;">tmpfile</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">mktemp</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #660033;">-O</span> <span style="color: #800000;">${tmpfile}</span> http:<span style="color: #000000; font-weight: bold;">//</span>geolite.maxmind.com<span style="color: #000000; font-weight: bold;">/</span>download<span style="color: #000000; font-weight: bold;">/</span>geoip<span style="color: #000000; font-weight: bold;">/</span>database<span style="color: #000000; font-weight: bold;">/</span>GeoIPCountryCSV.zip
  <span style="color: #c20cb9; font-weight: bold;">unzip</span> <span style="color: #800000;">${tmpfile}</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #800000;">${tmpfile}</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>generating geoip.sql&quot;</span>
&nbsp;
mysql <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;create database if not exists temp&quot;</span>
mysql <span style="color: #660033;">-D</span> temp <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;drop table if exists geo_ip&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'
CREATE TABLE geo_ip
(
  id           INT UNSIGNED  NOT NULL auto_increment,
  ip_poly      POLYGON       NOT NULL,
  ip_from      INT UNSIGNED  NOT NULL,
  ip_to        INT UNSIGNED  NOT NULL,
  country_code CHAR(2)       NOT NULL,
  country_name CHAR(50)      NOT NULL,
  PRIMARY KEY (id),
  SPATIAL INDEX (ip_poly)
);
LOAD DATA LOCAL INFILE &quot;GeoIPCountryWhois.csv&quot;
INTO TABLE geo_ip
FIELDS
  TERMINATED BY &quot;,&quot;
  ENCLOSED BY &quot;\&quot;&quot;
LINES
  TERMINATED BY &quot;\n&quot;
(
  @ip_from_string, @ip_to_string,
  @ip_from, @ip_to,
  @country_code, @country_string
)
SET
  id      := NULL,
  ip_from := @ip_from,
  ip_to   := @ip_to,
  ip_poly := GEOMFROMWKB(POLYGON(LINESTRING(
    /* clockwise, 4 points and back to 0 */
    POINT(@ip_from, -1), /* 0, top left */
    POINT(@ip_to,   -1), /* 1, top right */
    POINT(@ip_to,    1), /* 2, bottom right */
    POINT(@ip_from,  1), /* 3, bottom left */
    POINT(@ip_from, -1)  /* 0, back to start */
  ))),
  country_code := @country_code,
  country_name := @country_string
;
'</span> <span style="color: #000000; font-weight: bold;">|</span> mysql <span style="color: #660033;">-D</span> temp
&nbsp;
mysqldump <span style="color: #660033;">--opt</span> temp geo_ip <span style="color: #000000; font-weight: bold;">&gt;</span> geoip.sql
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>done generating geoip.sql&quot;</span></pre></td></tr></table></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/1037/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to easily add colored text output in bash scripts</title>
		<link>http://www.dopefish.de/archives/969</link>
		<comments>http://www.dopefish.de/archives/969#comments</comments>
		<pubDate>Tue, 31 May 2011 22:29:36 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Internet Stuff]]></category>
		<category><![CDATA[Me]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[color]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=969</guid>
		<description><![CDATA[Here is small snippet that can give your shell scripts some nice output: colortext.sh As with the debug.sh script, just download it to the same directory as your own script and add it with

Select All Code:. colortext.sh

It contains one simple function called text with the syntax text &#60;color&#62; &#8220;text to be output&#8221;. Color can be red, [...]]]></description>
			<content:encoded><![CDATA[<p>Here is small snippet that can give your shell scripts some nice output: <a href="http://www.dopefish.de/files/colortext.sh" target="_blank">colortext.sh</a> As with the <a href="http://www.dopefish.de/archives/873" target="_blank">debug.sh</a> script, just download it to the same directory as your own script and add it with</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">. colortext.sh</pre></div></div></div>

<p>It contains one simple function called <span style="color: #0000ff;">text</span> with the syntax <span style="color: #0000ff;">text &lt;color&gt; &#8220;text to be output&#8221;</span>. Color can be red, green, yellow, blue or grey. The function does not automatically add a linebreak to the putput, so pop a <span style="color: #0000ff;">\n</span> in there if you need it. I prefer using it together with printf for clean and easy color output.</p>
<p>Here are some examples of how the function can be used, and below the corresponding output:</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">. colortext.sh
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;normal text&quot;</span>
text blue <span style="color: #ff0000;">&quot;blue text, &quot;</span>
text yellow <span style="color: #ff0000;">&quot;yellow text<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Using it together with echo</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Status of script: [&quot;</span>;text <span style="color: #c20cb9; font-weight: bold;">red</span> <span style="color: #ff0000;">&quot;ERROR&quot;</span>;<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;]&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># more elegant usage with printf</span>
<span style="color: #7a0874; font-weight: bold;">printf</span> <span style="color: #ff0000;">&quot;Status of script: [%s]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span>text green <span style="color: #ff0000;">&quot;OK&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div></div>

<p>Output:</p>
<blockquote><p>normal text<br />
<span style="color: #0000ff;">blue text</span>, <span style="color: #808000;">yellow text<br />
</span> Status of script: [<span style="color: #ff0000;">ERROR</span>]<br />
Status of script: [<span style="color: #339966;">OK</span>]</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/969/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to add debugging to shellscripts</title>
		<link>http://www.dopefish.de/archives/873</link>
		<comments>http://www.dopefish.de/archives/873#comments</comments>
		<pubDate>Wed, 04 May 2011 21:44:13 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[debug]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=873</guid>
		<description><![CDATA[Debugging bash scripts is pretty straightforward, throwing around a couple echo and set -x quickly gives you what you need. But what if you want to add a nice breakpoint,  debugging to lots of paces in the code or turn all debugging on or off at once? Then this little script I wrote is the [...]]]></description>
			<content:encoded><![CDATA[<p>Debugging bash scripts is pretty straightforward, throwing around a couple <span style="color: #3366ff;">echo</span> and <span style="color: #3366ff;">set -x</span> quickly gives you what you need. But what if you want to add a nice breakpoint,  debugging to lots of paces in the code or turn all debugging on or off at once? Then this little script I wrote is the right thing for you: <a href="http://www.dopefish.de/files/debug.sh" target="_blank">debug.sh</a> just download it to the same directory as your script and include it with the following line:</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">. debug.sh</pre></div></div></div>

<p>It contains 4 simple functions that will make your bash coding easier.<br />
<span style="color: #3366ff;">debug</span> and <span style="color: #3366ff;">breakpoint</span> both print the argument with a timestamp to STDERR<br />
You can turn off all the functions by adding a <span style="color: #3366ff;">DEBUG=false</span> into your code</p>
<p>Example:</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
. debug.sh
&nbsp;
debug_on
<span style="color: #7a0874; font-weight: bold;">echo</span> foo
debug_off
&nbsp;
debug <span style="color: #ff0000;">&quot;I am a test&quot;</span>
&nbsp;
breakpoint <span style="color: #ff0000;">&quot;wait for a bit&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000; font-weight: bold;">done</span></pre></div></div></div>

<p>Output:</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">+ <span style="color: #7a0874; font-weight: bold;">echo</span> foo
foo
+ debug_off
+ <span style="color: #c20cb9; font-weight: bold;">true</span>
+ <span style="color: #000000; font-weight: bold;">set</span> +x
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">16</span>:<span style="color: #000000;">37</span>:<span style="color: #000000;">47</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> I am a <span style="color: #7a0874; font-weight: bold;">test</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">16</span>:<span style="color: #000000;">37</span>:<span style="color: #000000;">47</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #660033;">-breakpoint-</span> <span style="color: #7a0874; font-weight: bold;">wait</span> <span style="color: #000000; font-weight: bold;">for</span> a bit
press any key to <span style="color: #7a0874; font-weight: bold;">continue</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/873/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Disemvoweling</title>
		<link>http://www.dopefish.de/archives/864</link>
		<comments>http://www.dopefish.de/archives/864#comments</comments>
		<pubDate>Mon, 02 May 2011 19:21:06 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Internet Stuff]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[disemvoweling]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=864</guid>
		<description><![CDATA[Talk about weird words &#8230; ok, according to Wikipedia disemvoweling is the term for replacing or removing vowels from words. Commonly used as a tool for moderating.  I&#8217;m pretty sure everyone has run across  certain disemvoweled  words on the internet like f*ck or sh*t. Anyway I went and made a pure html/javascript page that does just that, [...]]]></description>
			<content:encoded><![CDATA[<p>Talk about weird words &#8230; ok, according to <a href="http://en.wikipedia.org/wiki/Disemvoweling" target="_blank">Wikipedia</a> disemvoweling is the term for replacing or removing vowels from words. Commonly used as a tool for moderating.  I&#8217;m pretty sure everyone has run across  certain disemvoweled  words on the internet like f*ck or sh*t. Anyway I went and made a pure html/javascript page that does just that, removes any vowels from an inputted text. The usefullness can certainly be argued, it was more for me to brush up on my javascript and css skills.</p>
<p><a href="http://www.dopefish.de/projects/disemvoweling/" target="_blank">http://www.dopefish.de/projects/disemvoweling/</a></p>
<p><a href="http://www.dopefish.de/projects/disemvoweling/" target="_blank"><img class="size-full wp-image-867 alignnone" title="disemvoweling" src="http://www.dopefish.de/wp-content/uploads/2011/05/20110502_disemvoweling.jpg" alt="" width="300" height="176" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/864/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>captcha cracking</title>
		<link>http://www.dopefish.de/archives/535</link>
		<comments>http://www.dopefish.de/archives/535#comments</comments>
		<pubDate>Fri, 18 Mar 2011 14:54:33 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Internet Stuff]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[captcha]]></category>
		<category><![CDATA[gocr]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=535</guid>
		<description><![CDATA[This is a pretty old posting from 2009 I just recently discovered in my &#8220;drafts&#8221; directory. Nowadays there are probably easier and more elegant ways of defeating a captcha, but for old times sake, here is my simple approach.
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;
Eclectic and Marko were so kind as to &#8220;provide&#8221; me a captcha to play around with. Took [...]]]></description>
			<content:encoded><![CDATA[<p>This is a pretty old posting from 2009 I just recently discovered in my &#8220;drafts&#8221; directory. Nowadays there are probably easier and more elegant ways of defeating a captcha, but for old times sake, here is my simple approach.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>Eclectic and Marko were so kind as to &#8220;provide&#8221; me a captcha to play around with. Took me a few days of poking around and googling but in the end it was easier than I had thought. As long as there aren&#8217;t and logic errors in the code (e.g. bad or no session handling) you probably won&#8217;t get around some kind of <a href="http://en.wikipedia.org/wiki/Optical_character_recognition" target="_blank">OCR</a>. As OCR software I decided to use <a href="http://jocr.sourceforge.net/" target="_blank">gocr</a> because it is free, runs under linux, and it is fairly easy to train to specific needs. Because I knew which libraries were being used to create the captcha images, it was possible for me to build a testing area. This just speeds things up a bit, the process would have worked just as well off the original website. First off: the spambot in action -&gt; <a href="http://captcha.dopefish.de/spambot.php" target="_blank">http://captcha.dopefish.de/spambot.php</a>, and the website it accesses: <a href="http://captcha.dopefish.de/" target="_blank">http://captcha.dopefish.de/</a></p>
<p>Now I&#8217;ll describe the steps I took to defeat the captcha.  Look at what happens on failed and successful inputs, first write a script that works if you enter the solution manually. I used the following 2 php functions for getting and posting stuff (and keeping the session intact)</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> get_url<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$timeout</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">urldecode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$cookie</span> <span style="color: #339933;">=</span> <span style="color: #990000;">tempnam</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/tmp&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;CURLCOOKIE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_USERAGENT<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$url</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_COOKIEFILE<span style="color: #339933;">,</span> <span style="color: #000088;">$cookie</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_COOKIEJAR<span style="color: #339933;">,</span> <span style="color: #000088;">$cookie</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_FOLLOWLOCATION<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_ENCODING<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_AUTOREFERER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_SSL_VERIFYPEER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;"># required for https urls
</span>    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_CONNECTTIMEOUT<span style="color: #339933;">,</span> <span style="color: #000088;">$timeout</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_TIMEOUT<span style="color: #339933;">,</span> <span style="color: #000088;">$timeout</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_MAXREDIRS<span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_getinfo</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$content</span><span style="color: #339933;">,</span> <span style="color: #000088;">$response</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> post_url<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #339933;">,</span>  <span style="color: #000088;">$postvars</span><span style="color: #339933;">,</span> <span style="color: #000088;">$timeout</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">urldecode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$cookie</span> <span style="color: #339933;">=</span> <span style="color: #990000;">tempnam</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/tmp&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;CURLCOOKIE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POST <span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS <span style="color: #339933;">,</span> <span style="color: #000088;">$postvars</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_USERAGENT<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$url</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_COOKIEFILE<span style="color: #339933;">,</span> <span style="color: #000088;">$cookie</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_COOKIEJAR<span style="color: #339933;">,</span> <span style="color: #000088;">$cookie</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_FOLLOWLOCATION<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_ENCODING<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_AUTOREFERER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_SSL_VERIFYPEER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;"># required for https urls
</span>    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_CONNECTTIMEOUT<span style="color: #339933;">,</span> <span style="color: #000088;">$timeout</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_TIMEOUT<span style="color: #339933;">,</span> <span style="color: #000088;">$timeout</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_MAXREDIRS<span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_getinfo</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$content</span><span style="color: #339933;">,</span> <span style="color: #000088;">$response</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div></div>

<p>Now train a gocr database for the images. Obviously it get&#8217;s better the more you train it.<br />
Since curl is taking care of  session handling, we can use the get_url() function for downloading the captcha image. I pipe it through this shell command to make it easier for gocr to read:</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">convert \
<span style="color: #660033;">-quality</span> <span style="color: #000000;">100</span> \
<span style="color: #660033;">-blur</span> <span style="color: #000000;">0.7</span> \
<span style="color: #660033;">-level</span> <span style="color: #000000;">9</span><span style="color: #000000; font-weight: bold;">%</span>,<span style="color: #000000;">60</span><span style="color: #000000; font-weight: bold;">%</span>,<span style="color: #000000;">100.0</span> \
<span style="color: #660033;">-monochrome</span> \
<span style="color: #660033;">-negate</span> \
<span style="color: #660033;">-blur</span> <span style="color: #000000;">0.1</span> \
<span style="color: #660033;">-gamma</span> <span style="color: #000000;">1</span> \
<span style="color: #660033;">-depth</span> <span style="color: #000000;">8</span> \
<span style="color: #660033;">-compress</span> none \</pre></td></tr></table></div></div>

<p>It turnes this:<br />
<img class="alignnone size-full wp-image-837" title="src" src="http://www.dopefish.de/wp-content/uploads/2011/03/20110318_src.jpg" alt="" width="430" height="50" /></p>
<p>into this:<br />
<img class="alignnone size-full wp-image-838" title="ocr" src="http://www.dopefish.de/wp-content/uploads/2011/03/20110318_ocr.jpg" alt="" width="430" height="50" /></p>
<p>Since the valid captcha result is always the same length, we can check if gocr matched all the chars. If it looks good we can use post_url() to continue our session and throw all the fields at the form and submit it. See, wasn&#8217;t that hard. Most of the time is spent training gocr and converting the image into something easier to read. It doesn&#8217;t solve 100% of the images, more like 80-90%, but still better than nothing <img src='http://www.dopefish.de/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/535/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>bash: using the content of a variable as variable name</title>
		<link>http://www.dopefish.de/archives/685</link>
		<comments>http://www.dopefish.de/archives/685#comments</comments>
		<pubDate>Thu, 05 Aug 2010 11:14:21 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=685</guid>
		<description><![CDATA[Since the implementation of Arrays in Bash is somewhat lacking compared to higher level programming languages (only one-dimensional), and hash lists require a bit of work to set up, you may run into a situation where you have a small list of key/value pairs that are both variable and you need to store.
There are various [...]]]></description>
			<content:encoded><![CDATA[<p>Since the implementation of <a href="http://tldp.org/LDP/abs/html/arrays.html#ARRAYREF">Arrays</a> in Bash is somewhat lacking compared to higher level programming languages (only one-dimensional), and hash lists require <a href="http://tldp.org/LDP/abs/html/contributed-scripts.html#HASHEX2">a bit of work</a> to set up, you may run into a situation where you have a small list of key/value pairs that are both variable and you need to store.<br />
There are various solutions for the problem, e.g. creating two arrays (one for the keys, one for the values, and combining them by using the same index values for the entries), or using the functions from the link above to build a hash list. For me the easiest way to solve the problem, if I only have a few variables and don&#8217;t want to bloat the code, is to (mis)use <strong><a href="http://tldp.org/LDP/abs/html/declareref.html">declare</a></strong>. declare is intended for setting the type of a variable (constant, array, integer,&#8230;), but has the nice side affect that you can use variables in the key name, and you can set the value of the variable.</p>
<p>usage:<br />
<code lang="bash">declare ${Key}=${Value}</code></p>
<p>Example:<br />
$File_Config is variable holding the name of a configfile, the content of the file could look like this:<br />
<code lang="text">foo:one<br />
bar:three<br />
foobar:bignumber</code></p>
<p>Snippet:<br />
<span style="font-family: monospace;"></p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${File_Config}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> line
<span style="color: #000000; font-weight: bold;">do</span>
<span style="color: #007800;">Key</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${line%%:*}</span>&quot;</span> <span style="color: #666666; font-style: italic;"># chop off everything after the first :</span>
<span style="color: #007800;">Value</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${line#*:}</span>&quot;</span> <span style="color: #666666; font-style: italic;"># chop off everything before the first :</span>
<span style="color: #007800;">Value</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$(echo ${Value}|sed 's#^ +##g;s# +$##g')</span>&quot;</span> <span style="color: #666666; font-style: italic;"># chop off any excess whitespace</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${Key}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${Value}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #666666; font-style: italic;"># only continue if we have something to do</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #7a0874; font-weight: bold;">declare</span> Configuration_<span style="color: #800000;">${Key}</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${Value}</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${File_Config}</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></td></tr></table></div></div>

<p></span></p>
<p>after the snippet has read the configfile, you can use <em>$Configuration_foo</em>, <em>$Configuration_bar</em> and <em>$Configuration_foobar</em> in your script. The keynames could also have came from a mysql query, array, command line args, &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/685/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Farmville</title>
		<link>http://www.dopefish.de/archives/562</link>
		<comments>http://www.dopefish.de/archives/562#comments</comments>
		<pubDate>Wed, 25 Nov 2009 10:52:39 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Autohotkey]]></category>
		<category><![CDATA[Farmville]]></category>
		<category><![CDATA[Script]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=562</guid>
		<description><![CDATA[Farmvill can be a fun way to pass some time, but if your fields get to be a bit big clicking on every single field can get to be a bit tiresome. I found this autohotkey script in the depths of the internet. It simplifies the process greatly  

Select All Code:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
!^c::
GoSub GetFarmSq
GoSub Getfirstsq
&#160;
i=1
&#160;
SetMouseDelay, 0
&#160;
cycle [...]]]></description>
			<content:encoded><![CDATA[<p>Farmvill can be a fun way to pass some time, but if your fields get to be a bit big clicking on every single field can get to be a bit tiresome. I found this <a href="http://www.autohotkey.com/download/">autohotkey</a> script in the depths of the internet. It simplifies the process greatly <img src='http://www.dopefish.de/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
</pre></td><td class="code"><pre class="autohotkey" style="font-family:monospace;">!<span style="color: #000000; font-weight: italic;">^</span>c::
<span style="color: #FF00FF; font-style: italic;">GoSub</span> GetFarmSq
<span style="color: #FF00FF; font-style: italic;">GoSub</span> Getfirstsq
&nbsp;
i<span style="color: #000000; font-weight: italic;">=</span><span style="color: #0000dd;">1</span>
&nbsp;
<span style="color: #FF00FF; font-style: italic;">SetMouseDelay</span><span style="color: #000000; font-weight: italic;">,</span> <span style="color: #0000dd;">0</span>
&nbsp;
cycle <span style="color: #000000; font-weight: italic;">:=</span> farmy<span style="color: #000000; font-weight: italic;">/</span><span style="color: #0000dd;">2</span>
newx<span style="color: #000000; font-weight: italic;">=</span><span style="font-weight: italic; color: #A00A0;">%startx%</span>
newy<span style="color: #000000; font-weight: italic;">=</span><span style="font-weight: italic; color: #A00A0;">%starty%</span>
<span style="color: #FF00FF; font-style: italic;">loop</span><span style="color: #000000; font-weight: italic;">,</span> <span style="font-weight: italic; color: #A00A0;">%cycle%</span>
<span style="color: #00FF00; font-weight: bold;">&#123;</span>
<span style="color: #FF00FF; font-style: italic;">Click</span> <span style="font-weight: italic; color: #A00A0;">%newx%</span><span style="color: #000000; font-weight: italic;">,</span><span style="font-weight: italic; color: #A00A0;">%newy%</span>
<span style="color: #FF00FF; font-style: italic;">Loop</span><span style="color: #000000; font-weight: italic;">,</span> <span style="font-weight: italic; color: #A00A0;">%farmx%</span>
<span style="color: #00FF00; font-weight: bold;">&#123;</span>
newx<span style="color: #000000; font-weight: italic;">+=</span><span style="color: #0000dd;">25</span>
newy<span style="color: #000000; font-weight: italic;">-=</span><span style="color: #0000dd;">12</span>
<span style="color: #FF00FF; font-style: italic;">Click</span> <span style="font-weight: italic; color: #A00A0;">%newx%</span><span style="color: #000000; font-weight: italic;">,</span><span style="font-weight: italic; color: #A00A0;">%newy%</span>
<span style="color: #00FF00; font-weight: bold;">&#125;</span>
newx<span style="color: #000000; font-weight: italic;">+=</span><span style="color: #0000dd;">25</span>
newy<span style="color: #000000; font-weight: italic;">+=</span><span style="color: #0000dd;">12</span>
<span style="color: #FF00FF; font-style: italic;">Click</span> <span style="font-weight: italic; color: #A00A0;">%newx%</span><span style="color: #000000; font-weight: italic;">,</span><span style="font-weight: italic; color: #A00A0;">%newy%</span>
<span style="color: #FF00FF; font-style: italic;">Loop</span><span style="color: #000000; font-weight: italic;">,</span> <span style="font-weight: italic; color: #A00A0;">%farmx%</span>
<span style="color: #00FF00; font-weight: bold;">&#123;</span>
newx<span style="color: #000000; font-weight: italic;">-=</span><span style="color: #0000dd;">25</span>
newy<span style="color: #000000; font-weight: italic;">+=</span><span style="color: #0000dd;">12</span>
<span style="color: #FF00FF; font-style: italic;">Click</span> <span style="font-weight: italic; color: #A00A0;">%newx%</span><span style="color: #000000; font-weight: italic;">,</span><span style="font-weight: italic; color: #A00A0;">%newy%</span>
<span style="color: #00FF00; font-weight: bold;">&#125;</span>
newx<span style="color: #000000; font-weight: italic;">+=</span><span style="color: #0000dd;">25</span>
newy<span style="color: #000000; font-weight: italic;">+=</span><span style="color: #0000dd;">12</span>
<span style="color: #00FF00; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #AAAAFF; font-weight: bold;">return</span>
&nbsp;
<span style="color: #FF00FF; font-style: italic;">GoSub</span> Getfirstsq
&nbsp;
<span style="color: #DD0000; font-style: italic;">GetFarmsq:
</span><span style="color: #FF00FF; font-style: italic;">Inputbox</span><span style="color: #000000; font-weight: italic;">,</span> farmy<span style="color: #000000; font-weight: italic;">,</span> Hi<span style="color: #000000; font-weight: italic;">,</span> <span style="color: #FF0000; font-style: italic;">Enter</span> the number of fields to the <span style="color: #FF0000; font-style: italic;">right</span> to <span style="color: #FF00FF; font-style: italic;">click</span> on<span style="color: #000000; font-weight: italic;">,,</span><span style="color: #0000dd;">200</span><span style="color: #000000; font-weight: italic;">,</span><span style="color: #0000dd;">150</span><span style="color: #000000; font-weight: italic;">,,,,,</span><span style="color: #0000dd;">6</span>
<span style="color: #AAAAFF; font-weight: bold;">if</span> <span style="color: #88FF88;">ErrorLevel</span>
<span style="color: #FF00FF; font-style: italic;">GoSub</span> <span style="color: #FF0000; font-style: italic;">Esc</span>
<span style="color: #FF00FF; font-style: italic;">Inputbox</span><span style="color: #000000; font-weight: italic;">,</span> farmx<span style="color: #000000; font-weight: italic;">,</span> Hi<span style="color: #000000; font-weight: italic;">,</span> <span style="color: #FF0000; font-style: italic;">Enter</span> the number of fields upwards to <span style="color: #FF00FF; font-style: italic;">click</span> on<span style="color: #000000; font-weight: italic;">,,</span><span style="color: #0000dd;">200</span><span style="color: #000000; font-weight: italic;">,</span><span style="color: #0000dd;">150</span><span style="color: #000000; font-weight: italic;">,,,,,</span><span style="color: #0000dd;">6</span>
<span style="color: #AAAAFF; font-weight: bold;">if</span> <span style="color: #88FF88;">ErrorLevel</span>
<span style="color: #FF00FF; font-style: italic;">GoSub</span> <span style="color: #FF0000; font-style: italic;">Esc</span>
farmx<span style="color: #000000; font-weight: italic;">--</span>
<span style="color: #AAAAFF; font-weight: bold;">return</span>
&nbsp;
<span style="color: #DD0000; font-style: italic;">Getfirstsq:
</span><span style="color: #FF00FF; font-style: italic;">Msgbox</span><span style="color: #000000; font-weight: italic;">,</span> <span style="color: #FF00FF; font-style: italic;">Click</span> on the leftmost field to start with <span style="color: #00FF00; font-weight: bold;">&#40;</span><span style="font-weight: bold; color: #008080;">&quot;9 O'clock&quot;</span> position<span style="color: #00FF00; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: italic;">.</span>
<span style="color: #FF00FF; font-style: italic;">KeyWait</span><span style="color: #000000; font-weight: italic;">,</span> <span style="color: #FF0000; font-style: italic;">LButton</span><span style="color: #000000; font-weight: italic;">,</span> D
<span style="color: #FF00FF; font-style: italic;">KeyWait</span><span style="color: #000000; font-weight: italic;">,</span> <span style="color: #FF0000; font-style: italic;">LButton</span><span style="color: #000000; font-weight: italic;">,</span> D
<span style="color: #FF00FF; font-style: italic;">MouseGetPos</span><span style="color: #000000; font-weight: italic;">,</span> startx<span style="color: #000000; font-weight: italic;">,</span> starty
<span style="color: #AAAAFF; font-weight: bold;">return</span>
&nbsp;
<span style="color: #FF0000; font-style: italic;">Esc</span>:
!<span style="color: #000000; font-weight: italic;">^</span>x::<span style="color: #FF00FF; font-style: italic;">reload</span>
<span style="color: #AAAAFF; font-weight: bold;">Return</span></pre></td></tr></table></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/562/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>bash scripting &#8230; switching into directory of the script</title>
		<link>http://www.dopefish.de/archives/551</link>
		<comments>http://www.dopefish.de/archives/551#comments</comments>
		<pubDate>Mon, 23 Nov 2009 09:13:26 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[readlink]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=551</guid>
		<description><![CDATA[Sometimes it is useful to switch into the directory of the script e.g. when we need to call or include further files and don&#8217;t want to go through the hassle of searching for the script in the file system. Especially when symlinks are involved everything get a bit more interesting. This little snippet switches into [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes it is useful to switch into the directory of the script e.g. when we need to call or include further files and don&#8217;t want to go through the hassle of searching for the script in the file system. Especially when symlinks are involved everything get a bit more interesting. This little snippet switches into the directory of the script, using readlink to dereference symlinks if the script is called via a symlink.</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">## a small snippet to switch into script directory</span>
<span style="color: #007800;">SCRIPTDIR</span>=<span style="color: #800000;">${0}</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-L</span> <span style="color: #800000;">${SCRIPTDIR}</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #666666; font-style: italic;"># dereference the symlink</span>
<span style="color: #007800;">SCRIPTDIR</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$(readlink -e ${0})</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #666666; font-style: italic;"># chop off the scriptname and switch into the directory</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #800000;">${SCRIPTDIR%/*}</span></pre></td></tr></table></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/551/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bash scripting, traps and sleep</title>
		<link>http://www.dopefish.de/archives/524</link>
		<comments>http://www.dopefish.de/archives/524#comments</comments>
		<pubDate>Fri, 09 Oct 2009 17:01:43 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[background]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[kill]]></category>
		<category><![CDATA[trap]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=524</guid>
		<description><![CDATA[Today I ran into any old problem: you have a script that should do something when it recieves a signal (e.g. if someone sends it USR1 it should write to a log/syslog), but the script uses a long sleep because it normally only checks/calculates stuff every x min. If you send it a kill -USR1 [...]]]></description>
			<content:encoded><![CDATA[<p>Today I ran into any old problem: you have a script that should do something when it recieves a signal (e.g. if someone sends it USR1 it should write to a log/syslog), but the script uses a long sleep because it normally only checks/calculates stuff every x min. If you send it a <span style="color: #800000;">kill -USR1 $pid</span> it will normally execute the trap AFTER the sleep is done, not so great. I figured of the following solution today: put the sleep in a while loop that checks if the full time was slept, and inside the loop a sleep that sleeps the X seconds remaing in the background followed by a wait.</p>
<p>If the script now recieves a USR1 it can kill the wait, execute the trap and will continue the remaining sleep on the next iteration of the loop.</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
initialize<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #000000; font-weight: bold;">set</span> <span style="color: #660033;">-o</span> nounset
<span style="color: #7a0874; font-weight: bold;">trap</span> cleanup INT TERM EXIT
<span style="color: #7a0874; font-weight: bold;">trap</span> print_foo USR1
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
cleanup<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
print_foo<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;foo&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
initialize
&nbsp;
<span style="color: #666666; font-style: italic;"># sleep 1 min</span>
<span style="color: #007800;">SLEEPTILL</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #ff0000;">'+%s'</span> <span style="color: #660033;">--date</span>=<span style="color: #ff0000;">&quot;+1 min&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #ff0000;">'+%s'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #660033;">-lt</span> <span style="color: #800000;">${SLEEPTILL}</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">do</span>
<span style="color: #c20cb9; font-weight: bold;">sleep</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">bc</span> <span style="color: #000000; font-weight: bold;">&lt;&lt;&lt;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${SLEEPTILL}</span> - <span style="color: #007800;">$(date '+%s')</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">&amp;</span>
<span style="color: #007800;">BACKGROUND</span>=<span style="color: #007800;">$!</span>
<span style="color: #7a0874; font-weight: bold;">wait</span> <span style="color: #007800;">$BACKGROUND</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/524/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Served from: www.dopefish.de @ 2012-02-08 10:59:14 by W3 Total Cache -->
