tag:blogger.com,1999:blog-84424537938036462192024-02-21T05:05:26.624+01:00The Linux Chroniclescat /dev/random > bloggerAnonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.comBlogger19125tag:blogger.com,1999:blog-8442453793803646219.post-75657276510273873582015-03-22T16:26:00.001+01:002015-03-22T16:26:29.572+01:00Parallel Redmine Installations: Installation<h2>
Install Redmine with RVM, Git, Apache, Phusion Passenger and MySql under Ubuntu Server 14.04 </h2>
<hr />
<h4>
Preface</h4>
<br />
As a system admin I've been responsable for managing our Redmine server in my company for nearly two years now. Over this time I've found some problems that made me search for a way to be able to install and test new Redmine versions without affecting my currently running Redmine environment.<br />
Using <a href="https://rvm.io/">RVM</a> and <a href="https://rvm.io/gemsets">dedicated gemsets</a> requieres a little more overhead for the installation but on the long run it brings huge benefits, specially if you are a sysadmin.<br />
<br />
<h4>
The Concept</h4>
Following table shows how separated redmine installations can be organized using <a href="https://rvm.io/">RVM</a>.
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqexJyUA82m8JYtOqECrSos0s3snw0UroI77-pk27nRzzP6ueehWHl8flm-71w6mQ4_j9-OOOJa_ah6_5RZBw3p7coRTHKC0ZjmjHolma3UQJ1WlCJEoptDuD6NbAupRTn7MsPzjoXryUU/s1600/redmine_install.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqexJyUA82m8JYtOqECrSos0s3snw0UroI77-pk27nRzzP6ueehWHl8flm-71w6mQ4_j9-OOOJa_ah6_5RZBw3p7coRTHKC0ZjmjHolma3UQJ1WlCJEoptDuD6NbAupRTn7MsPzjoXryUU/s1600/redmine_install.png" width="725" /></a></div>
<br />
In addition to the three elements in the table (redmine, ruby+gems,passenger) you can have separated databases for every Redmine version, or directly migrate the current database.<br />
In my case, my goal is to be able to test new Redmine releases in a safe environment before I decide to upgrade to it. So for testing purposes, I dump the currently used Redmine database, create a new database and import the dump back into the new database.<br />
Once I'm sure the new version works well, I drop the old database and the related Redmine and ruby stuff.<br />
If for some reason the new version does not work, switching back to the older version is not a problem.<br />
<br />
Following I will describe how to install Redmine from <a href="https://github.com/redmine/redmine">GitHub</a> 2.6-stable branch with dedicated ruby, gems and passenger module. Follow the same steps to install other separated Redmine versions.<br />
<h4>
Install Dependencies </h4>
<div class="code">
root@redmine:/home/adrian# apt-get install apache2 mysql-server
git libmysqlclient-dev imagemagick libmagickwand-dev libcurl4-openssl-dev apache2-threaded-dev libapr1-dev libaprutil1-dev
</div>
<h3>
</h3>
<h4>
Create Dedicated User</h4>
This user's home will live under <i><b>/opt/redmine</b></i> which will hold the redmine as well as ruby, ruby gems and the passenger modules.<br />
<div class="code">
root@redmine:/home/adrian# adduser --home /opt/redmine redmine
</div>
Add the redmine user to the sudo group
<br />
<div class="code">
root@redmine:/home/adrian# gpasswd -a redmine sudo
</div>
This is needed since RVM will install system dependencies via <b><i>apt-get</i></b> when needed.
<br />
<h3>
</h3>
<h4>
Install RVM</h4>
<b>Switch to the newly created redmine user</b> <br />
<div class="code">
root@redmine:/home/adrian# su - redmine
</div>
<b>Get RVM gpg key</b><br />
<div class="code">
redmine@redmine:~$ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
</div>
<b>Install RVM stable</b>
<br />
<div class="code">
redmine@redmine:~$ curl -sSL https://get.rvm.io | bash -s stable
</div>
To start using RVM source the <i><b>rvm</b></i> script<br />
<div class="code">
redmine@redmine:~$ source /opt/redmine/.rvm/scripts/rvm
</div>
The RVM installer adds RVM to the user's <i><b>PATH</b></i> variable so there is no need to source RVM every time you switch to the redmine user.<br />
<br />
Before we move on to install ruby and ruby gems, we will prepare the database and get Redmine from Git.<br />
<br />
<h4>
Prepare Redmine database</h4>
<br />
In this example I will create a new database named <b><i>redmine_production</i></b>, adjust the database name to your needs. If you are going to use the same database there is no need to create a new one but I would recommend to create a new separated database for testing since the <b><i>rake:migrate</i></b> commands later on will make changes to the database.<br />
<br />
Connect to the mysql server with the root user<br />
<div class="code">
redmine@redmine:~$ mysql -uroot -p
</div>
Create the Redmine database and the redmine user to connect to that database.
<br />
<div class="code">
mysql> create database redmine_production character set utf8;<br />
mysql> create user 'redmine'@'localhost' identified by 'redmine_mysql_user_password';<br />
mysql> grant all privileges on redmine_production.* to 'redmine'@'localhost';<br />
mysql> flush privileges;<br />
mysql> exit</div>
<h4>
Install Redmine from Git</h4>
<b>Clone Redmine's repository on GitHub</b> <br />
<div class="code">
redmine@redmine:~$ git clone https://github.com/redmine/redmine.git redmine-2.6-stable</div>
<b>Symlink the Redmine directory</b><br />
<div class="code">
redmine@redmine:~$ ln -s redmine-2.6-stable active
</div>
<div class="notice">
This symlink is needed for an easier switch between Redmine versions. The apache virtual host file will reference the <i><b>DocumentRoot</b></i>, <i><b>PassengerAppRoot</b></i>, etc using this <i><b>active</b></i> symlink.</div>
Of course you can have different virtual hosts for the different Redmine paths and enable/disable as needed. I prefer the symlink method.<br />
<br />
<b>Cehckout the 2.6 stable branch</b> <br />
<div class="code">
redmine@redmine:~/redmine-2.6.2$ git checkout origin/2.6-stable -b 2.6-stable</div>
<b>Configure Redmine </b><br />
<br />
The <b>database.yml</b> and <b>configuration.yml</b> is where you configure redmine. I will not dive into the <b>configuration.yml</b> file, take a look to the <a href="http://www.redmine.org/projects/redmine/wiki/redmineinstall">redmine installation documentation</a> for it.<br />
<div class="code">
redmine@redmine:~/redmine-2.6.2$ cp config/database.yml.example config/database.yml<br />
redmine@redmine:~/redmine-2.6.2$ cp config/configuration.yml.example config/configuration.yml</div>
Edit the <b>database.yml</b> (production section) file to fit the mysql redmine database created in the previous section.<br />
<div class="code">
production:<br />
adapter: mysql2<br />
database: redmine_production<br />
host: localhost<br />
username: redmine<br />
password: "redmine_mysql_user_password"<br />
encoding: utf8</div>
<b>Adjust permissions</b>
<div class="code">
redmine@redmine:~/redmine-2.6.2$ sudo chown -R www-data files log tmp public/plugin_assets
redmine@redmine:~/redmine-2.6.2$ sudo chmod -R 0775 files log tmp public/plugin_assets
</div>
<h4>
Install Ruby and Gems</h4>
<div class="code">
redmine@redmine:~$ rvm install 1.9.3
</div>
<b>
Create Gemset
</b>
<br />
<div class="code">
redmine@redmine:~$ rvm gemset create redmine-2.6-stable</div>
<b>
Use Gemset
</b>
<br />
<div class="danger">
This is a very important part. Every time you are going to manage gems you have to make sure the currently used gemset is the right one!
Please read the <a href="https://rvm.io/gemsets/using">RVM:Using Gemsets</a> reference.</div>
So after creating the <b><i>redmine-2.6-stable</i></b> gemset, listing the current available gemsets shows following. Note the arrow points to the currently used gemset:
<br />
<div class="code">
redmine@redmine:~$ rvm list gemsets<br />
<br />
rvm gemsets<br />
<br />
=> ruby-1.9.3-p551 [ x86_64 ]<br />
ruby-1.9.3-p551@global [ x86_64 ]<br />
ruby-1.9.3-p551@redmine-2.6-stable [ x86_64 ]</div>
We want to use our <b><i>redmine-2.6-stable</i></b> gemset:
<br />
<div class="code">
redmine@redmine:~$ rvm use gemset ruby-1.9.3-p551@redmine-2.6-stable<br />
Using /opt/redmine/.rvm/gems/ruby-1.9.3-p551 with gemset redmine-2.6-stable</div>
List gemsets again to check that the <b><i>redmine-2.6-stable</i></b> gemset is selected:
<br />
<div class="code">
redmine@redmine:~$ rvm list gemsets<br />
<br />
rvm gemsets<br />
<br />
ruby-1.9.3-p551 [ x86_64 ]<br />
ruby-1.9.3-p551@global [ x86_64 ]<br />
=> ruby-1.9.3-p551@redmine-2.6-stable [ x86_64 ]</div>
<b>Install Redmine Gems </b>
<br />
<div class="code">
redmine@redmine:~$ cd redmine-2.6-stable/<br />
redmine@redmine:~/redmine-2.6-stable$ bundle install --without develop,test,postgresql,sqlite<br />
redmine@redmine:~/redmine-2.6-stable$ gem install passenger
</div>
<b>RMV and bundler integration</b><br />
This step is needed since we will use <b>bundler</b> to install Redmine required gems. More detailed information about RVM and bundler integration in the <a href="https://rvm.io/integration/bundler">RVM site</a>.
<br />
<div class="code">
redmine@redmine:~$ gem regenerate_binstubs</div>
<h4>
Install Phusion Passenger</h4>
<div class="code">
redmine@redmine:~/redmine-2.6-stable$ passenger-install-apache2-module</div>
<h4>
Setup Apache</h4>
<b>Passenger</b><br />
Create a configuration file under <b>/etc/apache2/conf-available</b> named <b>passenger-ruby-1.9.3-p551@redmine-2.6-stable</b> which links to the passenger module.<br />
Why this name? Because later on, when we want to switch between redmine installations with their separated ruby + gems installations, we will have separated passenger modules for each of them.<br />
<div class="code">
LoadModule passenger_module /opt/redmine/.rvm/gems/ruby-1.9.3-p551@redmine-2.6-stable/gems/passenger-5.0.4/buildout/apache2/mod_passenger.so<br />
<IfModule mod_passenger.c><br />
PassengerRoot /opt/redmine/.rvm/gems/ruby-1.9.3-p551@redmine-2.6-stable/gems/passenger-5.0.4<br />
PassengerDefaultRuby /opt/redmine/.rvm/gems/ruby-1.9.3-p551@redmine-2.6-stable/wrappers/ruby<br />
</IfModule></div>
<b>Virtual Host</b>
<br />
Note that all references to the Redmine path use the <i><b>active</b></i> symlink instead of the actual Redmine directory, this way you only need to point the symlink to the right Redmine directoy to switch between Redmine versions and only one virtual host file is needed.<br />
This is just a sample file. You will have to adjust it to your needs. <br />
<div class="code">
<VirtualHost *:80><br />
ServerAdmin admin@domain.sample<br />
ServerName redmine.domain.sample<br />
<br />
DocumentRoot /opt/redmine/active/public/<br />
RailsBaseURI /<br />
PassengerAppRoot /opt/redmine/active<br />
PassengerUser www-data<br />
PassengerGroup www-data<br />
<br />
<Directory /opt/redmine/active/public/><br />
Options +Indexes +FollowSymLinks -MultiViews<br />
AllowOverride All<br />
Order allow,deny<br />
Allow from all<br />
</Directory><br />
LogLevel warn<br />
ErrorLog ${APACHE_LOG_DIR}/redmine.error.log<br />
CustomLog ${APACHE_LOG_DIR}/redmine.access.log combined<br />
</VirtualHost><br />
</div>
<b>Final Steps</b> <br />
<div class="danger">
Note that the <b><i>rake redmine:load_default_data</i></b> step is only needed when making a fresh new installation. Omit this step if using an already exiting (or imported from dump) Redmine database.
The <b><i>rake redmine:plugins:migrate</i></b> is needed only if you are using some extra plugins.
</div>
<div class="code">
redmine@redmine:~/redmine-2.6-stable$ rake generate_secret_token<br />
redmine@redmine:~/redmine-2.6-stable$ RAILS_ENV=production rake db:migrate<br />
redmine@redmine:~/redmine-2.6-stable$ RAILS_ENV=production rake redmine:load_default_data<br />
redmine@redmine:~/redmine-2.6-stable$ RAILS_ENV=production rake redmine:plugins:migrate<br />
</div>Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com2tag:blogger.com,1999:blog-8442453793803646219.post-13774873036004102062014-04-21T21:02:00.000+02:002014-04-22T23:02:43.133+02:00Encrypted Software RAID-5 on Debian WheezyThis post will cover the creation of a software RAID-5 with Luks encryption on top in a pure practical way. For more detailed information about Linux Software RAID check out following links:<br />
<br />
<a href="https://raid.wiki.kernel.org/index.php/RAID_setup">https://raid.wiki.kernel.org/index.php/RAID_setup</a><br />
<a href="https://wiki.archlinux.org/index.php/RAID">https://wiki.archlinux.org/index.php/RAID</a><br />
<br />
<h3>
</h3>
<h3>
Layout</h3>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYuN0tQUCnJFz4qqj23KkbhFKy37A93q8Ss9tzo0H7-FhPIIU-p0bUaome8_9K6zz_4iFIbf9L-GmXWHTUNrJMoHYO2AJGU3y9pihMA8E3qDA0D-pttC3MhvRAF-o4QK2OJAUVFHPVTnEr/s1600/raid-5_scheme.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYuN0tQUCnJFz4qqj23KkbhFKy37A93q8Ss9tzo0H7-FhPIIU-p0bUaome8_9K6zz_4iFIbf9L-GmXWHTUNrJMoHYO2AJGU3y9pihMA8E3qDA0D-pttC3MhvRAF-o4QK2OJAUVFHPVTnEr/s1600/raid-5_scheme.png" height="292" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<h3>
Partitioning</h3>
Create a same sized partition on every disk of type <b>Non-FS</b> (Id=da). Its important to create identic partitions on every disk to avoid future problems if a disk has to be replaced and the RAID has to be resynced.<br />
Fdisk output of the RAID members after partitioning:<br />
<br />
<pre>Device Boot Start End Blocks Id System
/dev/sda1 2048 1953525167 976761560 da Non-FS data
/dev/sdb1 2048 1953525167 976761560 da Non-FS data
/dev/sdc1 2048 1953525167 976761560 da Non-FS data
/dev/sdd1 2048 1953525167 976761560 da Non-FS data
/dev/sde1 2048 1953525167 976761560 da Non-FS data</pre>
<h3>
Create the RAID</h3>
<pre>mdadm --create --verbose --level=5 --metadata=1.2 --raid-devices=4 /dev/md0 /dev/sd[abcd]1 \</pre>
<pre>--spare-devices=1 /dev/sde1</pre>
The RAID should now start syncing. You can check the proggress with <br />
<pre>watch cat /proc/mdstat</pre>
If for some reason the sync doesn't start automatically, you can force is with the following command
<br />
<pre>mdadm --readwrite /dev/mdX</pre>
Where /dev/mdX is your RAID device. For example /dev/md0<br />
Depending on your RAID size this process will take a couple of hours.<br />
Save the RAID configuration
<br />
<pre>mdadm --detail --scan >> /etc/mdadm/mdadm.conf</pre>
<h3>
Encrypt RAID</h3>
Create the LUKS container on the <acronym title="Redundant Array of Inexpensive Disks">RAID</acronym> device /dev/md0. I used <i><b>aes-xts-plain64</b></i>, there are others like <i><b>aes-cbc-essiv</b></i> (which is the default at the time of this writing). What is the better cipher depends on many factors and is out of the scope of this post.<br />
<br />
<code></code><br />
<code>cryptsetup luksFormat --cipher aes-xts-plain64 --hash sha512 /dev/md0</code><br />
<br />
This will require a confirmation in upper case to go on and enter the desired passphrase.<br />
Open the new LUKS container:<br />
<br />
<code>cryptsetup luksDump /dev/md0</code><br />
<br />
This will create a new link under /dev/mapper named crypt-raid To get crypt setup on the encrypted device:<br />
<br />
<code>cryptsetup luksDump /dev/md0</code><br />
<br />
This shows, among other data, the used algorithm, key hash and used key slots<br />
<br />
<h3>
Create File System</h3>
Before you can mount the encrypted RAID device you have to create a file system on it. In this case I will go eith <i><b>ext4</b></i>:<br />
<br />
<code>mkfs.ext4 /dev/mapper/crypt-raid</code><br />
<br />
<h3>
Mount Device</h3>
Before the crypt device can be mounted it has to be unlocked:<br />
<br />
<code>cryptsetup luksOpen /dev/md0 crypt-raid</code><br />
<br />
This maps the crypt device under<i><b> /dev/mapper/crypt-raid</b></i><br />
<div b="">
</div>
Now the device can be mounted as usual:<br />
<br />
<code>mount /dev/mapper/crypt-raid [mount point]</code><br />
<h3>
</h3>
<h3>
On Boot Mount</h3>
In order to get the encrypted device unlocked and mounted on boot, it has to be added to both the <i><b>/etc/crypttab</b></i> and the <i><b>/etc/fstab</b></i> files as follows:<br />
<br />
/etc/crypttab:<br />
<code>crypt-raid /dev/md0 none luks</code><br />
<code> </code>
<br />
/etc/fstab:<br />
<code>/dev/mapper/crypt-raid /mnt/raid ext4 defaults 0 0</code><br />
<br />
<b>This will block the boot process</b> until you provide the required passphrase which can be a problem if you are running a headless server. One solution is to provide a key file on an external USB stick or only use the fstab entry (no crypttab entry) with the <b>noauto</b> option and manually mount the device via ssh.Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com1tag:blogger.com,1999:blog-8442453793803646219.post-76181548008846160682013-04-28T12:36:00.002+02:002013-04-28T12:45:17.663+02:00Thunar Custom Actions: Pictures Batch Resize with Zenity Progress Bar and ConvertThis is e better version of my old <a href="http://thelinuxchronicles.blogspot.de/2012/06/thunar-custom-actions-resize-and-rotate.html">picture resize script</a>. It uses <a href="http://en.wikipedia.org/wiki/Zenity">Zenity</a> to ask for the resizing percentage and to show a progress bar while the script is working.<br />
<br />
<h3>
Screenshots</h3>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFdkwt6dqz3fIfA-UIK5VIeqIhWqIW0U-4yW46zZsVCpPbN0rxOkSqZ2gSn4vOoC0tDQWYjJzvIazsQ5C3j3MUW_Mc0i4sK1kDOpT_TJHaGvt0WdbXPfK7TUtbUIUjgPNZERQEO0W-UIBE/s1600/2013-04-28-120446_883x669_scrot.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFdkwt6dqz3fIfA-UIK5VIeqIhWqIW0U-4yW46zZsVCpPbN0rxOkSqZ2gSn4vOoC0tDQWYjJzvIazsQ5C3j3MUW_Mc0i4sK1kDOpT_TJHaGvt0WdbXPfK7TUtbUIUjgPNZERQEO0W-UIBE/s320/2013-04-28-120446_883x669_scrot.png" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Resize Script Enter Percentage</td></tr>
</tbody></table>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhR7fWWqQF0mTvPgKaT6b4Yat2Ne_Frvq8gIhSF3gN96fiq5IFJF-xK-VgaCijFKUdHOhqJYylkbksKwBuVN-9cxuP4f7KeFqts1mi-5PoHe9RDsRrLN-mOtXsVi64N6Dkt_Exb54O3YVk-/s1600/2013-04-28-120700_3840x1080_scrot.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="241" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhR7fWWqQF0mTvPgKaT6b4Yat2Ne_Frvq8gIhSF3gN96fiq5IFJF-xK-VgaCijFKUdHOhqJYylkbksKwBuVN-9cxuP4f7KeFqts1mi-5PoHe9RDsRrLN-mOtXsVi64N6Dkt_Exb54O3YVk-/s320/2013-04-28-120700_3840x1080_scrot.png" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Resize Script Progress Bar</td></tr>
</tbody></table>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="clear: right; margin-bottom: 1em; margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_dKCrqMfh-Db5SRt1gmsxxioiw0UKeqIa6p9w1s9M1ysrpvxNBHBj8BL211WLrzQrpf6n9EGtk1UuHBi2wxrz_pnRVcnfnfWJMTi-lmOVtqsEtOz5_HvP4KJXLpBorfrTUmi_pZrLZIwn/s1600/2013-04-28-120523_1001x747_scrot.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="235" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_dKCrqMfh-Db5SRt1gmsxxioiw0UKeqIa6p9w1s9M1ysrpvxNBHBj8BL211WLrzQrpf6n9EGtk1UuHBi2wxrz_pnRVcnfnfWJMTi-lmOVtqsEtOz5_HvP4KJXLpBorfrTUmi_pZrLZIwn/s320/2013-04-28-120523_1001x747_scrot.png" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Resize Script Work Completed</td></tr>
</tbody></table>
<h3>
Bash Script</h3>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<!DOCTYPE html>
<html>
<head>
<title>picresize.sh</title>
<style type="text/css">
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:10pt; font-family:'Terminus';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.ipl { color:#c75e75; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
</style>
</head>
<body class="hl">
<pre class="hl"><span class="hl lin"> 1 </span><span class="hl slc">#!/bin/bash</span>
<span class="hl lin"> 2 </span>
<span class="hl lin"> 3 </span>
<span class="hl lin"> 4 </span><span class="hl slc"># paths, constants, etc.</span>
<span class="hl lin"> 5 </span>PICPATH<span class="hl opt">=</span><span class="hl str">"$1"</span>
<span class="hl lin"> 6 </span>TOTALPICS<span class="hl opt">=</span><span class="hl str">"$((($# - 1)))"</span>
<span class="hl lin"> 7 </span>STEP<span class="hl opt">=</span><span class="hl str">"$(((100 / $TOTALPICS)))"</span>
<span class="hl lin"> 8 </span>PROGR<span class="hl opt">=</span><span class="hl num">0</span>
<span class="hl lin"> 9 </span>MINSIZE<span class="hl opt">=</span><span class="hl num">1</span>
<span class="hl lin">10 </span>MAXSIZE<span class="hl opt">=</span><span class="hl num">500</span>
<span class="hl lin">11 </span>
<span class="hl lin">12 </span><span class="hl slc"># get the new size percentage, check it and set output directory path</span>
<span class="hl lin">13 </span>SIZE<span class="hl opt">=</span><span class="hl str">""</span>
<span class="hl lin">14 </span><span class="hl kwa">while</span> <span class="hl opt">[ -</span>z <span class="hl str">"$SIZE"</span> <span class="hl opt">]</span> || <span class="hl opt">[[</span> <span class="hl kwd">$SIZE</span><span class="hl opt"><</span><span class="hl kwd">$MINSIZE</span> <span class="hl opt">]]</span> || <span class="hl opt">[[</span> <span class="hl kwd">$SIZE</span><span class="hl opt">></span><span class="hl kwd">$MAXSIZE</span> <span class="hl opt">]];</span> <span class="hl kwa">do</span>
<span class="hl lin">15 </span> SIZE<span class="hl opt">=</span><span class="hl str">"$(zenity --entry --title="</span>Picture Resize<span class="hl str">" --text="</span>Enter percentage...<span class="hl str">")"</span>
<span class="hl lin">16 </span>
<span class="hl lin">17 </span> <span class="hl slc"># exit if cancel pressed</span>
<span class="hl lin">18 </span> <span class="hl opt">[[</span> $? <span class="hl opt">!=</span> <span class="hl num">0</span> <span class="hl opt">]] &&</span> <span class="hl kwb">exit</span> <span class="hl num">0</span>
<span class="hl lin">19 </span>
<span class="hl lin">20 </span> <span class="hl slc"># check range</span>
<span class="hl lin">21 </span> <span class="hl kwa">if</span> <span class="hl opt">[[</span> <span class="hl kwd">$SIZE</span><span class="hl opt"><</span><span class="hl kwd">$MINSIZE</span> <span class="hl opt">]]</span> || <span class="hl opt">[[</span> <span class="hl kwd">$SIZE</span><span class="hl opt">></span><span class="hl kwd">$MAXSIZE</span> <span class="hl opt">]];</span> <span class="hl kwa">then</span>
<span class="hl lin">22 </span> zenity <span class="hl opt">--</span>error <span class="hl opt">--</span>text<span class="hl opt">=</span><span class="hl str">"$SIZE% is outside the accepted range: 1% - 500%!"</span>
<span class="hl lin">23 </span> <span class="hl kwa">fi</span>
<span class="hl lin">24 </span><span class="hl kwa">done</span>
<span class="hl lin">25 </span>
<span class="hl lin">26 </span>OUTPATH<span class="hl opt">=</span><span class="hl str">"resize_$SIZE%"</span>
<span class="hl lin">27 </span>
<span class="hl lin">28 </span><span class="hl slc"># check if output directory exists</span>
<span class="hl lin">29 </span><span class="hl opt">[ -</span>d <span class="hl str">"$PICPATH/$OUTPATH"</span> <span class="hl opt">]</span> || mkdir <span class="hl str">"$PICPATH/$OUTPATH"</span>
<span class="hl lin">30 </span><span class="hl kwb">shift</span>
<span class="hl lin">31 </span>
<span class="hl lin">32 </span><span class="hl slc"># convert pics, show progress bar</span>
<span class="hl lin">33 </span><span class="hl kwa">while</span> <span class="hl opt">((</span> <span class="hl kwd">$#</span> <span class="hl opt">));</span> <span class="hl kwa">do</span>
<span class="hl lin">34 </span> convert <span class="hl str">"$PICPATH/$1"</span> <span class="hl opt">-</span>resize <span class="hl str">"$SIZE%"</span> <span class="hl str">"$PICPATH/$OUTPATH/resized_$SIZE%-$1"</span>
<span class="hl lin">35 </span> <span class="hl opt">((</span>PROGR<span class="hl opt">+=</span><span class="hl kwd">$STEP</span><span class="hl opt">))</span>
<span class="hl lin">36 </span> <span class="hl kwb">echo</span> <span class="hl kwd">$PROGR</span>
<span class="hl lin">37 </span> <span class="hl kwb">shift</span>
<span class="hl lin">38 </span><span class="hl kwa">done</span> | zenity <span class="hl opt">--</span>progress <span class="hl opt">--</span>title<span class="hl opt">=</span><span class="hl str">"Picture Resize"</span> <span class="hl opt">--</span>text<span class="hl opt">=</span><span class="hl str">"working..."</span> <span class="hl opt">--</span>percentage<span class="hl opt">=</span><span class="hl num">0</span> <span class="hl opt">-</span>
<span class="hl lin">39 </span> <span class="hl opt">-</span>auto<span class="hl opt">-</span>close
<span class="hl lin">40 </span>
<span class="hl lin">41 </span><span class="hl slc"># check return value and print message depending on it</span>
<span class="hl lin">42 </span><span class="hl opt">[[</span> $? <span class="hl opt">==</span> <span class="hl num">0</span> <span class="hl opt">]] &&</span> zenity <span class="hl opt">--</span>info <span class="hl opt">--</span>title<span class="hl opt">=</span><span class="hl str">"Picture Resize"</span> <span class="hl opt">--</span>text<span class="hl opt">=</span><span class="hl str">"All done!"</span> || zenity <span class="hl opt">-</span>
<span class="hl lin">43 </span> <span class="hl opt">-</span>error <span class="hl opt">--</span>text<span class="hl opt">=</span><span class="hl str">"Canceled!"</span>
</pre>
</body>
</html>
<!--HTML generated by highlight 3.13, http://www.andre-simon.de/-->
<br />
<h3>
Add Thunar Custom Action</h3>
Please refer to <a href="http://docs.xfce.org/xfce/thunar/custom-actions">this article</a> for adding a custom action to Thunar.<br />
Here is a screenshot of my custom action for this resizing script.<br />
<br />
<b>Note</b> that the bash script needs two <i>"thunar arguments"</i> :<br />
<ol>
<li>Path to the pictures to be resized -> %d</li>
<li>Pictures list -> %N</li>
</ol>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguwYbNHhZYyEQlxGBRALOHKWs42DiYfIX2eWnlMpHtPCP20g4EVVVYP4kep0bFeZmWPz9E0UEXA4KZU7a0HGI10YOhKbijCa6F26cqo-9AokhvBCetdQAPBPqYfuVLwSUOrmaEC5hI_mRF/s1600/2013-04-28-122859_450x469_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguwYbNHhZYyEQlxGBRALOHKWs42DiYfIX2eWnlMpHtPCP20g4EVVVYP4kep0bFeZmWPz9E0UEXA4KZU7a0HGI10YOhKbijCa6F26cqo-9AokhvBCetdQAPBPqYfuVLwSUOrmaEC5hI_mRF/s400/2013-04-28-122859_450x469_scrot.png" width="383" /></a></div>
<br />
<h3>
</h3>
Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com1tag:blogger.com,1999:blog-8442453793803646219.post-50082702595257240642013-04-27T22:29:00.000+02:002013-04-28T09:24:12.474+02:00Volume OSD with Openbox and Riitek Micro KeyboardWith this small bash script you can show a nice progress bar on the screen when changing the volume in a TV-like fashion.<br />
The script uses <i>osd_cat</i> provided by the <a href="http://sourceforge.net/projects/libxosd/">xosd</a> package under <a href="http://www.archlinux.org/">Arch Linux</a> (Please refer to your system specific package manager for install this package under other distributions).<br />
<br />
In order to make this script work with Openbox you will have to edit the Openbox configuration file <i>rc.xml</i> file normally placed under <i><b>/home/<user>/.config/openbox/rc.xml</b></i>. I will show how I did it later on this post.<br />
<br />
<h3>
OSD Bash Script</h3>
A few function definitions make the main part simpler. They are mainly responsible of increasing/decreasing the volume and showing the on-screen message.<br />
There are also some constants responsible for placing the message on a certain screen position, setting the font, etc.<br />
<html>
<head>
<title>test.sh</title>
<style type="text/css">
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:10pt; font-family:'Terminus';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.ipl { color:#c75e75; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
</style>
</head>
<body class="hl">
<pre class="hl"><span class="hl lin"> 1 </span><span class="hl slc">#!/bin/bash</span>
<span class="hl lin"> 2 </span>
<span class="hl lin"> 3 </span>
<span class="hl lin"> 4 </span>
<span class="hl lin"> 5 </span><span class="hl slc"># constants</span>
<span class="hl lin"> 6 </span>FONT<span class="hl opt">=</span><span class="hl str">'-*-fixed-*-*-*-*-100-*-*-*-*-*-*-*'</span>
<span class="hl lin"> 7 </span>COLOR<span class="hl opt">=</span><span class="hl str">"green"</span>
<span class="hl lin"> 8 </span>DELAY<span class="hl opt">=</span><span class="hl num">4</span>
<span class="hl lin"> 9 </span>POS<span class="hl opt">=</span><span class="hl str">"bottom"</span>
<span class="hl lin">10 </span>ALIGN<span class="hl opt">=</span><span class="hl str">"center"</span>
<span class="hl lin">11 </span>BARMOD<span class="hl opt">=</span><span class="hl str">"percentage"</span>
<span class="hl lin">12 </span>VOLTXT<span class="hl opt">=</span><span class="hl str">"Volume"</span>
<span class="hl lin">13 </span>VOLMUTEDTXT<span class="hl opt">=</span><span class="hl str">"Muted"</span>
<span class="hl lin">14 </span>VOLSTEP<span class="hl opt">=</span><span class="hl num">2</span>
<span class="hl lin">15 </span>
<span class="hl lin">16 </span>
<span class="hl lin">17 </span>
<span class="hl lin">18 </span><span class="hl slc"># kills an existing osd_cat process</span>
<span class="hl lin">19 </span><span class="hl slc"># needed when holding down a key to force repaint of the onscreen message</span>
<span class="hl lin">20 </span>preKill<span class="hl opt">() {</span>
<span class="hl lin">21 </span> killall osd_cat
<span class="hl lin">22 </span><span class="hl opt">}</span>
<span class="hl lin">23 </span>
<span class="hl lin">24 </span><span class="hl slc"># gets the actual volume value</span>
<span class="hl lin">25 </span>getVol<span class="hl opt">() {</span>
<span class="hl lin">26 </span> VOL<span class="hl opt">=</span><span class="hl str">"$(amixer sget Master | grep "</span>Mono<span class="hl opt">:</span><span class="hl str">" | awk '{ print $4 }' | sed -e 's/\[//' -</span>
<span class="hl lin"> </span><span class="hl str"> e 's/\]//' -e 's/\%//')"</span>
<span class="hl lin">27 </span><span class="hl opt">}</span>
<span class="hl lin">28 </span>
<span class="hl lin">29 </span><span class="hl slc"># gets the actual volume value and prints is on the screen</span>
<span class="hl lin">30 </span><span class="hl slc"># with a percent bar + a percent number</span>
<span class="hl lin">31 </span>showVol<span class="hl opt">() {</span>
<span class="hl lin">32 </span> getVol
<span class="hl lin">33 </span> <span class="hl kwa">if</span> <span class="hl opt">[[</span> <span class="hl kwd">$VOL</span> <span class="hl opt">==</span> <span class="hl num">0</span> <span class="hl opt">]];</span> <span class="hl kwa">then</span>
<span class="hl lin">34 </span> osd_cat <span class="hl opt">--</span>pos<span class="hl opt">=</span><span class="hl str">"$POS"</span> <span class="hl opt">--</span>align<span class="hl opt">=</span><span class="hl str">"$ALIGN"</span> <span class="hl opt">--</span>delay<span class="hl opt">=</span><span class="hl str">"$DELAY"</span> <span class="hl opt">--</span>colour<span class="hl opt">=</span><span class="hl str">"$COLOR"</span> <span class="hl opt">--</span>
<span class="hl lin"> </span> font<span class="hl opt">=</span><span class="hl str">"$FONT"</span> <span class="hl opt">--</span>barmode<span class="hl opt">=</span><span class="hl str">"$BARMOD"</span> <span class="hl opt">--</span>text<span class="hl opt">=</span><span class="hl str">"$VOLMUTEDTXT $VOL%"</span> <span class="hl opt">--</span>
<span class="hl lin"> </span> percentage<span class="hl opt">=</span><span class="hl str">"$VOL"</span>
<span class="hl lin">35 </span> <span class="hl kwa">else</span>
<span class="hl lin">36 </span> osd_cat <span class="hl opt">--</span>pos<span class="hl opt">=</span><span class="hl str">"$POS"</span> <span class="hl opt">--</span>align<span class="hl opt">=</span><span class="hl str">"$ALIGN"</span> <span class="hl opt">--</span>delay<span class="hl opt">=</span><span class="hl str">"$DELAY"</span> <span class="hl opt">--</span>colour<span class="hl opt">=</span><span class="hl str">"$COLOR"</span> <span class="hl opt">--</span>
<span class="hl lin"> </span> font<span class="hl opt">=</span><span class="hl str">"$FONT"</span> <span class="hl opt">--</span>barmode<span class="hl opt">=</span><span class="hl str">"$BARMOD"</span> <span class="hl opt">--</span>text<span class="hl opt">=</span><span class="hl str">"$VOLTXT $VOL%"</span> <span class="hl opt">--</span>
<span class="hl lin"> </span> percentage<span class="hl opt">=</span><span class="hl str">"$VOL"</span>
<span class="hl lin">37 </span> <span class="hl kwa">fi</span>
<span class="hl lin">38 </span><span class="hl opt">}</span>
<span class="hl lin">39 </span>
<span class="hl lin">40 </span><span class="hl slc"># reises the master channel by "VOLSTEP"</span>
<span class="hl lin">41 </span><span class="hl slc">volUp() {</span>
<span class="hl lin">42 </span> amixer sset Master <span class="hl str">"$VOLSTEP+"</span>
<span class="hl lin">43 </span><span class="hl opt">}</span>
<span class="hl lin">44 </span>
<span class="hl lin">45 </span><span class="hl slc"># decreases the master channel by "VOLSTEP"</span>
<span class="hl lin">46 </span>volDown<span class="hl opt">() {</span>
<span class="hl lin">47 </span> amixer sset Master <span class="hl str">"$VOLSTEP-"</span>
<span class="hl lin">48 </span><span class="hl opt">}</span>
<span class="hl lin">49 </span>
<span class="hl lin">50 </span><span class="hl slc"># mutes the master channel</span>
<span class="hl lin">51 </span>volMute<span class="hl opt">() {</span>
<span class="hl lin">52 </span> amixer sset Master <span class="hl num">0</span>
<span class="hl lin">53 </span><span class="hl opt">}</span>
<span class="hl lin">54 </span>
<span class="hl lin">55 </span>
<span class="hl lin">56 </span><span class="hl slc"># main part</span>
<span class="hl lin">57 </span>preKill
<span class="hl lin">58 </span>
<span class="hl lin">59 </span><span class="hl kwa">case</span> <span class="hl str">"$1"</span> <span class="hl kwa">in</span>
<span class="hl lin">60 </span> <span class="hl str">"volup"</span><span class="hl opt">)</span>
<span class="hl lin">61 </span> volUp
<span class="hl lin">62 </span> showVol
<span class="hl lin">63 </span> <span class="hl opt">;;</span>
<span class="hl lin">64 </span> <span class="hl str">"voldown"</span><span class="hl opt">)</span>
<span class="hl lin">65 </span> volDown
<span class="hl lin">66 </span> showVol
<span class="hl lin">67 </span> <span class="hl opt">;;</span>
<span class="hl lin">68 </span> <span class="hl str">"mute"</span><span class="hl opt">)</span>
<span class="hl lin">69 </span> volMute
<span class="hl lin">70 </span> showVol
<span class="hl lin">71 </span> <span class="hl opt">;;</span>
<span class="hl lin">72 </span> <span class="hl opt">*)</span>
<span class="hl lin">73 </span> <span class="hl opt">;;</span>
<span class="hl lin">74 </span><span class="hl kwa">esac</span>
</pre>
</body>
</html>
<!--HTML generated by highlight 3.13, http://www.andre-simon.de/-->
<br />
<h3>
Openbox rc.xml File</h3>
The rc.xml file has a keyboard section where custom keyboard shortcuts can be defined.<br />
<br />
<h3>
<html>
<head>
<title>x.xml</title>
<style type="text/css">
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:10pt; font-family:'Terminus';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.ipl { color:#c75e75; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
</style>
</head>
<body class="hl">
<pre class="hl"><span class="hl lin"> 1 </span> <span class="hl kwa"><keybind</span> <span class="hl kwb">key</span>=<span class="hl str">"W-KP_Add"</span><span class="hl kwa">></span>
<span class="hl lin"> 2 </span> <span class="hl kwa"><action</span> <span class="hl kwb">name</span>=<span class="hl str">"execute"</span><span class="hl kwa">></span>
<span class="hl lin"> 3 </span> <span class="hl kwa"><execute></span>/home/adrian/scripts/osd.sh volup<span class="hl kwa"></execute></span>
<span class="hl lin"> 4 </span> <span class="hl kwa"></action></span>
<span class="hl lin"> 5 </span> <span class="hl kwa"></keybind></span>
<span class="hl lin"> 6 </span> <span class="hl kwa"><keybind</span> <span class="hl kwb">key</span>=<span class="hl str">"W-KP_Subtract"</span><span class="hl kwa">></span>
<span class="hl lin"> 7 </span> <span class="hl kwa"><action</span> <span class="hl kwb">name</span>=<span class="hl str">"execute"</span><span class="hl kwa">></span>
<span class="hl lin"> 8 </span> <span class="hl kwa"><execute></span>/home/adrian/scripts/osd.sh voldown<span class="hl kwa"></execute></span>
<span class="hl lin"> 9 </span> <span class="hl kwa"></action></span>
<span class="hl lin">10 </span> <span class="hl kwa"></keybind></span>
<span class="hl lin">11 </span> <span class="hl kwa"><keybind</span> <span class="hl kwb">key</span>=<span class="hl str">"W-m"</span><span class="hl kwa">></span>
<span class="hl lin">12 </span> <span class="hl kwa"><action</span> <span class="hl kwb">name</span>=<span class="hl str">"execute"</span><span class="hl kwa">></span>
<span class="hl lin">13 </span> <span class="hl kwa"><execute></span>/home/adrian/scripts/osd.sh mute<span class="hl kwa"></execute></span>
<span class="hl lin">14 </span> <span class="hl kwa"></action></span>
<span class="hl lin">15 </span> <span class="hl kwa"></keybind></span>
</pre>
</body>
</html>
<!--HTML generated by highlight 3.13, http://www.andre-simon.de/-->Riitek Micro Keyboard</h3>
Besides the keyboard shortcuts, I wanted to use my <a href="http://www.riitek.com/product_Info.asp?id=63">Riitek Micro Keyboard RT-MWK03</a>. When pressing the Vulume Up/Down/Mute key combination on the keyboard an X event is generated on the X server.<br />
These X events can be used in the rc.xml file just the same way as any other keyboard shortcut. I used <a href="https://wiki.archlinux.org/index.php/Extra_Keyboard_Keys">Xev</a> to find out what events are generated when pressing the volume key combinations.<br />
<html>
<head>
<title>y.xml</title>
<style type="text/css">
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:10pt; font-family:'Terminus';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.ipl { color:#c75e75; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
</style>
</head>
<body class="hl">
<pre class="hl"><span class="hl lin"> 1 </span> <span class="hl kwa"><keybind</span> <span class="hl kwb">key</span>=<span class="hl str">"XF86AudioRaiseVolume"</span><span class="hl kwa">></span>
<span class="hl lin"> 2 </span> <span class="hl kwa"><action</span> <span class="hl kwb">name</span>=<span class="hl str">"execute"</span><span class="hl kwa">></span>
<span class="hl lin"> 3 </span> <span class="hl kwa"><execute></span>/home/adrian/scripts/osd.sh volup<span class="hl kwa"></execute></span>
<span class="hl lin"> 4 </span> <span class="hl kwa"></action></span>
<span class="hl lin"> 5 </span> <span class="hl kwa"></keybind></span>
<span class="hl lin"> 6 </span> <span class="hl kwa"><keybind</span> <span class="hl kwb">key</span>=<span class="hl str">"XF86AudioLowerVolume"</span><span class="hl kwa">></span>
<span class="hl lin"> 7 </span> <span class="hl kwa"><action</span> <span class="hl kwb">name</span>=<span class="hl str">"execute"</span><span class="hl kwa">></span>
<span class="hl lin"> 8 </span> <span class="hl kwa"><execute></span>/home/adrian/scripts/osd.sh voldown<span class="hl kwa"></execute></span>
<span class="hl lin"> 9 </span> <span class="hl kwa"></action></span>
<span class="hl lin">10 </span> <span class="hl kwa"></keybind></span>
<span class="hl lin">11 </span> <span class="hl kwa"><keybind</span> <span class="hl kwb">key</span>=<span class="hl str">"XF86AudioMute"</span><span class="hl kwa">></span>
<span class="hl lin">12 </span> <span class="hl kwa"><action</span> <span class="hl kwb">name</span>=<span class="hl str">"execute"</span><span class="hl kwa">></span>
<span class="hl lin">13 </span> <span class="hl kwa"><execute></span>/home/adrian/scripts/osd.sh mute<span class="hl kwa"></execute></span>
<span class="hl lin">14 </span> <span class="hl kwa"></action></span>
<span class="hl lin">15 </span> <span class="hl kwa"></keybind></span>
</pre>
</body>
</html>
<!--HTML generated by highlight 3.13, http://www.andre-simon.de/-->
<br />
<br />
<h3>
Screenshots</h3>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3bcc5VqVyUBD_-BsFcuPqmqs6Cx8jcGXsjlShPdo-ZN3-iNxUFBGCU6XCjeMuMtptoykd74ISOG2AyQ6vw98WzbmD0GKQDroIG_1bafHEGx1ftm6HwykYYnXDhxrJRqApc5-TiRkLY_XV/s1600/2013-04-27-220529_3840x1080_scrot.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3bcc5VqVyUBD_-BsFcuPqmqs6Cx8jcGXsjlShPdo-ZN3-iNxUFBGCU6XCjeMuMtptoykd74ISOG2AyQ6vw98WzbmD0GKQDroIG_1bafHEGx1ftm6HwykYYnXDhxrJRqApc5-TiRkLY_XV/s640/2013-04-27-220529_3840x1080_scrot.png" width="640" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Openbox OSD Script Volume 59%</td></tr>
</tbody></table>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_a1wmMK03Zsop3JF2bHYBkSuupMuAXM_45Bb2w7r2JEP0nqzEpqWlNuiRt082ivhi0hp5k2AdlomAbEhLHsVQIgAgiDBegTPG_6q05_oEskM8s6dfMY0-mhgwzAWWZLxr9NA2BdUwlYg2/s1600/out2.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_a1wmMK03Zsop3JF2bHYBkSuupMuAXM_45Bb2w7r2JEP0nqzEpqWlNuiRt082ivhi0hp5k2AdlomAbEhLHsVQIgAgiDBegTPG_6q05_oEskM8s6dfMY0-mhgwzAWWZLxr9NA2BdUwlYg2/s640/out2.png" width="640" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Openbox OSD Script Muted Volume</td></tr>
</tbody></table>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com2tag:blogger.com,1999:blog-8442453793803646219.post-41220051475165558342012-09-27T20:06:00.000+02:002012-09-27T20:06:08.883+02:00Adjust pdf quality with Ghostscript and BashSometimes pdf files are too big to attach them to an email. Here is a
simple bash script witch uses Ghostscript to adjust a pdf file quality
(and size) to three different levels: low, mid and high.<br />
To see the script usage just type <b>./pdf-quality.sh</b> on the terminal. Don't forget to give it execution permissions: <b>chmod u+x pdf-quality.sh</b><br />
<h4>
</h4>
<h4>
Code</h4>
<html>
<head>
<title>.pdf-quality.sh.tmp</title>
<style type="text/css">
<!--
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:9pt; font-family:'monospace';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
//</style></head></html>
<br />
<pre class="hl"> <span class="hl num">1</span> <span class="hl slc">#/bin/bash</span>
<span class="hl num">2</span>
<span class="hl num">3</span>
<span class="hl num">4</span> <span class="hl slc"># function to print proper usage</span>
<span class="hl num">5</span> <span class="hl kwa">function</span> usage<span class="hl opt">() {</span>
<span class="hl num">6</span> <span class="hl kwb">echo</span> <span class="hl str">"Usage: pdf-quality.sh <quality> <input_file>"</span>
<span class="hl num">7</span> <span class="hl kwb">echo</span> <span class="hl str">"Where <quality> is low, mid or high and <input_file> the pdf file to be adjusted."</span>
<span class="hl num">8</span> <span class="hl opt">}</span>
<span class="hl num">9</span>
<span class="hl num">10</span>
<span class="hl num">11</span> <span class="hl slc"># check parameter number</span>
<span class="hl num">12</span> <span class="hl kwa">if</span> <span class="hl opt">[[</span> <span class="hl kwd">$#</span> <span class="hl opt">!=</span> <span class="hl num">2</span> <span class="hl opt">]];</span> <span class="hl kwa">then</span> usage<span class="hl opt">;</span> <span class="hl kwb">exit</span> <span class="hl num">1</span><span class="hl opt">;</span> <span class="hl kwa">fi</span>
<span class="hl num">13</span>
<span class="hl num">14</span> <span class="hl slc"># set variables</span>
<span class="hl num">15</span> QUALITY<span class="hl opt">=</span><span class="hl str">"$1"</span>
<span class="hl num">16</span> INFILE<span class="hl opt">=</span><span class="hl str">"$2"</span>
<span class="hl num">17</span> OUTFILE<span class="hl opt">=</span><span class="hl str">"$(echo "</span><span class="hl kwd">$2</span><span class="hl str">" | sed 's/.pdf//')-$QUALITY.pdf"</span>
<span class="hl num">18</span>
<span class="hl num">19</span> <span class="hl slc"># perform action depending on the given parameter</span>
<span class="hl num">20</span> <span class="hl kwa">case</span> <span class="hl str">"$QUALITY"</span> <span class="hl kwa">in</span>
<span class="hl num">21</span> <span class="hl str">"low"</span><span class="hl opt">)</span>
<span class="hl num">22</span> gs <span class="hl opt">-</span>dNOPAUSE <span class="hl opt">-</span>dBATCH <span class="hl opt">-</span>sDEVICE<span class="hl opt">=</span>pdfwrite <span class="hl opt">-</span>dCompatibilityLevel<span class="hl opt">=</span><span class="hl num">1.4</span> <span class="hl opt">-</span>dPDFSETTINGS<span class="hl opt">=/</span>screen <span class="hl opt">-</span>
sOutputFile<span class="hl opt">=</span><span class="hl str">"$OUTFILE"</span> <span class="hl str">"$INFILE"</span><span class="hl opt">;;</span>
<span class="hl num">23</span> <span class="hl str">"mid"</span><span class="hl opt">)</span>
<span class="hl num">24</span> gs <span class="hl opt">-</span>dNOPAUSE <span class="hl opt">-</span>dBATCH <span class="hl opt">-</span>sDEVICE<span class="hl opt">=</span>pdfwrite <span class="hl opt">-</span>dCompatibilityLevel<span class="hl opt">=</span><span class="hl num">1.4</span> <span class="hl opt">-</span>dPDFSETTINGS<span class="hl opt">=/</span>ebook <span class="hl opt">-</span>
sOutputFile<span class="hl opt">=</span><span class="hl str">"$OUTFILE"</span> <span class="hl str">"$INFILE"</span><span class="hl opt">;;</span>
<span class="hl num">25</span> <span class="hl str">"high"</span><span class="hl opt">)</span>
<span class="hl num">26</span> gs <span class="hl opt">-</span>dNOPAUSE <span class="hl opt">-</span>dBATCH <span class="hl opt">-</span>sDEVICE<span class="hl opt">=</span>pdfwrite <span class="hl opt">-</span>dCompatibilityLevel<span class="hl opt">=</span><span class="hl num">1.4</span> <span class="hl opt">-</span>dPDFSETTINGS<span class="hl opt">=/</span>printer <span class="hl opt">-</span>
sOutputFile<span class="hl opt">=</span><span class="hl str">"$OUTFILE"</span> <span class="hl str">"$INFILE"</span><span class="hl opt">;;</span>
<span class="hl num">27</span> <span class="hl opt">*)</span> <span class="hl kwb">echo</span> <span class="hl str">"$1 is not a valid option."</span>
<span class="hl num">28</span> usage
<span class="hl num">29</span> <span class="hl kwb">exit</span> <span class="hl num">1</span><span class="hl opt">;;</span>
<span class="hl num">30</span> <span class="hl kwa">esac</span>
<span class="hl num">31</span>
<span class="hl num">32</span> <span class="hl kwb">echo</span> <span class="hl opt">-</span>e <span class="hl str">"</span><span class="hl esc">\n</span><span class="hl str">all done!"</span>
<span class="hl num">33</span> <span class="hl kwb">exit</span> <span class="hl num">0</span>
</pre>
<br />
<!--HTML generated by highlight 3.9, http://www.andre-simon.de/-->
Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com1tag:blogger.com,1999:blog-8442453793803646219.post-24717828930465501742012-08-22T13:19:00.000+02:002012-08-22T13:19:12.837+02:00python: yat (yet another tetris)<br />
Who doesn't want to write its own version of one of the most popular games in the video game history, huh?<br />
<br />
Here is YAT, my tetris version written in python using the popular game developing module <a href="http://www.pygame.org/">pygame</a>.<br />
<br />
Source code is available <a href="https://docs.google.com/open?id=0B3ekw2gUVlP-d3JpbC1pMVJrdk0">here</a>.<br />
<h3>
Screenshots </h3>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhty1Tg6-8IqQazWzOtUOeq6qYsAdDItvSzBgX3Pj8K_nfSx3FL-FX8lvwmV-BsgtimdLIHhnhqfv4d1RV-_GxnlEWObBaTSOnj7wkX4p6WJXvPTuwzqYc429_pvJPhbwPOCLPXm4HOJBAc/s1600/yat1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhty1Tg6-8IqQazWzOtUOeq6qYsAdDItvSzBgX3Pj8K_nfSx3FL-FX8lvwmV-BsgtimdLIHhnhqfv4d1RV-_GxnlEWObBaTSOnj7wkX4p6WJXvPTuwzqYc429_pvJPhbwPOCLPXm4HOJBAc/s400/yat1.png" width="203" /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgciVGl017PY3_SqT2srEPyWvvg2G_uInQlxHWKVmxgBg-aAqSTDCAZ-yMUUWhI6WSyyr8JczODR1qmVfojaetiEyYqESnQyrNW7yBLp_tEdhTgMltPF4k9Nc0soZYGwIxguGXqxqvKAqbx/s1600/yat2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgciVGl017PY3_SqT2srEPyWvvg2G_uInQlxHWKVmxgBg-aAqSTDCAZ-yMUUWhI6WSyyr8JczODR1qmVfojaetiEyYqESnQyrNW7yBLp_tEdhTgMltPF4k9Nc0soZYGwIxguGXqxqvKAqbx/s400/yat2.png" width="202" /></a></div>
<br />
<h3>
About surfaces:</h3>
I think, besides the game logic, the most interesting thing here is the use of two different surfaces to show the blocks and the game information on the screen (including the next block).<br />
This is managed in the main game file following. Basically I'm updating the game information surface with the "updateInfo" function and the blocks screen with the table class method "show". Both functions update the game surface "screen" with their own surfaces by themselves. <br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<title>.tempfile</title>
<style type="text/css">
<!--
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:11pt; font-family:'monospace';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
//-->
</style>
<pre class="hl"> <span class="hl num">1</span> <span class="hl slc">#======================================================================#</span>
<span class="hl num">2</span> <span class="hl slc"># This file is part of YAT (Yet Another Tetris). #</span>
<span class="hl num">3</span> <span class="hl slc"># #</span>
<span class="hl num">4</span> <span class="hl slc"># YAT is free software: you can redistribute it and/or modify #</span>
<span class="hl num">5</span> <span class="hl slc"># it under the terms of the GNU General Public License as published by #</span>
<span class="hl num">6</span> <span class="hl slc"># the Free Software Foundation, either version 3 of the License, or #</span>
<span class="hl num">7</span> <span class="hl slc"># (at your option) any later version. #</span>
<span class="hl num">8</span> <span class="hl slc"># #</span>
<span class="hl num">9</span> <span class="hl slc"># YAT is distributed in the hope that it will be useful, #</span>
<span class="hl num">10</span> <span class="hl slc"># but WITHOUT ANY WARRANTY; without even the implied warranty of #</span>
<span class="hl num">11</span> <span class="hl slc"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #</span>
<span class="hl num">12</span> <span class="hl slc"># GNU General Public License for more details. #</span>
<span class="hl num">13</span> <span class="hl slc"># #</span>
<span class="hl num">14</span> <span class="hl slc"># You should have received a copy of the GNU General Public License #</span>
<span class="hl num">15</span> <span class="hl slc"># along with YAT. If not, see <http://www.gnu.org/licenses/>. #</span>
<span class="hl num">16</span> <span class="hl slc">#======================================================================#</span>
<span class="hl num">17</span>
<span class="hl num">18</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">19</span> <span class="hl slc"># Name : yat.py (Yet Another Tetris) #</span>
<span class="hl num">20</span> <span class="hl slc"># Description: Main game file for the tetris like game "yat" #</span>
<span class="hl num">21</span> <span class="hl slc"># Author : Adrian Antonana #</span>
<span class="hl num">22</span> <span class="hl slc"># Date : 17.08.2012 #</span>
<span class="hl num">23</span> <span class="hl slc"># Copyright : Adrian Antonana 2012 #</span>
<span class="hl num">24</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">25</span> <span class="hl kwa">import</span> pygame <span class="hl kwa">as</span> pg
<span class="hl num">26</span> <span class="hl kwa">import</span> table <span class="hl kwa">as</span> tb
<span class="hl num">27</span> <span class="hl kwa">import</span> blocks <span class="hl kwa">as</span> bk
<span class="hl num">28</span> <span class="hl kwa">import</span> colors <span class="hl kwa">as</span> col
<span class="hl num">29</span>
<span class="hl num">30</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">31</span> <span class="hl slc"># Global Variables and Constants #</span>
<span class="hl num">32</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">33</span> GAME_SPEED <span class="hl opt">=</span> <span class="hl num">500</span>
<span class="hl num">34</span> SPEED_INC_TICK <span class="hl opt">=</span> <span class="hl num">50</span>
<span class="hl num">35</span> LINES_INC_TICK <span class="hl opt">=</span> <span class="hl num">10</span>
<span class="hl num">36</span> LEVEL <span class="hl opt">=</span> <span class="hl num">1</span>
<span class="hl num">37</span> REMOVED_LINES <span class="hl opt">=</span> <span class="hl num">0</span>
<span class="hl num">38</span> MAX_LEVEL <span class="hl opt">=</span> <span class="hl num">10</span>
<span class="hl num">39</span> FPS <span class="hl opt">=</span> <span class="hl num">100</span>
<span class="hl num">40</span>
<span class="hl num">41</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">42</span> <span class="hl slc"># Function Definitions #</span>
<span class="hl num">43</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">44</span>
<span class="hl num">45</span> <span class="hl slc">#-------------------- Checks when blocks have to move down ----------------#</span>
<span class="hl num">46</span> <span class="hl kwa">def</span> <span class="hl kwd">delay</span><span class="hl opt">(</span>ticks<span class="hl opt">):</span>
<span class="hl num">47</span> <span class="hl kwa">return</span> <span class="hl opt">(</span>ticks <span class="hl opt">%</span> GAME_SPEED<span class="hl opt">) >=</span> GAME_SPEED<span class="hl opt">-</span><span class="hl num">10</span>
<span class="hl num">48</span>
<span class="hl num">49</span> <span class="hl slc">#------------------ Increases the Game Level and Game Speed ---------------#</span>
<span class="hl num">50</span> <span class="hl kwa">def</span> <span class="hl kwd">incSpeed</span><span class="hl opt">(</span>remlines<span class="hl opt">):</span>
<span class="hl num">51</span> <span class="hl kwa">global</span> GAME_SPEED
<span class="hl num">52</span> <span class="hl kwa">global</span> LEVEL
<span class="hl num">53</span>
<span class="hl num">54</span> <span class="hl kwa">if</span> LEVEL <span class="hl opt"><</span> MAX_LEVEL<span class="hl opt">:</span>
<span class="hl num">55</span> <span class="hl kwa">if</span> remlines <span class="hl opt">/ (</span>LEVEL<span class="hl opt">*</span>LINES_INC_TICK<span class="hl opt">) ==</span> <span class="hl num">1</span><span class="hl opt">:</span>
<span class="hl num">56</span> LEVEL <span class="hl opt">+=</span> <span class="hl num">1</span>
<span class="hl num">57</span> GAME_SPEED <span class="hl opt">-=</span> SPEED_INC_TICK
<span class="hl num">58</span> <span class="hl kwa">return True</span>
<span class="hl num">59</span> <span class="hl kwa">return False</span>
<span class="hl num">60</span>
<span class="hl num">61</span> <span class="hl slc">#---------------------- Updates the information surface -------------------#</span>
<span class="hl num">62</span> <span class="hl kwa">def</span> <span class="hl kwd">updateInfo</span><span class="hl opt">(</span>nb<span class="hl opt">):</span>
<span class="hl num">63</span> <span class="hl kwa">global</span> LEVEL_NUM_TEXT
<span class="hl num">64</span> <span class="hl kwa">global</span> LINES_NUM_TEXT
<span class="hl num">65</span> <span class="hl kwa">global</span> infosurface
<span class="hl num">66</span>
<span class="hl num">67</span> LEVEL_NUM_TEXT <span class="hl opt">=</span> font<span class="hl opt">.</span><span class="hl kwd">render</span><span class="hl opt">(</span><span class="hl kwb">str</span><span class="hl opt">(</span>LEVEL<span class="hl opt">),</span><span class="hl kwa">True</span><span class="hl opt">,</span>col<span class="hl opt">.</span>WHITE<span class="hl opt">)</span>
<span class="hl num">68</span> LINES_NUM_TEXT <span class="hl opt">=</span> font<span class="hl opt">.</span><span class="hl kwd">render</span><span class="hl opt">(</span><span class="hl kwb">str</span><span class="hl opt">(</span>REMOVED_LINES<span class="hl opt">),</span><span class="hl kwa">True</span><span class="hl opt">,</span>col<span class="hl opt">.</span>WHITE<span class="hl opt">)</span>
<span class="hl num">69</span> infosurface<span class="hl opt">.</span><span class="hl kwd">fill</span><span class="hl opt">(</span>col<span class="hl opt">.</span>GREY_DARK<span class="hl opt">)</span>
<span class="hl num">70</span> infosurface<span class="hl opt">.</span><span class="hl kwd">blit</span><span class="hl opt">(</span>LEVEL_TEXT<span class="hl opt">,</span>LEVEL_TEXT_OFFSET<span class="hl opt">)</span>
<span class="hl num">71</span> infosurface<span class="hl opt">.</span><span class="hl kwd">blit</span><span class="hl opt">(</span>LEVEL_NUM_TEXT<span class="hl opt">,</span>LEVEL_NUM_TEXT_OFFSET<span class="hl opt">)</span>
<span class="hl num">72</span> infosurface<span class="hl opt">.</span><span class="hl kwd">blit</span><span class="hl opt">(</span>LINES_TEXT<span class="hl opt">,</span>LINES_TEXT_OFFSET<span class="hl opt">)</span>
<span class="hl num">73</span> infosurface<span class="hl opt">.</span><span class="hl kwd">blit</span><span class="hl opt">(</span>LINES_NUM_TEXT<span class="hl opt">,</span>LINES_NUM_TEXT_OFFSET<span class="hl opt">)</span>
<span class="hl num">74</span> nb<span class="hl opt">.</span><span class="hl kwd">show</span><span class="hl opt">(</span>infosurface<span class="hl opt">,</span>NEXT_BLOCK_OFFSET<span class="hl opt">,</span><span class="hl num">20</span><span class="hl opt">,</span>INF_BLOCK_SIZE<span class="hl opt">)</span>
<span class="hl num">75</span>
<span class="hl num">76</span>
<span class="hl num">77</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">78</span> <span class="hl slc"># Initialize pygame (display,mixer and clock) #</span>
<span class="hl num">79</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">80</span> pg<span class="hl opt">.</span><span class="hl kwd">init</span><span class="hl opt">()</span>
<span class="hl num">81</span> pg<span class="hl opt">.</span>mixer<span class="hl opt">.</span><span class="hl kwd">init</span><span class="hl opt">()</span>
<span class="hl num">82</span> sndblockplaced <span class="hl opt">=</span> pg<span class="hl opt">.</span>mixer<span class="hl opt">.</span><span class="hl kwd">Sound</span><span class="hl opt">(</span><span class="hl str">"sounds/block_placed.wav"</span><span class="hl opt">)</span>
<span class="hl num">83</span> sndblockrotate <span class="hl opt">=</span> pg<span class="hl opt">.</span>mixer<span class="hl opt">.</span><span class="hl kwd">Sound</span><span class="hl opt">(</span><span class="hl str">"sounds/block_rotate.wav"</span><span class="hl opt">)</span>
<span class="hl num">84</span> sndremovelines <span class="hl opt">=</span> pg<span class="hl opt">.</span>mixer<span class="hl opt">.</span><span class="hl kwd">Sound</span><span class="hl opt">(</span><span class="hl str">"sounds/remove_lines.wav"</span><span class="hl opt">)</span>
<span class="hl num">85</span> sndlevelup <span class="hl opt">=</span> pg<span class="hl opt">.</span>mixer<span class="hl opt">.</span><span class="hl kwd">Sound</span><span class="hl opt">(</span><span class="hl str">"sounds/level_up.wav"</span><span class="hl opt">)</span>
<span class="hl num">86</span> sndgameover <span class="hl opt">=</span> pg<span class="hl opt">.</span>mixer<span class="hl opt">.</span><span class="hl kwd">Sound</span><span class="hl opt">(</span><span class="hl str">"sounds/game_over.wav"</span><span class="hl opt">)</span>
<span class="hl num">87</span> clock <span class="hl opt">=</span> pg<span class="hl opt">.</span>time<span class="hl opt">.</span><span class="hl kwd">Clock</span><span class="hl opt">()</span>
<span class="hl num">88</span> pg<span class="hl opt">.</span>display<span class="hl opt">.</span><span class="hl kwd">set_caption</span><span class="hl opt">(</span><span class="hl str">"yat - yet another tetris"</span><span class="hl opt">)</span>
<span class="hl num">89</span> pg<span class="hl opt">.</span>key<span class="hl opt">.</span><span class="hl kwd">set_repeat</span><span class="hl opt">(</span><span class="hl num">10</span><span class="hl opt">,</span><span class="hl num">50</span><span class="hl opt">)</span>
<span class="hl num">90</span>
<span class="hl num">91</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">92</span> <span class="hl slc"># Information surface #</span>
<span class="hl num">93</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">94</span> INFO_SURFACE_HEIGHT <span class="hl opt">=</span> <span class="hl num">105</span>
<span class="hl num">95</span> FONT_SIZE <span class="hl opt">=</span> <span class="hl num">30</span>
<span class="hl num">96</span> FONT_SIZE_GAME_OVER <span class="hl opt">=</span> <span class="hl num">60</span>
<span class="hl num">97</span> UPPER_OFFSET <span class="hl opt">=</span> <span class="hl num">20</span>
<span class="hl num">98</span> LEFT_OFFSET <span class="hl opt">=</span> <span class="hl num">10</span>
<span class="hl num">99</span> INF_BLOCK_SIZE <span class="hl opt">=</span> <span class="hl num">20</span>
<span class="hl num">100</span> font <span class="hl opt">=</span> pg<span class="hl opt">.</span>font<span class="hl opt">.</span><span class="hl kwd">SysFont</span><span class="hl opt">(</span>pg<span class="hl opt">.</span>font<span class="hl opt">.</span><span class="hl kwd">get_default_font</span><span class="hl opt">(),</span>FONT_SIZE<span class="hl opt">)</span>
<span class="hl num">101</span> font_game_over <span class="hl opt">=</span> pg<span class="hl opt">.</span>font<span class="hl opt">.</span><span class="hl kwd">SysFont</span><span class="hl opt">(</span>pg<span class="hl opt">.</span>font<span class="hl opt">.</span><span class="hl kwd">get_default_font</span><span class="hl opt">(),</span>
FONT_SIZE_GAME_OVER<span class="hl opt">)</span>
<span class="hl num">102</span> LEVEL_TEXT <span class="hl opt">=</span> font<span class="hl opt">.</span><span class="hl kwd">render</span><span class="hl opt">(</span><span class="hl str">"Level : "</span><span class="hl opt">,</span><span class="hl kwa">True</span><span class="hl opt">,</span>col<span class="hl opt">.</span>WHITE<span class="hl opt">)</span>
<span class="hl num">103</span> LINES_TEXT <span class="hl opt">=</span> font<span class="hl opt">.</span><span class="hl kwd">render</span><span class="hl opt">(</span><span class="hl str">"Lines : "</span><span class="hl opt">,</span><span class="hl kwa">True</span><span class="hl opt">,</span>col<span class="hl opt">.</span>WHITE<span class="hl opt">)</span>
<span class="hl num">104</span> LEVEL_TEXT_OFFSET <span class="hl opt">= (</span>LEFT_OFFSET<span class="hl opt">,</span>UPPER_OFFSET<span class="hl opt">)</span>
<span class="hl num">105</span> LEVEL_NUM_TEXT_OFFSET <span class="hl opt">= (</span><span class="hl num">70</span><span class="hl opt">+</span>LEFT_OFFSET<span class="hl opt">,</span>UPPER_OFFSET<span class="hl opt">)</span>
<span class="hl num">106</span> LINES_TEXT_OFFSET <span class="hl opt">= (</span>LEFT_OFFSET<span class="hl opt">,</span>INFO_SURFACE_HEIGHT<span class="hl opt">-</span><span class="hl num">40</span><span class="hl opt">)</span>
<span class="hl num">107</span> LINES_NUM_TEXT_OFFSET <span class="hl opt">= (</span><span class="hl num">70</span><span class="hl opt">+</span>LEFT_OFFSET<span class="hl opt">,</span>INFO_SURFACE_HEIGHT<span class="hl opt">-</span><span class="hl num">40</span><span class="hl opt">)</span>
<span class="hl num">108</span> NEXT_BLOCK_OFFSET <span class="hl opt">=</span> tb<span class="hl opt">.</span>BLOCK_SIZE<span class="hl opt">*</span>tb<span class="hl opt">.</span>WIDTH <span class="hl opt">-</span> INF_BLOCK_SIZE <span class="hl opt">*</span> <span class="hl num">5</span>
<span class="hl num">109</span>
<span class="hl num">110</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">111</span> <span class="hl slc"># Game over text #</span>
<span class="hl num">112</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">113</span> GAME_OVER_TEXT <span class="hl opt">=</span> font_game_over<span class="hl opt">.</span><span class="hl kwd">render</span><span class="hl opt">(</span><span class="hl str">"GAME OVER"</span><span class="hl opt">,</span><span class="hl kwa">True</span><span class="hl opt">,</span>col<span class="hl opt">.</span>WHITE<span class="hl opt">)</span>
<span class="hl num">114</span> GAME_OVER_TEXT_OFFSET <span class="hl opt">= ((</span>tb<span class="hl opt">.</span>BLOCK_SIZE<span class="hl opt">*</span>tb<span class="hl opt">.</span>WIDTH<span class="hl opt">/</span><span class="hl num">2</span><span class="hl opt">)-</span><span class="hl num">120</span><span class="hl opt">,(</span>tb<span class="hl opt">.</span>BLOCK_SIZE<span class="hl opt">*</span>tb<span class="hl opt">.</span>
HEIGHT<span class="hl opt">/</span><span class="hl num">2</span><span class="hl opt">)-</span><span class="hl num">50</span><span class="hl opt">)</span>
<span class="hl num">115</span>
<span class="hl num">116</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">117</span> <span class="hl slc"># Initialize surfaces #</span>
<span class="hl num">118</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">119</span> screen <span class="hl opt">=</span> pg<span class="hl opt">.</span>display<span class="hl opt">.</span><span class="hl kwd">set_mode</span><span class="hl opt">((</span>tb<span class="hl opt">.</span>BLOCK_SIZE<span class="hl opt">*</span>tb<span class="hl opt">.</span>WIDTH<span class="hl opt">,</span>tb<span class="hl opt">.</span>
BLOCK_SIZE<span class="hl opt">*</span>tb<span class="hl opt">.</span>HEIGHT<span class="hl opt">+</span>INFO_SURFACE_HEIGHT<span class="hl opt">))</span>
<span class="hl num">120</span> tablesurface <span class="hl opt">=</span> screen<span class="hl opt">.</span><span class="hl kwd">subsurface</span><span class="hl opt">((</span><span class="hl num">0</span><span class="hl opt">,</span>INFO_SURFACE_HEIGHT<span class="hl opt">,</span>tb<span class="hl opt">.</span>BLOCK_SIZE<span class="hl opt">*</span>tb<span class="hl opt">.</span>
WIDTH<span class="hl opt">,</span>tb<span class="hl opt">.</span>BLOCK_SIZE<span class="hl opt">*</span>tb<span class="hl opt">.</span>HEIGHT<span class="hl opt">))</span>
<span class="hl num">121</span> infosurface <span class="hl opt">=</span> screen<span class="hl opt">.</span><span class="hl kwd">subsurface</span><span class="hl opt">((</span><span class="hl num">0</span><span class="hl opt">,</span><span class="hl num">0</span><span class="hl opt">,</span>tb<span class="hl opt">.</span>BLOCK_SIZE<span class="hl opt">*</span>tb<span class="hl opt">.</span>WIDTH<span class="hl opt">,</span>
INFO_SURFACE_HEIGHT<span class="hl opt">))</span>
<span class="hl num">122</span>
<span class="hl num">123</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">124</span> <span class="hl slc"># Block spawn position #</span>
<span class="hl num">125</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">126</span> BLOCK_SPAWN_POS <span class="hl opt">= (</span><span class="hl num">0</span><span class="hl opt">,(</span>tb<span class="hl opt">.</span>WIDTH<span class="hl opt">/</span><span class="hl num">2</span><span class="hl opt">)-</span><span class="hl num">1</span><span class="hl opt">)</span>
<span class="hl num">127</span>
<span class="hl num">128</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">129</span> <span class="hl slc"># Create the table and an initial block #</span>
<span class="hl num">130</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">131</span> t <span class="hl opt">=</span> tb<span class="hl opt">.</span><span class="hl kwd">table</span><span class="hl opt">(</span>tablesurface<span class="hl opt">)</span>
<span class="hl num">132</span> b <span class="hl opt">=</span> bk<span class="hl opt">.</span><span class="hl kwd">block</span><span class="hl opt">(</span>BLOCK_SPAWN_POS<span class="hl opt">)</span>
<span class="hl num">133</span> nextb <span class="hl opt">=</span> bk<span class="hl opt">.</span><span class="hl kwd">block</span><span class="hl opt">(</span>BLOCK_SPAWN_POS<span class="hl opt">)</span>
<span class="hl num">134</span>
<span class="hl num">135</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">136</span> <span class="hl slc"># Draw initial information surface #</span>
<span class="hl num">137</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">138</span> <span class="hl kwd">updateInfo</span><span class="hl opt">(</span>nextb<span class="hl opt">)</span>
<span class="hl num">139</span>
<span class="hl num">140</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">141</span> <span class="hl slc"># Main loop #</span>
<span class="hl num">142</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">143</span> running <span class="hl opt">=</span> <span class="hl kwa">True</span>
<span class="hl num">144</span>
<span class="hl num">145</span> <span class="hl kwa">while</span> running<span class="hl opt">:</span>
<span class="hl num">146</span> clock<span class="hl opt">.</span><span class="hl kwd">tick_busy_loop</span><span class="hl opt">(</span>FPS<span class="hl opt">)</span>
<span class="hl num">147</span> t<span class="hl opt">.</span><span class="hl kwd">adBlock</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">(),</span>b<span class="hl opt">.</span><span class="hl kwd">getType</span><span class="hl opt">())</span>
<span class="hl num">148</span> t<span class="hl opt">.</span><span class="hl kwd">show</span><span class="hl opt">()</span>
<span class="hl num">149</span>
<span class="hl num">150</span> <span class="hl slc"># check if the fall delay has been reached. If yes, move block down.</span>
<span class="hl num">151</span> <span class="hl kwa">if</span> <span class="hl kwd">delay</span><span class="hl opt">(</span>pg<span class="hl opt">.</span>time<span class="hl opt">.</span><span class="hl kwd">get_ticks</span><span class="hl opt">()):</span>
<span class="hl num">152</span> <span class="hl kwa">if</span> b<span class="hl opt">.</span><span class="hl kwd">canMovDown</span><span class="hl opt">(</span>t<span class="hl opt">.</span><span class="hl kwd">getHeight</span><span class="hl opt">(),</span>t<span class="hl opt">.</span><span class="hl kwd">getOcupPosList</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">())):</span>
<span class="hl num">153</span> t<span class="hl opt">.</span><span class="hl kwd">adBlock</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">(),</span>bk<span class="hl opt">.</span>E<span class="hl opt">)</span>
<span class="hl num">154</span> b<span class="hl opt">.</span><span class="hl kwd">movDown</span><span class="hl opt">()</span>
<span class="hl num">155</span> <span class="hl kwa">else</span><span class="hl opt">:</span>
<span class="hl num">156</span> <span class="hl slc"># if the block can't move down, spawn a new block</span>
<span class="hl num">157</span> t<span class="hl opt">.</span><span class="hl kwd">adBlock</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">(),</span>b<span class="hl opt">.</span><span class="hl kwd">getType</span><span class="hl opt">())</span>
<span class="hl num">158</span> sndblockplaced<span class="hl opt">.</span><span class="hl kwd">play</span><span class="hl opt">()</span>
<span class="hl num">159</span> retval <span class="hl opt">=</span> t<span class="hl opt">.</span><span class="hl kwd">delFullLines</span><span class="hl opt">()</span>
<span class="hl num">160</span> <span class="hl kwa">if</span> retval <span class="hl opt">!=</span> <span class="hl num">0</span><span class="hl opt">:</span>
<span class="hl num">161</span> sndremovelines<span class="hl opt">.</span><span class="hl kwd">play</span><span class="hl opt">()</span>
<span class="hl num">162</span> REMOVED_LINES <span class="hl opt">+=</span> retval
<span class="hl num">163</span> <span class="hl kwa">if</span> <span class="hl kwd">incSpeed</span><span class="hl opt">(</span>REMOVED_LINES<span class="hl opt">):</span>
<span class="hl num">164</span> sndlevelup<span class="hl opt">.</span><span class="hl kwd">play</span><span class="hl opt">()</span>
<span class="hl num">165</span> b<span class="hl opt">.</span><span class="hl kwd">__init__</span><span class="hl opt">(</span>BLOCK_SPAWN_POS<span class="hl opt">,</span>nextb<span class="hl opt">.</span><span class="hl kwd">getType</span><span class="hl opt">())</span>
<span class="hl num">166</span> nextb <span class="hl opt">=</span> bk<span class="hl opt">.</span><span class="hl kwd">block</span><span class="hl opt">(</span>BLOCK_SPAWN_POS<span class="hl opt">)</span>
<span class="hl num">167</span> <span class="hl kwd">updateInfo</span><span class="hl opt">(</span>nextb<span class="hl opt">)</span>
<span class="hl num">168</span>
<span class="hl num">169</span> <span class="hl slc"># check if the game is over</span>
<span class="hl num">170</span> <span class="hl kwa">if</span> t<span class="hl opt">.</span><span class="hl kwd">gameOver</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">()):</span>
<span class="hl num">171</span> t<span class="hl opt">.</span><span class="hl kwd">adBlock</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">(),</span>b<span class="hl opt">.</span><span class="hl kwd">getType</span><span class="hl opt">())</span>
<span class="hl num">172</span> t<span class="hl opt">.</span><span class="hl kwd">show</span><span class="hl opt">()</span>
<span class="hl num">173</span> running <span class="hl opt">=</span> <span class="hl kwa">False</span>
<span class="hl num">174</span>
<span class="hl num">175</span> <span class="hl slc"># get one event from the queue and perform action</span>
<span class="hl num">176</span> event <span class="hl opt">=</span> pg<span class="hl opt">.</span>event<span class="hl opt">.</span><span class="hl kwd">poll</span><span class="hl opt">()</span>
<span class="hl num">177</span> <span class="hl kwa">if</span> event<span class="hl opt">.</span><span class="hl kwb">type</span> <span class="hl opt">==</span> pg<span class="hl opt">.</span>KEYDOWN<span class="hl opt">:</span>
<span class="hl num">178</span> key <span class="hl opt">=</span> event<span class="hl opt">.</span>key
<span class="hl num">179</span> <span class="hl kwa">if</span> key <span class="hl opt">==</span> pg<span class="hl opt">.</span>K_ESCAPE<span class="hl opt">:</span>
<span class="hl num">180</span> running <span class="hl opt">=</span> <span class="hl kwa">False</span>
<span class="hl num">181</span> <span class="hl kwa">elif</span> key <span class="hl opt">==</span> pg<span class="hl opt">.</span>K_DOWN<span class="hl opt">:</span>
<span class="hl num">182</span> <span class="hl kwa">if</span> b<span class="hl opt">.</span><span class="hl kwd">canMovDown</span><span class="hl opt">(</span>t<span class="hl opt">.</span><span class="hl kwd">getHeight</span><span class="hl opt">(),</span>t<span class="hl opt">.</span><span class="hl kwd">getOcupPosList</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">())):</span>
<span class="hl num">183</span> t<span class="hl opt">.</span><span class="hl kwd">adBlock</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">(),</span>bk<span class="hl opt">.</span>E<span class="hl opt">)</span>
<span class="hl num">184</span> b<span class="hl opt">.</span><span class="hl kwd">movDown</span><span class="hl opt">()</span>
<span class="hl num">185</span> <span class="hl kwa">else</span><span class="hl opt">:</span>
<span class="hl num">186</span> <span class="hl slc"># if the block can't move down, spawn a new block</span>
<span class="hl num">187</span> t<span class="hl opt">.</span><span class="hl kwd">adBlock</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">(),</span>b<span class="hl opt">.</span><span class="hl kwd">getType</span><span class="hl opt">())</span>
<span class="hl num">188</span> sndblockplaced<span class="hl opt">.</span><span class="hl kwd">play</span><span class="hl opt">()</span>
<span class="hl num">189</span> retval <span class="hl opt">=</span> t<span class="hl opt">.</span><span class="hl kwd">delFullLines</span><span class="hl opt">()</span>
<span class="hl num">190</span> <span class="hl kwa">if</span> retval <span class="hl opt">!=</span> <span class="hl num">0</span><span class="hl opt">:</span>
<span class="hl num">191</span> sndremovelines<span class="hl opt">.</span><span class="hl kwd">play</span><span class="hl opt">()</span>
<span class="hl num">192</span> REMOVED_LINES <span class="hl opt">+=</span> retval
<span class="hl num">193</span> <span class="hl kwa">if</span> <span class="hl kwd">incSpeed</span><span class="hl opt">(</span>REMOVED_LINES<span class="hl opt">):</span>
<span class="hl num">194</span> sndlevelup<span class="hl opt">.</span><span class="hl kwd">play</span><span class="hl opt">()</span>
<span class="hl num">195</span> b<span class="hl opt">.</span><span class="hl kwd">__init__</span><span class="hl opt">(</span>BLOCK_SPAWN_POS<span class="hl opt">,</span>nextb<span class="hl opt">.</span><span class="hl kwd">getType</span><span class="hl opt">())</span>
<span class="hl num">196</span> nextb <span class="hl opt">=</span> bk<span class="hl opt">.</span><span class="hl kwd">block</span><span class="hl opt">(</span>BLOCK_SPAWN_POS<span class="hl opt">)</span>
<span class="hl num">197</span> <span class="hl kwd">updateInfo</span><span class="hl opt">(</span>nextb<span class="hl opt">)</span>
<span class="hl num">198</span>
<span class="hl num">199</span> <span class="hl slc"># check if the game is over</span>
<span class="hl num">200</span> <span class="hl kwa">if</span> t<span class="hl opt">.</span><span class="hl kwd">gameOver</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">()):</span>
<span class="hl num">201</span> t<span class="hl opt">.</span><span class="hl kwd">adBlock</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">(),</span>b<span class="hl opt">.</span><span class="hl kwd">getType</span><span class="hl opt">())</span>
<span class="hl num">202</span> t<span class="hl opt">.</span><span class="hl kwd">show</span><span class="hl opt">()</span>
<span class="hl num">203</span> running <span class="hl opt">=</span> <span class="hl kwa">False</span>
<span class="hl num">204</span>
<span class="hl num">205</span> <span class="hl slc"># move/rotate block left/right</span>
<span class="hl num">206</span> <span class="hl kwa">elif</span> key <span class="hl opt">==</span> pg<span class="hl opt">.</span>K_LEFT<span class="hl opt">:</span>
<span class="hl num">207</span> <span class="hl kwa">if</span> b<span class="hl opt">.</span><span class="hl kwd">canMovLeft</span><span class="hl opt">(</span>t<span class="hl opt">.</span><span class="hl kwd">getWidth</span><span class="hl opt">(),</span>t<span class="hl opt">.</span><span class="hl kwd">getOcupPosList</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">())):</span>
<span class="hl num">208</span> t<span class="hl opt">.</span><span class="hl kwd">adBlock</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">(),</span>bk<span class="hl opt">.</span>E<span class="hl opt">)</span>
<span class="hl num">209</span> b<span class="hl opt">.</span><span class="hl kwd">movLeft</span><span class="hl opt">()</span>
<span class="hl num">210</span> <span class="hl kwa">elif</span> key <span class="hl opt">==</span> pg<span class="hl opt">.</span>K_RIGHT<span class="hl opt">:</span>
<span class="hl num">211</span> <span class="hl kwa">if</span> b<span class="hl opt">.</span><span class="hl kwd">canMovRight</span><span class="hl opt">(</span>t<span class="hl opt">.</span><span class="hl kwd">getWidth</span><span class="hl opt">(),</span>t<span class="hl opt">.</span><span class="hl kwd">getOcupPosList</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">())):</span>
<span class="hl num">212</span> t<span class="hl opt">.</span><span class="hl kwd">adBlock</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">(),</span>bk<span class="hl opt">.</span>E<span class="hl opt">)</span>
<span class="hl num">213</span> b<span class="hl opt">.</span><span class="hl kwd">movRight</span><span class="hl opt">()</span>
<span class="hl num">214</span> <span class="hl kwa">elif</span> key <span class="hl opt">==</span> pg<span class="hl opt">.</span>K_LCTRL<span class="hl opt">:</span>
<span class="hl num">215</span> t<span class="hl opt">.</span><span class="hl kwd">adBlock</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">(),</span>bk<span class="hl opt">.</span>E<span class="hl opt">)</span>
<span class="hl num">216</span> <span class="hl kwa">if</span> b<span class="hl opt">.</span><span class="hl kwd">rotLeft</span><span class="hl opt">(</span>t<span class="hl opt">.</span><span class="hl kwd">getHeight</span><span class="hl opt">(),</span>t<span class="hl opt">.</span><span class="hl kwd">getWidth</span><span class="hl opt">(),</span>t<span class="hl opt">.</span><span class="hl kwd">getOcupPosList</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">()</span>
<span class="hl opt">)):</span>
<span class="hl num">217</span> sndblockrotate<span class="hl opt">.</span><span class="hl kwd">play</span><span class="hl opt">()</span>
<span class="hl num">218</span> <span class="hl kwa">elif</span> key <span class="hl opt">==</span> pg<span class="hl opt">.</span>K_LALT<span class="hl opt">:</span>
<span class="hl num">219</span> t<span class="hl opt">.</span><span class="hl kwd">adBlock</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">(),</span>bk<span class="hl opt">.</span>E<span class="hl opt">)</span>
<span class="hl num">220</span> <span class="hl kwa">if</span> b<span class="hl opt">.</span><span class="hl kwd">rotRight</span><span class="hl opt">(</span>t<span class="hl opt">.</span><span class="hl kwd">getHeight</span><span class="hl opt">(),</span>t<span class="hl opt">.</span><span class="hl kwd">getWidth</span><span class="hl opt">(),</span>t<span class="hl opt">.</span><span class="hl kwd">getOcupPosList</span><span class="hl opt">(</span>b<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">(</span>
<span class="hl opt">))):</span>
<span class="hl num">221</span> sndblockrotate<span class="hl opt">.</span><span class="hl kwd">play</span><span class="hl opt">()</span>
<span class="hl num">222</span>
<span class="hl num">223</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">224</span> <span class="hl slc"># The game is over #</span>
<span class="hl num">225</span> <span class="hl slc">#==========================================================================#</span>
<span class="hl num">226</span> tablesurface<span class="hl opt">.</span><span class="hl kwd">fill</span><span class="hl opt">(</span>col<span class="hl opt">.</span>BLACK<span class="hl opt">)</span>
<span class="hl num">227</span> t<span class="hl opt">.</span><span class="hl kwd">setSurfAlpha</span><span class="hl opt">(</span><span class="hl num">60</span><span class="hl opt">)</span>
<span class="hl num">228</span> t<span class="hl opt">.</span><span class="hl kwd">show</span><span class="hl opt">()</span>
<span class="hl num">229</span> tablesurface<span class="hl opt">.</span><span class="hl kwd">blit</span><span class="hl opt">(</span>GAME_OVER_TEXT<span class="hl opt">,</span>GAME_OVER_TEXT_OFFSET<span class="hl opt">)</span>
<span class="hl num">230</span> pg<span class="hl opt">.</span>display<span class="hl opt">.</span><span class="hl kwd">flip</span><span class="hl opt">()</span>
<span class="hl num">231</span> sndgameover<span class="hl opt">.</span><span class="hl kwd">play</span><span class="hl opt">()</span>
<span class="hl num">232</span>
<span class="hl num">233</span> quit <span class="hl opt">=</span> <span class="hl kwa">False</span>
<span class="hl num">234</span> <span class="hl kwa">while not</span> quit<span class="hl opt">:</span>
<span class="hl num">235</span> event <span class="hl opt">=</span> pg<span class="hl opt">.</span>event<span class="hl opt">.</span><span class="hl kwd">wait</span><span class="hl opt">()</span>
<span class="hl num">236</span> <span class="hl kwa">if</span> event<span class="hl opt">.</span><span class="hl kwb">type</span> <span class="hl opt">==</span> pg<span class="hl opt">.</span>QUIT<span class="hl opt">:</span>
<span class="hl num">237</span> quit <span class="hl opt">=</span> <span class="hl kwa">True</span>
<span class="hl num">238</span> <span class="hl kwa">if</span> event<span class="hl opt">.</span><span class="hl kwb">type</span> <span class="hl opt">==</span> pg<span class="hl opt">.</span>KEYDOWN<span class="hl opt">:</span>
<span class="hl num">239</span> <span class="hl kwa">if</span> event<span class="hl opt">.</span>key <span class="hl opt">==</span> pg<span class="hl opt">.</span>K_ESCAPE<span class="hl opt">:</span>
<span class="hl num">240</span> quit <span class="hl opt">=</span> <span class="hl kwa">True</span>
</pre>
Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com3tag:blogger.com,1999:blog-8442453793803646219.post-37500248942942365092012-08-01T12:07:00.000+02:002012-08-01T12:12:09.164+02:00python: snake gameHere is my second try to write a simple game in <a href="http://www.python.org/">python</a> using <a href="http://www.pygame.org/">pygame</a>. I'm not posting here the whole source code since I don't want the post to be too long. The complete source code can be downloaded from <a href="https://docs.google.com/open?id=0B3ekw2gUVlP-ZGZzdC1ERWhtdms">this link</a>.<br />
<br />
First lets take a look at the game...<br />
<h4>
Snake Game Screen Cast: </h4>
<iframe allowfullscreen="allowfullscreen" frameborder="0" height="480" src="http://www.youtube.com/embed/sYCzenmlGvE" width="853"></iframe>
<br />
<h4>
</h4>
<h4>
Snake Class: </h4>
Besides pygame, im using three self made classes on the game:<br />
<ul>
<li>snake.py </li>
<li>food.py </li>
<li>colors.py</li>
</ul>
The source code has plenty of comments which should make it quite easy to understand what is going on.<br />
<title>snake.py</title>
<style type="text/css">
<!--
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:11pt; font-family:'terminus';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
//-->
</style>
<br />
<pre class="hl"><span class="hl lin"> 1 </span><span class="hl slc">#==============================================================================================#</span>
<span class="hl lin"> 2 </span><span class="hl slc"># Name : snake.py #</span>
<span class="hl lin"> 3 </span><span class="hl slc"># Description : The snake class definition for the snake game. #</span>
<span class="hl lin"> 4 </span><span class="hl slc"># Author : Adrian Antonana #</span>
<span class="hl lin"> 5 </span><span class="hl slc"># Date : 29.07.2012 #</span>
<span class="hl lin"> 6 </span><span class="hl slc">#==============================================================================================#</span>
<span class="hl lin"> 7 </span>
<span class="hl lin"> 8 </span><span class="hl slc"># imports</span>
<span class="hl lin"> 9 </span><span class="hl kwa">import</span> pygame <span class="hl kwa">as</span> pg
<span class="hl lin"> 10 </span><span class="hl kwa">from</span> colors <span class="hl kwa">import</span> <span class="hl opt">*</span>
<span class="hl lin"> 11 </span>
<span class="hl lin"> 12 </span><span class="hl slc"># motion direction constants</span>
<span class="hl lin"> 13 </span>UP <span class="hl opt">=</span> <span class="hl num">0</span>
<span class="hl lin"> 14 </span>DOWN <span class="hl opt">=</span> <span class="hl num">1</span>
<span class="hl lin"> 15 </span>LEFT <span class="hl opt">=</span> <span class="hl num">2</span>
<span class="hl lin"> 16 </span>RIGHT <span class="hl opt">=</span> <span class="hl num">3</span>
<span class="hl lin"> 17 </span>
<span class="hl lin"> 18 </span><span class="hl slc"># block sizes</span>
<span class="hl lin"> 19 </span>BLOCK_SIZE <span class="hl opt">=</span> <span class="hl num">30</span>
<span class="hl lin"> 20 </span>BLOCK_SIZE_INNER <span class="hl opt">=</span> <span class="hl num">20</span>
<span class="hl lin"> 21 </span>
<span class="hl lin"> 22 </span><span class="hl slc"># snake class definition</span>
<span class="hl lin"> 23 </span><span class="hl kwa">class</span> snake<span class="hl opt">:</span>
<span class="hl lin"> 24 </span>
<span class="hl lin"> 25 </span> <span class="hl slc"># constructor</span>
<span class="hl lin"> 26 </span> <span class="hl kwa">def</span> <span class="hl kwd">__init__</span><span class="hl opt">(</span>self<span class="hl opt">,</span>surface<span class="hl opt">,</span>headposx<span class="hl opt">=</span><span class="hl num">10</span><span class="hl opt">,</span>headposy<span class="hl opt">=</span><span class="hl num">10</span><span class="hl opt">):</span>
<span class="hl lin"> 27 </span> self<span class="hl opt">.</span>surface <span class="hl opt">=</span> surface
<span class="hl lin"> 28 </span> self<span class="hl opt">.</span>length <span class="hl opt">=</span> <span class="hl num">10</span>
<span class="hl lin"> 29 </span> self<span class="hl opt">.</span>poslist <span class="hl opt">= [(</span>headposx<span class="hl opt">,</span>y<span class="hl opt">)</span> <span class="hl kwa">for</span> y <span class="hl kwa">in</span> <span class="hl kwb">reversed</span><span class="hl opt">(</span><span class="hl kwb">range</span><span class="hl opt">(</span>headposy<span class="hl opt">-</span>self<span class="hl opt">.</span>length<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">,</span>headposy<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">))]</span>
<span class="hl lin"> 30 </span> self<span class="hl opt">.</span>motdir <span class="hl opt">=</span> RIGHT
<span class="hl lin"> 31 </span> self<span class="hl opt">.</span>crashed <span class="hl opt">=</span> <span class="hl kwa">False</span>
<span class="hl lin"> 32 </span>
<span class="hl lin"> 33 </span> <span class="hl slc"># for drawing the snake</span>
<span class="hl lin"> 34 </span> self<span class="hl opt">.</span>snakeblock <span class="hl opt">=</span> pg<span class="hl opt">.</span><span class="hl kwd">Surface</span><span class="hl opt">((</span>BLOCK_SIZE<span class="hl opt">,</span>BLOCK_SIZE<span class="hl opt">))</span>
<span class="hl lin"> 35 </span> self<span class="hl opt">.</span>snakeblock<span class="hl opt">.</span><span class="hl kwd">set_alpha</span><span class="hl opt">(</span><span class="hl num">255</span><span class="hl opt">)</span>
<span class="hl lin"> 36 </span> self<span class="hl opt">.</span>snakeblock<span class="hl opt">.</span><span class="hl kwd">fill</span><span class="hl opt">(</span>GREEN<span class="hl opt">)</span>
<span class="hl lin"> 37 </span> self<span class="hl opt">.</span>snakeblockdark <span class="hl opt">=</span> pg<span class="hl opt">.</span><span class="hl kwd">Surface</span><span class="hl opt">((</span>BLOCK_SIZE_INNER<span class="hl opt">,</span>BLOCK_SIZE_INNER<span class="hl opt">))</span>
<span class="hl lin"> 38 </span> self<span class="hl opt">.</span>snakeblockdark<span class="hl opt">.</span><span class="hl kwd">set_alpha</span><span class="hl opt">(</span><span class="hl num">255</span><span class="hl opt">)</span>
<span class="hl lin"> 39 </span> self<span class="hl opt">.</span>snakeblockdark<span class="hl opt">.</span><span class="hl kwd">fill</span><span class="hl opt">(</span>GREEN_DARK<span class="hl opt">)</span>
<span class="hl lin"> 40 </span>
<span class="hl lin"> 41 </span> <span class="hl slc"># for removing the snake</span>
<span class="hl lin"> 42 </span> self<span class="hl opt">.</span>backblock <span class="hl opt">=</span> pg<span class="hl opt">.</span><span class="hl kwd">Surface</span><span class="hl opt">((</span>BLOCK_SIZE<span class="hl opt">,</span>BLOCK_SIZE<span class="hl opt">))</span>
<span class="hl lin"> 43 </span> self<span class="hl opt">.</span>backblock<span class="hl opt">.</span><span class="hl kwd">set_alpha</span><span class="hl opt">(</span><span class="hl num">255</span><span class="hl opt">)</span>
<span class="hl lin"> 44 </span> self<span class="hl opt">.</span>backblock<span class="hl opt">.</span><span class="hl kwd">fill</span><span class="hl opt">(</span>BLACK<span class="hl opt">)</span>
<span class="hl lin"> 45 </span>
<span class="hl lin"> 46 </span> <span class="hl slc"># get snake's head position</span>
<span class="hl lin"> 47 </span> <span class="hl kwa">def</span> <span class="hl kwd">getHeadPos</span><span class="hl opt">(</span>self<span class="hl opt">):</span>
<span class="hl lin"> 48 </span> <span class="hl kwa">return</span> <span class="hl opt">(</span>self<span class="hl opt">.</span>poslist<span class="hl opt">[</span><span class="hl num">0</span><span class="hl opt">])</span>
<span class="hl lin"> 49 </span>
<span class="hl lin"> 50 </span> <span class="hl slc"># get the motion direction</span>
<span class="hl lin"> 51 </span> <span class="hl kwa">def</span> <span class="hl kwd">getMotionDir</span><span class="hl opt">(</span>self<span class="hl opt">):</span>
<span class="hl lin"> 52 </span> <span class="hl kwa">return</span> self<span class="hl opt">.</span>motdir
<span class="hl lin"> 53 </span>
<span class="hl lin"> 54 </span> <span class="hl slc"># get the snake positions list</span>
<span class="hl lin"> 55 </span> <span class="hl kwa">def</span> <span class="hl kwd">getPosList</span><span class="hl opt">(</span>self<span class="hl opt">):</span>
<span class="hl lin"> 56 </span> <span class="hl kwa">return</span> self<span class="hl opt">.</span>poslist
<span class="hl lin"> 57 </span>
<span class="hl lin"> 58 </span> <span class="hl slc"># set the motion direction</span>
<span class="hl lin"> 59 </span> <span class="hl kwa">def</span> <span class="hl kwd">setMotionDir</span><span class="hl opt">(</span>self<span class="hl opt">,</span>motdir<span class="hl opt">):</span>
<span class="hl lin"> 60 </span> self<span class="hl opt">.</span>motdir <span class="hl opt">=</span> motdir
<span class="hl lin"> 61 </span>
<span class="hl lin"> 62 </span> <span class="hl slc"># increase the snake length by one</span>
<span class="hl lin"> 63 </span> <span class="hl kwa">def</span> <span class="hl kwd">incLength</span><span class="hl opt">(</span>self<span class="hl opt">):</span>
<span class="hl lin"> 64 </span> self<span class="hl opt">.</span>length <span class="hl opt">+=</span> <span class="hl num">1</span>
<span class="hl lin"> 65 </span>
<span class="hl lin"> 66 </span> <span class="hl slc"># move the snake updates the positions list and checks if the snake has crashed</span>
<span class="hl lin"> 67 </span> <span class="hl kwa">def</span> <span class="hl kwd">move</span><span class="hl opt">(</span>self<span class="hl opt">):</span>
<span class="hl lin"> 68 </span> motdir <span class="hl opt">=</span> self<span class="hl opt">.</span><span class="hl kwd">getMotionDir</span><span class="hl opt">()</span>
<span class="hl lin"> 69 </span> headpos <span class="hl opt">=</span> self<span class="hl opt">.</span><span class="hl kwd">getHeadPos</span><span class="hl opt">()</span>
<span class="hl lin"> 70 </span>
<span class="hl lin"> 71 </span> <span class="hl slc"># update positions</span>
<span class="hl lin"> 72 </span> <span class="hl kwa">if</span> motdir <span class="hl opt">==</span> UP<span class="hl opt">:</span>
<span class="hl lin"> 73 </span> poslist <span class="hl opt">= [(</span>headpos<span class="hl opt">[</span><span class="hl num">0</span><span class="hl opt">]-</span><span class="hl num">1</span><span class="hl opt">,</span>headpos<span class="hl opt">[</span><span class="hl num">1</span><span class="hl opt">])]</span>
<span class="hl lin"> 74 </span> <span class="hl kwa">elif</span> motdir <span class="hl opt">==</span> DOWN<span class="hl opt">:</span>
<span class="hl lin"> 75 </span> poslist <span class="hl opt">= [(</span>headpos<span class="hl opt">[</span><span class="hl num">0</span><span class="hl opt">]+</span><span class="hl num">1</span><span class="hl opt">,</span>headpos<span class="hl opt">[</span><span class="hl num">1</span><span class="hl opt">])]</span>
<span class="hl lin"> 76 </span> <span class="hl kwa">elif</span> motdir <span class="hl opt">==</span> LEFT<span class="hl opt">:</span>
<span class="hl lin"> 77 </span> poslist <span class="hl opt">= [(</span>headpos<span class="hl opt">[</span><span class="hl num">0</span><span class="hl opt">],</span>headpos<span class="hl opt">[</span><span class="hl num">1</span><span class="hl opt">]-</span><span class="hl num">1</span><span class="hl opt">)]</span>
<span class="hl lin"> 78 </span> <span class="hl kwa">elif</span> motdir <span class="hl opt">==</span> RIGHT<span class="hl opt">:</span>
<span class="hl lin"> 79 </span> poslist <span class="hl opt">= [(</span>headpos<span class="hl opt">[</span><span class="hl num">0</span><span class="hl opt">],</span>headpos<span class="hl opt">[</span><span class="hl num">1</span><span class="hl opt">]+</span><span class="hl num">1</span><span class="hl opt">)]</span>
<span class="hl lin"> 80 </span>
<span class="hl lin"> 81 </span> poslist<span class="hl opt">.</span><span class="hl kwd">extend</span><span class="hl opt">(</span>self<span class="hl opt">.</span>poslist<span class="hl opt">[:-</span><span class="hl num">1</span><span class="hl opt">])</span>
<span class="hl lin"> 82 </span> self<span class="hl opt">.</span>poslist <span class="hl opt">=</span> poslist
<span class="hl lin"> 83 </span>
<span class="hl lin"> 84 </span> <span class="hl slc"># check if crashed</span>
<span class="hl lin"> 85 </span> <span class="hl kwa">if</span> self<span class="hl opt">.</span><span class="hl kwd">getHeadPos</span><span class="hl opt">()</span> <span class="hl kwa">in</span> self<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">()[</span><span class="hl num">1</span><span class="hl opt">:]:</span>
<span class="hl lin"> 86 </span> self<span class="hl opt">.</span>crashed <span class="hl opt">=</span> <span class="hl kwa">True</span>
<span class="hl lin"> 87 </span>
<span class="hl lin"> 88 </span> <span class="hl slc"># check if the snake has crashed</span>
<span class="hl lin"> 89 </span> <span class="hl kwa">def</span> <span class="hl kwd">chrashed</span><span class="hl opt">(</span>self<span class="hl opt">):</span>
<span class="hl lin"> 90 </span> <span class="hl kwa">return</span> self<span class="hl opt">.</span>crashed
<span class="hl lin"> 91 </span>
<span class="hl lin"> 92 </span> <span class="hl slc"># grow the snake. add a new position at the end</span>
<span class="hl lin"> 93 </span> <span class="hl kwa">def</span> <span class="hl kwd">grow</span><span class="hl opt">(</span>self<span class="hl opt">):</span>
<span class="hl lin"> 94 </span> lastpos <span class="hl opt">=</span> self<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">()[-</span><span class="hl num">1</span><span class="hl opt">]</span>
<span class="hl lin"> 95 </span> self<span class="hl opt">.</span>length <span class="hl opt">+=</span> <span class="hl num">1</span>
<span class="hl lin"> 96 </span> self<span class="hl opt">.</span>poslist<span class="hl opt">.</span><span class="hl kwd">append</span><span class="hl opt">((</span>lastpos<span class="hl opt">[</span><span class="hl num">0</span><span class="hl opt">]-</span><span class="hl num">1</span><span class="hl opt">,</span>lastpos<span class="hl opt">[</span><span class="hl num">1</span><span class="hl opt">]))</span>
<span class="hl lin"> 97 </span>
<span class="hl lin"> 98 </span> <span class="hl slc"># draw the snake</span>
<span class="hl lin"> 99 </span> <span class="hl kwa">def</span> <span class="hl kwd">draw</span><span class="hl opt">(</span>self<span class="hl opt">):</span>
<span class="hl lin">100 </span> skb <span class="hl opt">=</span> self<span class="hl opt">.</span>snakeblock
<span class="hl lin">101 </span> skbd <span class="hl opt">=</span> self<span class="hl opt">.</span>snakeblockdark
<span class="hl lin">102 </span> sf <span class="hl opt">=</span> self<span class="hl opt">.</span>surface
<span class="hl lin">103 </span>
<span class="hl lin">104 </span> <span class="hl kwa">for</span> blockpos <span class="hl kwa">in</span> self<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">():</span>
<span class="hl lin">105 </span> sf<span class="hl opt">.</span><span class="hl kwd">blit</span><span class="hl opt">(</span>skb<span class="hl opt">,(</span>blockpos<span class="hl opt">[</span><span class="hl num">1</span><span class="hl opt">]*</span>BLOCK_SIZE<span class="hl opt">,</span>blockpos<span class="hl opt">[</span><span class="hl num">0</span><span class="hl opt">]*</span>BLOCK_SIZE<span class="hl opt">))</span>
<span class="hl lin">106 </span> sf<span class="hl opt">.</span><span class="hl kwd">blit</span><span class="hl opt">(</span>skbd<span class="hl opt">,(</span>blockpos<span class="hl opt">[</span><span class="hl num">1</span><span class="hl opt">]*</span>BLOCK_SIZE<span class="hl opt">+</span><span class="hl num">5</span><span class="hl opt">,</span>blockpos<span class="hl opt">[</span><span class="hl num">0</span><span class="hl opt">]*</span>BLOCK_SIZE<span class="hl opt">+</span><span class="hl num">5</span><span class="hl opt">))</span>
<span class="hl lin">107 </span>
<span class="hl lin">108 </span> <span class="hl slc"># delete the snake</span>
<span class="hl lin">109 </span> <span class="hl kwa">def</span> <span class="hl kwd">remove</span><span class="hl opt">(</span>self<span class="hl opt">):</span>
<span class="hl lin">110 </span> bkb <span class="hl opt">=</span> self<span class="hl opt">.</span>backblock
<span class="hl lin">111 </span> sf <span class="hl opt">=</span> self<span class="hl opt">.</span>surface
<span class="hl lin">112 </span>
<span class="hl lin">113 </span> <span class="hl slc"># draw block for every snake position</span>
<span class="hl lin">114 </span> <span class="hl kwa">for</span> blockpos <span class="hl kwa">in</span> self<span class="hl opt">.</span><span class="hl kwd">getPosList</span><span class="hl opt">():</span>
<span class="hl lin">115 </span> sf<span class="hl opt">.</span><span class="hl kwd">blit</span><span class="hl opt">(</span>bkb<span class="hl opt">,(</span>blockpos<span class="hl opt">[</span><span class="hl num">1</span><span class="hl opt">]*</span>BLOCK_SIZE<span class="hl opt">,</span>blockpos<span class="hl opt">[</span><span class="hl num">0</span><span class="hl opt">]*</span>BLOCK_SIZE<span class="hl opt">))</span>
</pre>Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com17tag:blogger.com,1999:blog-8442453793803646219.post-16163963631009668792012-07-23T13:21:00.003+02:002012-07-24T00:44:38.046+02:00mazes: just for funHere is a small screencast showing some randomly generated mazes. I slightly modified the code I posted on my <a href="http://thelinuxchronicles.blogspot.com.es/2012/07/python-maze-generation-and-solution.html">previous post</a> to show the solution path step by step as it is created.<br />
<br />
<iframe width="853" height="480" src="http://www.youtube.com/embed/wPuN15pUCDs" frameborder="0" allowfullscreen></iframe>Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com2tag:blogger.com,1999:blog-8442453793803646219.post-26117787419927772142012-07-20T20:27:00.003+02:002012-07-23T20:24:03.700+02:00python: maze generation and solutionHere I'm posting a maze generation and resolve class written in python. I wanted to learn python and generating and solving mazes is a good exercise to start with.<br />
In addition to the maze class I've written another script using pygame to show the maze and its solution path in a window. <br />
This is my first try with python, there is for sure a best way of doing things. Please feel free to comment and suggest!<br />
<br />
The generation algorithm creates a "Perfect" maze. This means that, assuming I wrote it correctly, for every pair of cells chosen there is always exactly one path connecting them. No loops and no isolated regions.<br />
There are many ways for solving mazes, I choose a "Shortest Path Finder" here.<br />
<br />
<a href="http://www.astrolog.org/labyrnth/algrithm.htm">This</a> is a very good website with tons of information about maze-types, generation algorithms, solution algorithms, etc.<br />
<br />
<h3>
Maze Class: Crate, solve and display on terminal</h3>
<title>mymazeclass.py</title>
<style type="text/css">
<!--
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:10pt; font-family:'Terminus';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
//-->
</style>
<br />
<pre class="hl"><span class="hl lin"> 1 </span><span class="hl slc">#=============================================================================#</span>
<span class="hl lin"> 2 </span><span class="hl slc"># Name : mymazeclass.py #</span>
<span class="hl lin"> 3 </span><span class="hl slc"># Autor : Adrian Antonana #</span>
<span class="hl lin"> 4 </span><span class="hl slc"># Date : July, 20 2012 #</span>
<span class="hl lin"> 5 </span><span class="hl slc"># Description : Maze class. Constructor generates random perfect maze. Maze #</span>
<span class="hl lin"> 6 </span><span class="hl slc"># can also be solved. #</span>
<span class="hl lin"> 7 </span><span class="hl slc">#=============================================================================#</span>
<span class="hl lin"> 8 </span>
<span class="hl lin"> 9 </span>
<span class="hl lin"> 10 </span><span class="hl slc"># imports</span>
<span class="hl lin"> 11 </span><span class="hl kwa">import</span> random
<span class="hl lin"> 12 </span><span class="hl kwa">import</span> sys
<span class="hl lin"> 13 </span>
<span class="hl lin"> 14 </span>
<span class="hl lin"> 15 </span><span class="hl slc"># constants and help for list access</span>
<span class="hl lin"> 16 </span>BOTTOMWALL <span class="hl opt">=</span> <span class="hl num">0</span>
<span class="hl lin"> 17 </span>RIGHTWALL <span class="hl opt">=</span> <span class="hl num">1</span>
<span class="hl lin"> 18 </span>VISITED <span class="hl opt">=</span> <span class="hl num">2</span>
<span class="hl lin"> 19 </span>UP <span class="hl opt">=</span> <span class="hl num">0</span>
<span class="hl lin"> 20 </span>RIGHT <span class="hl opt">=</span> <span class="hl num">1</span>
<span class="hl lin"> 21 </span>DOWN <span class="hl opt">=</span> <span class="hl num">2</span>
<span class="hl lin"> 22 </span>LEFT <span class="hl opt">=</span> <span class="hl num">3</span>
<span class="hl lin"> 23 </span>
<span class="hl lin"> 24 </span>
<span class="hl lin"> 25 </span><span class="hl slc"># maze class definition</span>
<span class="hl lin"> 26 </span><span class="hl kwa">class</span> Maze<span class="hl opt">:</span>
<span class="hl lin"> 27 </span>
<span class="hl lin"> 28 </span> <span class="hl slc">#object constructor</span>
<span class="hl lin"> 29 </span> <span class="hl kwa">def</span> <span class="hl kwd">__init__</span><span class="hl opt">(</span>self<span class="hl opt">,</span>rows<span class="hl opt">,</span>cols<span class="hl opt">):</span>
<span class="hl lin"> 30 </span>
<span class="hl lin"> 31 </span> <span class="hl slc"># set maze size, walls and visited values</span>
<span class="hl lin"> 32 </span> self<span class="hl opt">.</span>rows <span class="hl opt">=</span> rows
<span class="hl lin"> 33 </span> self<span class="hl opt">.</span>cols <span class="hl opt">=</span> cols
<span class="hl lin"> 34 </span> self<span class="hl opt">.</span>maze <span class="hl opt">= [[[</span><span class="hl kwa">True</span><span class="hl opt">,</span><span class="hl kwa">True</span><span class="hl opt">,</span><span class="hl kwa">False</span><span class="hl opt">]</span> <span class="hl kwa">for</span> j <span class="hl kwa">in</span> <span class="hl kwb">range</span><span class="hl opt">(</span>cols<span class="hl opt">)]</span> <span class="hl kwa">for</span> i <span class="hl kwa">in</span> <span class="hl kwb">range</span><span class="hl opt">(</span>rows<span class="hl opt">)]</span>
<span class="hl lin"> 35 </span>
<span class="hl lin"> 36 </span> <span class="hl slc"># set current position, start and end point</span>
<span class="hl lin"> 37 </span> <span class="hl slc"># start and end points are used to solve the maze</span>
<span class="hl lin"> 38 </span> <span class="hl slc"># currrow is used to generate the maze</span>
<span class="hl lin"> 39 </span> self<span class="hl opt">.</span>startrow <span class="hl opt">=</span> random<span class="hl opt">.</span><span class="hl kwd">randrange</span><span class="hl opt">(</span>rows<span class="hl opt">)</span>
<span class="hl lin"> 40 </span> self<span class="hl opt">.</span>startcol <span class="hl opt">=</span> random<span class="hl opt">.</span><span class="hl kwd">randrange</span><span class="hl opt">(</span>cols<span class="hl opt">)</span>
<span class="hl lin"> 41 </span>
<span class="hl lin"> 42 </span> self<span class="hl opt">.</span>endrow <span class="hl opt">=</span> random<span class="hl opt">.</span><span class="hl kwd">randrange</span><span class="hl opt">(</span>rows<span class="hl opt">)</span>
<span class="hl lin"> 43 </span> self<span class="hl opt">.</span>endcol <span class="hl opt">=</span> random<span class="hl opt">.</span><span class="hl kwd">randrange</span><span class="hl opt">(</span>cols<span class="hl opt">)</span>
<span class="hl lin"> 44 </span>
<span class="hl lin"> 45 </span> currrow <span class="hl opt">=</span> self<span class="hl opt">.</span>startrow
<span class="hl lin"> 46 </span> currcol <span class="hl opt">=</span> self<span class="hl opt">.</span>startcol
<span class="hl lin"> 47 </span>
<span class="hl lin"> 48 </span> <span class="hl slc"># The searh can be quite deep</span>
<span class="hl lin"> 49 </span> <span class="hl kwa">if</span> rows<span class="hl opt">*</span>cols <span class="hl opt">></span> sys<span class="hl opt">.</span><span class="hl kwd">getrecursionlimit</span><span class="hl opt">():</span>
<span class="hl lin"> 50 </span> sys<span class="hl opt">.</span><span class="hl kwd">setrecursionlimit</span><span class="hl opt">(</span>rows<span class="hl opt">*</span>cols<span class="hl opt">+</span><span class="hl num">10</span><span class="hl opt">)</span>
<span class="hl lin"> 51 </span>
<span class="hl lin"> 52 </span> <span class="hl slc"># generate the maze with depth-first algorithm</span>
<span class="hl lin"> 53 </span> self<span class="hl opt">.</span><span class="hl kwd">_gen_maze</span><span class="hl opt">(</span>currrow<span class="hl opt">,</span>currcol<span class="hl opt">)</span>
<span class="hl lin"> 54 </span>
<span class="hl lin"> 55 </span> <span class="hl slc"># number matrix for solving</span>
<span class="hl lin"> 56 </span> self<span class="hl opt">.</span>numtable <span class="hl opt">= [[-</span><span class="hl num">1</span> <span class="hl kwa">for</span> j <span class="hl kwa">in</span> <span class="hl kwb">range</span><span class="hl opt">(</span>cols<span class="hl opt">)]</span><span class="hl kwa">for</span> i <span class="hl kwa">in</span> <span class="hl kwb">range</span><span class="hl opt">(</span>rows<span class="hl opt">)]</span>
<span class="hl lin"> 57 </span>
<span class="hl lin"> 58 </span> <span class="hl slc">#solution path</span>
<span class="hl lin"> 59 </span> self<span class="hl opt">.</span>solutionpath <span class="hl opt">= []</span>
<span class="hl lin"> 60 </span>
<span class="hl lin"> 61 </span> <span class="hl slc">#-----------------------------------------------------------------------------</span>
<span class="hl lin"> 62 </span>
<span class="hl lin"> 63 </span> <span class="hl slc"># returns the maze in ascii characters for printing on terminal</span>
<span class="hl lin"> 64 </span> <span class="hl kwa">def</span> <span class="hl kwd">__str__</span><span class="hl opt">(</span>self<span class="hl opt">):</span>
<span class="hl lin"> 65 </span>
<span class="hl lin"> 66 </span> <span class="hl slc"># the upper wall first</span>
<span class="hl lin"> 67 </span> outtable <span class="hl opt">=</span> <span class="hl str">'.'</span><span class="hl opt">+</span>self<span class="hl opt">.</span>cols<span class="hl opt">*</span><span class="hl str">'_.'</span><span class="hl opt">+</span><span class="hl str">'</span><span class="hl esc">\n</span><span class="hl str">'</span>
<span class="hl lin"> 68 </span>
<span class="hl lin"> 69 </span> <span class="hl kwa">for</span> i <span class="hl kwa">in</span> <span class="hl kwb">range</span><span class="hl opt">(</span>self<span class="hl opt">.</span>rows<span class="hl opt">):</span>
<span class="hl lin"> 70 </span> outtable <span class="hl opt">+=</span> <span class="hl str">'|'</span>
<span class="hl lin"> 71 </span>
<span class="hl lin"> 72 </span> <span class="hl kwa">for</span> j <span class="hl kwa">in</span> <span class="hl kwb">range</span><span class="hl opt">(</span>self<span class="hl opt">.</span>cols<span class="hl opt">):</span>
<span class="hl lin"> 73 </span> <span class="hl kwa">if</span> self<span class="hl opt">.</span>maze<span class="hl opt">[</span>i<span class="hl opt">][</span>j<span class="hl opt">][</span>BOTTOMWALL<span class="hl opt">]:</span>
<span class="hl lin"> 74 </span> outtable <span class="hl opt">+=</span> <span class="hl str">'_'</span>
<span class="hl lin"> 75 </span> <span class="hl kwa">else</span><span class="hl opt">:</span>
<span class="hl lin"> 76 </span> outtable <span class="hl opt">+=</span> <span class="hl str">' '</span>
<span class="hl lin"> 77 </span> <span class="hl kwa">if</span> self<span class="hl opt">.</span>maze<span class="hl opt">[</span>i<span class="hl opt">][</span>j<span class="hl opt">][</span>RIGHTWALL<span class="hl opt">]:</span>
<span class="hl lin"> 78 </span> outtable <span class="hl opt">+=</span> <span class="hl str">'|'</span>
<span class="hl lin"> 79 </span> <span class="hl kwa">else</span><span class="hl opt">:</span>
<span class="hl lin"> 80 </span> outtable <span class="hl opt">+=</span> <span class="hl str">'.'</span>
<span class="hl lin"> 81 </span>
<span class="hl lin"> 82 </span> outtable <span class="hl opt">+=</span> <span class="hl str">'</span><span class="hl esc">\n</span><span class="hl str">'</span>
<span class="hl lin"> 83 </span>
<span class="hl lin"> 84 </span> outtable <span class="hl opt">+=</span> <span class="hl str">'Start Point : ('</span><span class="hl opt">+</span><span class="hl kwb">str</span><span class="hl opt">(</span>self<span class="hl opt">.</span>startrow<span class="hl opt">)+</span><span class="hl str">','</span><span class="hl opt">+</span><span class="hl kwb">str</span><span class="hl opt">(</span>self<span class="hl opt">.</span>startcol<span class="hl opt">)+</span><span class="hl str">')</span><span class="hl esc">\n</span><span class="hl str">'</span>
<span class="hl lin"> 85 </span> outtable <span class="hl opt">+=</span> <span class="hl str">'End Point : ('</span><span class="hl opt">+</span><span class="hl kwb">str</span><span class="hl opt">(</span>self<span class="hl opt">.</span>endrow<span class="hl opt">)+</span><span class="hl str">','</span><span class="hl opt">+</span><span class="hl kwb">str</span><span class="hl opt">(</span>self<span class="hl opt">.</span>endcol<span class="hl opt">)+</span><span class="hl str">')</span><span class="hl esc">\n</span><span class="hl str">'</span>
<span class="hl lin"> 86 </span>
<span class="hl lin"> 87 </span> <span class="hl kwa">return</span> outtable
<span class="hl lin"> 88 </span>
<span class="hl lin"> 89 </span> <span class="hl slc">#------------------------------------------------------------------------------</span>
<span class="hl lin"> 90 </span>
<span class="hl lin"> 91 </span> <span class="hl slc"># get a list with posible directions from the current position</span>
<span class="hl lin"> 92 </span> <span class="hl kwa">def</span> <span class="hl kwd">_get_dirs</span><span class="hl opt">(</span>self<span class="hl opt">,</span>r<span class="hl opt">,</span>c<span class="hl opt">):</span>
<span class="hl lin"> 93 </span> dirlist <span class="hl opt">= []</span>
<span class="hl lin"> 94 </span>
<span class="hl lin"> 95 </span> <span class="hl slc"># check limits</span>
<span class="hl lin"> 96 </span> <span class="hl kwa">if</span> r<span class="hl opt">-</span><span class="hl num">1</span> <span class="hl opt">>=</span> <span class="hl num">0</span> <span class="hl opt">:</span> dirlist<span class="hl opt">.</span><span class="hl kwd">append</span><span class="hl opt">(</span>UP<span class="hl opt">)</span>
<span class="hl lin"> 97 </span> <span class="hl kwa">if</span> r<span class="hl opt">+</span><span class="hl num">1</span> <span class="hl opt"><=</span> self<span class="hl opt">.</span>rows<span class="hl opt">-</span><span class="hl num">1</span> <span class="hl opt">:</span> dirlist<span class="hl opt">.</span><span class="hl kwd">append</span><span class="hl opt">(</span>DOWN<span class="hl opt">)</span>
<span class="hl lin"> 98 </span> <span class="hl kwa">if</span> c<span class="hl opt">-</span><span class="hl num">1</span> <span class="hl opt">>=</span> <span class="hl num">0</span> <span class="hl opt">:</span> dirlist<span class="hl opt">.</span><span class="hl kwd">append</span><span class="hl opt">(</span>LEFT<span class="hl opt">)</span>
<span class="hl lin"> 99 </span> <span class="hl kwa">if</span> c<span class="hl opt">+</span><span class="hl num">1</span> <span class="hl opt"><=</span> self<span class="hl opt">.</span>cols<span class="hl opt">-</span><span class="hl num">1</span> <span class="hl opt">:</span> dirlist<span class="hl opt">.</span><span class="hl kwd">append</span><span class="hl opt">(</span>RIGHT<span class="hl opt">)</span>
<span class="hl lin">100 </span>
<span class="hl lin">101 </span> <span class="hl kwa">return</span> dirlist
<span class="hl lin">102 </span>
<span class="hl lin">103 </span> <span class="hl slc">#------------------------------------------------------------------------------</span>
<span class="hl lin">104 </span>
<span class="hl lin">105 </span> <span class="hl slc"># generates the maze with depth-first algorithm</span>
<span class="hl lin">106 </span> <span class="hl kwa">def</span> <span class="hl kwd">_gen_maze</span><span class="hl opt">(</span>self<span class="hl opt">,</span>r<span class="hl opt">,</span>c<span class="hl opt">,</span>d<span class="hl opt">=</span><span class="hl kwa">None</span><span class="hl opt">):</span>
<span class="hl lin">107 </span> maze <span class="hl opt">=</span> self<span class="hl opt">.</span>maze
<span class="hl lin">108 </span>
<span class="hl lin">109 </span> <span class="hl slc"># knock down the wall between actual and previous position</span>
<span class="hl lin">110 </span> maze<span class="hl opt">[</span>r<span class="hl opt">][</span>c<span class="hl opt">][</span>VISITED<span class="hl opt">] =</span> <span class="hl kwa">True</span>
<span class="hl lin">111 </span> <span class="hl kwa">if</span> d <span class="hl opt">==</span> UP <span class="hl opt">:</span> maze<span class="hl opt">[</span>r<span class="hl opt">] [</span>c<span class="hl opt">] [</span>BOTTOMWALL<span class="hl opt">] =</span> <span class="hl kwa">False</span>
<span class="hl lin">112 </span> <span class="hl kwa">elif</span> d <span class="hl opt">==</span> DOWN <span class="hl opt">:</span> maze<span class="hl opt">[</span>r<span class="hl opt">-</span><span class="hl num">1</span><span class="hl opt">][</span>c<span class="hl opt">] [</span>BOTTOMWALL<span class="hl opt">] =</span> <span class="hl kwa">False</span>
<span class="hl lin">113 </span> <span class="hl kwa">elif</span> d <span class="hl opt">==</span> RIGHT <span class="hl opt">:</span> maze<span class="hl opt">[</span>r<span class="hl opt">] [</span>c<span class="hl opt">-</span><span class="hl num">1</span><span class="hl opt">] [</span>RIGHTWALL<span class="hl opt">] =</span> <span class="hl kwa">False</span>
<span class="hl lin">114 </span> <span class="hl kwa">elif</span> d <span class="hl opt">==</span> LEFT <span class="hl opt">:</span> maze<span class="hl opt">[</span>r<span class="hl opt">] [</span>c<span class="hl opt">] [</span>RIGHTWALL<span class="hl opt">] =</span> <span class="hl kwa">False</span>
<span class="hl lin">115 </span>
<span class="hl lin">116 </span> <span class="hl slc"># get the next no visited directions to move</span>
<span class="hl lin">117 </span> dirs <span class="hl opt">=</span> self<span class="hl opt">.</span><span class="hl kwd">_get_dirs</span><span class="hl opt">(</span>r<span class="hl opt">,</span>c<span class="hl opt">)</span>
<span class="hl lin">118 </span>
<span class="hl lin">119 </span> <span class="hl slc"># random reorder directions</span>
<span class="hl lin">120 </span> <span class="hl kwa">for</span> i <span class="hl kwa">in</span> <span class="hl kwb">range</span><span class="hl opt">(</span><span class="hl kwb">len</span><span class="hl opt">(</span>dirs<span class="hl opt">)):</span>
<span class="hl lin">121 </span> j <span class="hl opt">=</span> random<span class="hl opt">.</span><span class="hl kwd">randrange</span><span class="hl opt">(</span><span class="hl kwb">len</span><span class="hl opt">(</span>dirs<span class="hl opt">))</span>
<span class="hl lin">122 </span> dirs<span class="hl opt">[</span>i<span class="hl opt">],</span>dirs<span class="hl opt">[</span>j<span class="hl opt">] =</span> dirs<span class="hl opt">[</span>j<span class="hl opt">],</span>dirs<span class="hl opt">[</span>i<span class="hl opt">]</span>
<span class="hl lin">123 </span>
<span class="hl lin">124 </span> <span class="hl slc"># make recursive call if the target cell is not visited</span>
<span class="hl lin">125 </span> <span class="hl kwa">for</span> d <span class="hl kwa">in</span> dirs<span class="hl opt">:</span>
<span class="hl lin">126 </span> <span class="hl kwa">if</span> d<span class="hl opt">==</span>UP<span class="hl opt">:</span>
<span class="hl lin">127 </span> <span class="hl kwa">if not</span> maze<span class="hl opt">[</span>r<span class="hl opt">-</span><span class="hl num">1</span><span class="hl opt">][</span>c<span class="hl opt">][</span>VISITED<span class="hl opt">]:</span>
<span class="hl lin">128 </span> self<span class="hl opt">.</span><span class="hl kwd">_gen_maze</span><span class="hl opt">(</span> r<span class="hl opt">-</span><span class="hl num">1</span><span class="hl opt">,</span>c<span class="hl opt">,</span>UP <span class="hl opt">)</span>
<span class="hl lin">129 </span> <span class="hl kwa">elif</span> d<span class="hl opt">==</span>DOWN<span class="hl opt">:</span>
<span class="hl lin">130 </span> <span class="hl kwa">if not</span> maze<span class="hl opt">[</span>r<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">][</span>c<span class="hl opt">][</span>VISITED<span class="hl opt">]:</span>
<span class="hl lin">131 </span> self<span class="hl opt">.</span><span class="hl kwd">_gen_maze</span><span class="hl opt">(</span> r<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">,</span>c<span class="hl opt">,</span>DOWN <span class="hl opt">)</span>
<span class="hl lin">132 </span> <span class="hl kwa">elif</span> d<span class="hl opt">==</span>RIGHT<span class="hl opt">:</span>
<span class="hl lin">133 </span> <span class="hl kwa">if not</span> maze<span class="hl opt">[</span>r<span class="hl opt">][</span>c<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">][</span>VISITED<span class="hl opt">]:</span>
<span class="hl lin">134 </span> self<span class="hl opt">.</span><span class="hl kwd">_gen_maze</span><span class="hl opt">(</span> r<span class="hl opt">,</span>c<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">,</span>RIGHT <span class="hl opt">)</span>
<span class="hl lin">135 </span> <span class="hl kwa">elif</span> d<span class="hl opt">==</span>LEFT<span class="hl opt">:</span>
<span class="hl lin">136 </span> <span class="hl kwa">if not</span> maze<span class="hl opt">[</span>r<span class="hl opt">][</span>c<span class="hl opt">-</span><span class="hl num">1</span><span class="hl opt">][</span>VISITED<span class="hl opt">]:</span>
<span class="hl lin">137 </span> self<span class="hl opt">.</span><span class="hl kwd">_gen_maze</span><span class="hl opt">(</span> r<span class="hl opt">,</span>c<span class="hl opt">-</span><span class="hl num">1</span><span class="hl opt">,</span>LEFT <span class="hl opt">)</span>
<span class="hl lin">138 </span>
<span class="hl lin">139 </span> <span class="hl slc">#------------------------------------------------------------------------------</span>
<span class="hl lin">140 </span>
<span class="hl lin">141 </span> <span class="hl slc"># solve the maze by filling it with numbers(algorithm name?)</span>
<span class="hl lin">142 </span> <span class="hl kwa">def</span> <span class="hl kwd">_solve_maze_aux</span><span class="hl opt">(</span>self<span class="hl opt">,</span>r<span class="hl opt">,</span>c<span class="hl opt">,</span>n<span class="hl opt">):</span>
<span class="hl lin">143 </span> maze <span class="hl opt">=</span> self<span class="hl opt">.</span>maze
<span class="hl lin">144 </span> numtable <span class="hl opt">=</span> self<span class="hl opt">.</span>numtable
<span class="hl lin">145 </span> numtable<span class="hl opt">[</span>r<span class="hl opt">][</span>c<span class="hl opt">] =</span> n
<span class="hl lin">146 </span>
<span class="hl lin">147 </span> <span class="hl slc"># check if the end has been reached</span>
<span class="hl lin">148 </span> <span class="hl kwa">if</span> <span class="hl opt">(</span>r<span class="hl opt">,</span>c<span class="hl opt">) != (</span>self<span class="hl opt">.</span>endrow<span class="hl opt">,</span>self<span class="hl opt">.</span>endcol<span class="hl opt">):</span>
<span class="hl lin">149 </span> directions <span class="hl opt">=</span> self<span class="hl opt">.</span><span class="hl kwd">_get_dirs</span><span class="hl opt">(</span>r<span class="hl opt">,</span>c<span class="hl opt">)</span>
<span class="hl lin">150 </span>
<span class="hl lin">151 </span> <span class="hl slc"># recursive calls only if there is no wall between cells and</span>
<span class="hl lin">152 </span> <span class="hl slc"># targel cell is not marked (=-1)</span>
<span class="hl lin">153 </span> <span class="hl kwa">for</span> d <span class="hl kwa">in</span> directions<span class="hl opt">:</span>
<span class="hl lin">154 </span> <span class="hl kwa">if</span> d<span class="hl opt">==</span>UP <span class="hl kwa">and not</span> maze<span class="hl opt">[</span>r<span class="hl opt">-</span><span class="hl num">1</span><span class="hl opt">][</span>c<span class="hl opt">][</span>BOTTOMWALL<span class="hl opt">]</span> <span class="hl kwa">and</span> numtable<span class="hl opt">[</span>r<span class="hl opt">-</span><span class="hl num">1</span><span class="hl opt">][</span>c<span class="hl opt">] == -</span><span class="hl num">1</span><span class="hl opt">:</span>
<span class="hl lin">155 </span> self<span class="hl opt">.</span><span class="hl kwd">_solve_maze_aux</span><span class="hl opt">(</span>r<span class="hl opt">-</span><span class="hl num">1</span><span class="hl opt">,</span>c<span class="hl opt">,</span>n<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)</span>
<span class="hl lin">156 </span> <span class="hl kwa">elif</span> d<span class="hl opt">==</span>DOWN <span class="hl kwa">and not</span> maze<span class="hl opt">[</span>r<span class="hl opt">][</span>c<span class="hl opt">][</span>BOTTOMWALL<span class="hl opt">]</span> <span class="hl kwa">and</span> numtable<span class="hl opt">[</span>r<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">][</span>c<span class="hl opt">] == -</span><span class="hl num">1</span><span class="hl opt">:</span>
<span class="hl lin">157 </span> self<span class="hl opt">.</span><span class="hl kwd">_solve_maze_aux</span><span class="hl opt">(</span>r<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">,</span>c<span class="hl opt">,</span>n<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)</span>
<span class="hl lin">158 </span> <span class="hl kwa">elif</span> d<span class="hl opt">==</span>RIGHT <span class="hl kwa">and not</span> maze<span class="hl opt">[</span>r<span class="hl opt">][</span>c<span class="hl opt">][</span>RIGHTWALL<span class="hl opt">]</span> <span class="hl kwa">and</span> numtable<span class="hl opt">[</span>r<span class="hl opt">][</span>c<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">] == -</span><span class="hl num">1</span><span class="hl opt">:</span>
<span class="hl lin">159 </span> self<span class="hl opt">.</span><span class="hl kwd">_solve_maze_aux</span><span class="hl opt">(</span>r<span class="hl opt">,</span>c<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">,</span>n<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)</span>
<span class="hl lin">160 </span> <span class="hl kwa">elif</span> d<span class="hl opt">==</span>LEFT <span class="hl kwa">and not</span> maze<span class="hl opt">[</span>r<span class="hl opt">][</span>c<span class="hl opt">-</span><span class="hl num">1</span><span class="hl opt">][</span>RIGHTWALL<span class="hl opt">]</span> <span class="hl kwa">and</span> numtable<span class="hl opt">[</span>r<span class="hl opt">][</span>c<span class="hl opt">-</span><span class="hl num">1</span><span class="hl opt">] == -</span><span class="hl num">1</span><span class="hl opt">:</span>
<span class="hl lin">161 </span> self<span class="hl opt">.</span><span class="hl kwd">_solve_maze_aux</span><span class="hl opt">(</span>r<span class="hl opt">,</span>c<span class="hl opt">-</span><span class="hl num">1</span><span class="hl opt">,</span>n<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)</span>
<span class="hl lin">162 </span>
<span class="hl lin">163 </span> <span class="hl slc">#------------------------------------------------------------------------------</span>
<span class="hl lin">164 </span>
<span class="hl lin">165 </span> <span class="hl slc"># get the solution path</span>
<span class="hl lin">166 </span> <span class="hl kwa">def</span> <span class="hl kwd">_get_solution_path</span><span class="hl opt">(</span>self<span class="hl opt">):</span>
<span class="hl lin">167 </span> actrow <span class="hl opt">=</span> self<span class="hl opt">.</span>endrow
<span class="hl lin">168 </span> actcol <span class="hl opt">=</span> self<span class="hl opt">.</span>endcol
<span class="hl lin">169 </span> startrow <span class="hl opt">=</span> self<span class="hl opt">.</span>startrow
<span class="hl lin">170 </span> startcol <span class="hl opt">=</span> self<span class="hl opt">.</span>startcol
<span class="hl lin">171 </span> path <span class="hl opt">= []</span>
<span class="hl lin">172 </span> numtable <span class="hl opt">=</span> self<span class="hl opt">.</span>numtable
<span class="hl lin">173 </span> path <span class="hl opt">=</span> self<span class="hl opt">.</span>solutionpath
<span class="hl lin">174 </span>
<span class="hl lin">175 </span> <span class="hl kwa">while</span> <span class="hl opt">(</span>actrow<span class="hl opt">,</span>actcol<span class="hl opt">) != (</span>startrow<span class="hl opt">,</span>startcol<span class="hl opt">):</span>
<span class="hl lin">176 </span> path<span class="hl opt">.</span><span class="hl kwd">append</span><span class="hl opt">((</span>actrow<span class="hl opt">,</span>actcol<span class="hl opt">))</span>
<span class="hl lin">177 </span> directions <span class="hl opt">=</span> self<span class="hl opt">.</span><span class="hl kwd">_get_dirs</span><span class="hl opt">(</span>actrow<span class="hl opt">,</span>actcol<span class="hl opt">)</span>
<span class="hl lin">178 </span> <span class="hl kwa">for</span> d <span class="hl kwa">in</span> directions<span class="hl opt">:</span>
<span class="hl lin">179 </span> <span class="hl kwa">if</span> d<span class="hl opt">==</span> UP<span class="hl opt">:</span>
<span class="hl lin">180 </span> <span class="hl kwa">if</span> numtable<span class="hl opt">[</span>actrow<span class="hl opt">][</span>actcol<span class="hl opt">]-</span><span class="hl num">1</span> <span class="hl opt">==</span> numtable<span class="hl opt">[</span>actrow<span class="hl opt">-</span><span class="hl num">1</span><span class="hl opt">][</span>actcol<span class="hl opt">]:</span>
<span class="hl lin">181 </span> actrow <span class="hl opt">-=</span><span class="hl num">1</span>
<span class="hl lin">182 </span> <span class="hl kwa">break</span>
<span class="hl lin">183 </span> <span class="hl kwa">elif</span> d<span class="hl opt">==</span> DOWN<span class="hl opt">:</span>
<span class="hl lin">184 </span> <span class="hl kwa">if</span> numtable<span class="hl opt">[</span>actrow<span class="hl opt">][</span>actcol<span class="hl opt">]-</span><span class="hl num">1</span> <span class="hl opt">==</span> numtable<span class="hl opt">[</span>actrow<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">][</span>actcol<span class="hl opt">]:</span>
<span class="hl lin">185 </span> actrow <span class="hl opt">+=</span> <span class="hl num">1</span>
<span class="hl lin">186 </span> <span class="hl kwa">break</span>
<span class="hl lin">187 </span> <span class="hl kwa">elif</span> d<span class="hl opt">==</span> LEFT<span class="hl opt">:</span>
<span class="hl lin">188 </span> <span class="hl kwa">if</span> numtable<span class="hl opt">[</span>actrow<span class="hl opt">][</span>actcol<span class="hl opt">]-</span><span class="hl num">1</span> <span class="hl opt">==</span> numtable<span class="hl opt">[</span>actrow<span class="hl opt">][</span>actcol<span class="hl opt">-</span><span class="hl num">1</span><span class="hl opt">]:</span>
<span class="hl lin">189 </span> actcol <span class="hl opt">-=</span> <span class="hl num">1</span>
<span class="hl lin">190 </span> <span class="hl kwa">break</span>
<span class="hl lin">191 </span> <span class="hl kwa">elif</span> d<span class="hl opt">==</span> RIGHT<span class="hl opt">:</span>
<span class="hl lin">192 </span> <span class="hl kwa">if</span> numtable<span class="hl opt">[</span>actrow<span class="hl opt">][</span>actcol<span class="hl opt">]-</span><span class="hl num">1</span> <span class="hl opt">==</span> numtable<span class="hl opt">[</span>actrow<span class="hl opt">][</span>actcol<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">]:</span>
<span class="hl lin">193 </span> actcol <span class="hl opt">+=</span> <span class="hl num">1</span>
<span class="hl lin">194 </span> <span class="hl kwa">break</span>
<span class="hl lin">195 </span>
<span class="hl lin">196 </span> path<span class="hl opt">.</span><span class="hl kwd">append</span><span class="hl opt">((</span>actrow<span class="hl opt">,</span>actcol<span class="hl opt">))</span>
<span class="hl lin">197 </span> path<span class="hl opt">.</span><span class="hl kwd">reverse</span><span class="hl opt">()</span>
<span class="hl lin">198 </span>
<span class="hl lin">199 </span> <span class="hl slc">#------------------------------------------------------------------------------</span>
<span class="hl lin">200 </span>
<span class="hl lin">201 </span> <span class="hl slc"># solve the maze</span>
<span class="hl lin">202 </span> <span class="hl kwa">def</span> <span class="hl kwd">solve_maze</span><span class="hl opt">(</span>self<span class="hl opt">):</span>
<span class="hl lin">203 </span> self<span class="hl opt">.</span><span class="hl kwd">_solve_maze_aux</span><span class="hl opt">(</span>self<span class="hl opt">.</span>startrow<span class="hl opt">,</span>self<span class="hl opt">.</span>startcol<span class="hl opt">,</span><span class="hl num">0</span><span class="hl opt">)</span>
<span class="hl lin">204 </span> self<span class="hl opt">.</span><span class="hl kwd">_get_solution_path</span><span class="hl opt">()</span>
</pre>
<br />
<h3>
Pygame: Show maze</h3>
It is possible to print the maze on the terminal with the above class but it doesn't look very good. So I defined a few functions using pygame to show mazes properly including the solution path.<br />
<br />
<title>mazeprogram.py</title>
<style type="text/css">
<!--
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:10pt; font-family:'Terminus';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
//-->
</style>
<br />
<pre class="hl"><span class="hl lin"> 1 </span><span class="hl slc">#!/usr/bin/python2</span>
<span class="hl lin"> 2 </span>
<span class="hl lin"> 3 </span>
<span class="hl lin"> 4 </span><span class="hl slc">#=============================================================================#</span>
<span class="hl lin"> 5 </span><span class="hl slc"># Name : mazeprogram.py #</span>
<span class="hl lin"> 6 </span><span class="hl slc"># Autor : Adrian Antonana #</span>
<span class="hl lin"> 7 </span><span class="hl slc"># Date : July, 20 2012 #</span>
<span class="hl lin"> 8 </span><span class="hl slc"># Description : Function definitions for drawing a maze in a window using #</span>
<span class="hl lin"> 9 </span><span class="hl slc"># pygame and the maze class (mymazeclass.py) I made. #</span>
<span class="hl lin"> 10 </span><span class="hl slc">#=============================================================================#</span>
<span class="hl lin"> 11 </span>
<span class="hl lin"> 12 </span><span class="hl slc"># imports</span>
<span class="hl lin"> 13 </span><span class="hl kwa">from</span> mymazeclass <span class="hl kwa">import</span> <span class="hl opt">*</span>
<span class="hl lin"> 14 </span><span class="hl kwa">from</span> sys <span class="hl kwa">import</span> <span class="hl opt">*</span>
<span class="hl lin"> 15 </span><span class="hl kwa">import</span> pygame
<span class="hl lin"> 16 </span>
<span class="hl lin"> 17 </span>
<span class="hl lin"> 18 </span><span class="hl slc">#-------------------------------------------------------------------------------------#</span>
<span class="hl lin"> 19 </span><span class="hl slc"># function definitions for drawing the maze with pygame #</span>
<span class="hl lin"> 20 </span><span class="hl slc">#-------------------------------------------------------------------------------------#</span>
<span class="hl lin"> 21 </span>
<span class="hl lin"> 22 </span><span class="hl slc"># generates a window with maze with all cells isolated from each other</span>
<span class="hl lin"> 23 </span><span class="hl kwa">def</span> <span class="hl kwd">base_window</span><span class="hl opt">(</span>m<span class="hl opt">):</span>
<span class="hl lin"> 24 </span> winwidth <span class="hl opt">=</span> m<span class="hl opt">.</span>cols<span class="hl opt">*</span>CELLSIZE<span class="hl opt">+(</span>m<span class="hl opt">.</span>cols<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)*</span>WALLSIZE
<span class="hl lin"> 25 </span> winheight <span class="hl opt">=</span> m<span class="hl opt">.</span>rows<span class="hl opt">*</span>CELLSIZE<span class="hl opt">+(</span>m<span class="hl opt">.</span>rows<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)*</span>WALLSIZE
<span class="hl lin"> 26 </span> w <span class="hl opt">=</span> pygame<span class="hl opt">.</span>display<span class="hl opt">.</span><span class="hl kwd">set_mode</span><span class="hl opt">((</span>winwidth<span class="hl opt">,</span>winheight<span class="hl opt">))</span>
<span class="hl lin"> 27 </span> w<span class="hl opt">.</span><span class="hl kwd">fill</span><span class="hl opt">(</span>BLACK<span class="hl opt">)</span>
<span class="hl lin"> 28 </span>
<span class="hl lin"> 29 </span> <span class="hl kwa">for</span> i <span class="hl kwa">in</span> <span class="hl kwb">range</span><span class="hl opt">(</span>m<span class="hl opt">.</span>rows<span class="hl opt">):</span>
<span class="hl lin"> 30 </span> <span class="hl kwa">for</span> j <span class="hl kwa">in</span> <span class="hl kwb">range</span><span class="hl opt">(</span>m<span class="hl opt">.</span>cols<span class="hl opt">):</span>
<span class="hl lin"> 31 </span> pygame<span class="hl opt">.</span>draw<span class="hl opt">.</span><span class="hl kwd">rect</span><span class="hl opt">(</span>w<span class="hl opt">,</span>WHITE<span class="hl opt">,(</span>WALLSIZE<span class="hl opt">+(</span>j<span class="hl opt">*</span>CELLSIZE<span class="hl opt">+</span>j<span class="hl opt">*</span>WALLSIZE<span class="hl opt">),</span>WALLSIZE<span class="hl opt">+(</span>i<span class="hl opt">*</span>CELLSIZE<span class="hl opt">+</span>
<span class="hl lin"> 32 </span> i<span class="hl opt">*</span>WALLSIZE<span class="hl opt">),</span>CELLSIZE<span class="hl opt">,</span>CELLSIZE<span class="hl opt">))</span>
<span class="hl lin"> 33 </span>
<span class="hl lin"> 34 </span> <span class="hl kwa">return</span> w
<span class="hl lin"> 35 </span>
<span class="hl lin"> 36 </span><span class="hl slc">#--------------------------------------------------------------------------------------</span>
<span class="hl lin"> 37 </span>
<span class="hl lin"> 38 </span><span class="hl slc"># knocks down walls from base_window to create the path</span>
<span class="hl lin"> 39 </span><span class="hl kwa">def</span> <span class="hl kwd">maze_window</span><span class="hl opt">(</span>m<span class="hl opt">):</span>
<span class="hl lin"> 40 </span> w <span class="hl opt">=</span> <span class="hl kwd">base_window</span><span class="hl opt">(</span>m<span class="hl opt">)</span>
<span class="hl lin"> 41 </span>
<span class="hl lin"> 42 </span> <span class="hl kwa">for</span> i <span class="hl kwa">in</span> <span class="hl kwb">range</span><span class="hl opt">(</span>m<span class="hl opt">.</span>rows<span class="hl opt">):</span>
<span class="hl lin"> 43 </span> <span class="hl kwa">for</span> j <span class="hl kwa">in</span> <span class="hl kwb">range</span><span class="hl opt">(</span>m<span class="hl opt">.</span>cols<span class="hl opt">):</span>
<span class="hl lin"> 44 </span> <span class="hl kwa">if not</span> m<span class="hl opt">.</span>maze<span class="hl opt">[</span>i<span class="hl opt">][</span>j<span class="hl opt">][</span>BOTTOMWALL<span class="hl opt">]:</span>
<span class="hl lin"> 45 </span> pygame<span class="hl opt">.</span>draw<span class="hl opt">.</span><span class="hl kwd">rect</span><span class="hl opt">(</span>w<span class="hl opt">,</span>WHITE<span class="hl opt">,(</span>j<span class="hl opt">*</span>CELLSIZE<span class="hl opt">+(</span>j<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)*</span>WALLSIZE<span class="hl opt">,(</span>i<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)*</span>CELLSIZE<span class="hl opt">+(</span>i<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)</span>
<span class="hl lin"> 46 </span> <span class="hl opt">*</span>WALLSIZE<span class="hl opt">,</span>CELLSIZE<span class="hl opt">,</span>WALLSIZE<span class="hl opt">))</span>
<span class="hl lin"> 47 </span> <span class="hl kwa">if not</span> m<span class="hl opt">.</span>maze<span class="hl opt">[</span>i<span class="hl opt">][</span>j<span class="hl opt">][</span>RIGHTWALL<span class="hl opt">]:</span>
<span class="hl lin"> 48 </span> pygame<span class="hl opt">.</span>draw<span class="hl opt">.</span><span class="hl kwd">rect</span><span class="hl opt">(</span>w<span class="hl opt">,</span>WHITE<span class="hl opt">,((</span>j<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)*</span>CELLSIZE<span class="hl opt">+(</span>j<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)*</span>WALLSIZE<span class="hl opt">,</span>i<span class="hl opt">*</span>CELLSIZE<span class="hl opt">+(</span>i<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)</span>
<span class="hl lin"> 49 </span> <span class="hl opt">*</span>WALLSIZE<span class="hl opt">,</span>WALLSIZE<span class="hl opt">,</span>CELLSIZE<span class="hl opt">))</span>
<span class="hl lin"> 50 </span>
<span class="hl lin"> 51 </span> pygame<span class="hl opt">.</span>display<span class="hl opt">.</span><span class="hl kwd">update</span><span class="hl opt">()</span>
<span class="hl lin"> 52 </span> <span class="hl kwa">return</span> w
<span class="hl lin"> 53 </span>
<span class="hl lin"> 54 </span><span class="hl slc">#--------------------------------------------------------------------------------------</span>
<span class="hl lin"> 55 </span>
<span class="hl lin"> 56 </span><span class="hl slc"># paints the solution path in the maze window</span>
<span class="hl lin"> 57 </span><span class="hl kwa">def</span> <span class="hl kwd">maze_path_window</span><span class="hl opt">(</span>m<span class="hl opt">,</span>w<span class="hl opt">):</span>
<span class="hl lin"> 58 </span> path <span class="hl opt">=</span> m<span class="hl opt">.</span>solutionpath
<span class="hl lin"> 59 </span>
<span class="hl lin"> 60 </span> <span class="hl slc"># print every cell within the solution path</span>
<span class="hl lin"> 61 </span> <span class="hl kwa">for</span> index <span class="hl kwa">in</span> <span class="hl kwb">range</span><span class="hl opt">(</span><span class="hl kwb">len</span><span class="hl opt">(</span>path<span class="hl opt">)-</span><span class="hl num">1</span><span class="hl opt">):</span>
<span class="hl lin"> 62 </span> actrow <span class="hl opt">=</span> path<span class="hl opt">[</span>index<span class="hl opt">][</span><span class="hl num">0</span><span class="hl opt">]</span>
<span class="hl lin"> 63 </span> actcol <span class="hl opt">=</span> path<span class="hl opt">[</span>index<span class="hl opt">][</span><span class="hl num">1</span><span class="hl opt">]</span>
<span class="hl lin"> 64 </span> nextrow <span class="hl opt">=</span> path<span class="hl opt">[</span>index<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">][</span><span class="hl num">0</span><span class="hl opt">]</span>
<span class="hl lin"> 65 </span> nextcol <span class="hl opt">=</span> path<span class="hl opt">[</span>index<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">][</span><span class="hl num">1</span><span class="hl opt">]</span>
<span class="hl lin"> 66 </span> pygame<span class="hl opt">.</span>draw<span class="hl opt">.</span><span class="hl kwd">rect</span><span class="hl opt">(</span>w<span class="hl opt">,</span>RED<span class="hl opt">,(</span>actcol<span class="hl opt">*</span>CELLSIZE<span class="hl opt">+(</span>actcol<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)*</span>WALLSIZE<span class="hl opt">,</span>actrow<span class="hl opt">*</span>CELLSIZE<span class="hl opt">+(</span>actrow<span class="hl opt">+</span>
<span class="hl lin"> 67 </span> <span class="hl num">1</span><span class="hl opt">)*</span>WALLSIZE<span class="hl opt">,</span>CELLSIZE<span class="hl opt">,</span>CELLSIZE<span class="hl opt">))</span>
<span class="hl lin"> 68 </span>
<span class="hl lin"> 69 </span> <span class="hl slc"># also paint the white spaces between the cells</span>
<span class="hl lin"> 70 </span> <span class="hl kwa">if</span> actrow <span class="hl opt">==</span> nextrow<span class="hl opt">:</span>
<span class="hl lin"> 71 </span> <span class="hl kwa">if</span> actcol <span class="hl opt"><</span> nextcol<span class="hl opt">:</span>
<span class="hl lin"> 72 </span> pygame<span class="hl opt">.</span>draw<span class="hl opt">.</span><span class="hl kwd">rect</span><span class="hl opt">(</span>w<span class="hl opt">,</span>RED<span class="hl opt">,((</span>actcol<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)*</span>CELLSIZE<span class="hl opt">+(</span>actcol<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)*</span>WALLSIZE<span class="hl opt">,</span>actrow<span class="hl opt">*</span>CELLSIZE<span class="hl opt">+</span>
<span class="hl lin"> 73 </span> <span class="hl opt">(</span>actrow<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)*</span>WALLSIZE<span class="hl opt">,</span>WALLSIZE<span class="hl opt">,</span>CELLSIZE<span class="hl opt">))</span>
<span class="hl lin"> 74 </span> <span class="hl kwa">else</span><span class="hl opt">:</span>
<span class="hl lin"> 75 </span> pygame<span class="hl opt">.</span>draw<span class="hl opt">.</span><span class="hl kwd">rect</span><span class="hl opt">(</span>w<span class="hl opt">,</span>RED<span class="hl opt">,(</span>actcol<span class="hl opt">*</span>CELLSIZE<span class="hl opt">+</span>actcol<span class="hl opt">*</span>WALLSIZE<span class="hl opt">,</span>actrow<span class="hl opt">*</span>CELLSIZE<span class="hl opt">+(</span>actrow<span class="hl opt">+</span>
<span class="hl lin"> 76 </span> <span class="hl num">1</span><span class="hl opt">)*</span>WALLSIZE<span class="hl opt">,</span>WALLSIZE<span class="hl opt">,</span>CELLSIZE<span class="hl opt">))</span>
<span class="hl lin"> 77 </span> <span class="hl kwa">elif</span> actcol <span class="hl opt">==</span> nextcol<span class="hl opt">:</span>
<span class="hl lin"> 78 </span> <span class="hl kwa">if</span> actrow <span class="hl opt"><</span> nextrow<span class="hl opt">:</span>
<span class="hl lin"> 79 </span> pygame<span class="hl opt">.</span>draw<span class="hl opt">.</span><span class="hl kwd">rect</span><span class="hl opt">(</span>w<span class="hl opt">,</span>RED<span class="hl opt">,(</span>actcol<span class="hl opt">*</span>CELLSIZE<span class="hl opt">+(</span>actcol<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)*</span>WALLSIZE<span class="hl opt">,(</span>actrow<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)*</span>CELLSIZE<span class="hl opt">+</span>
<span class="hl lin"> 80 </span> <span class="hl opt">(</span>actrow<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)*</span>WALLSIZE<span class="hl opt">,</span>CELLSIZE<span class="hl opt">,</span>WALLSIZE<span class="hl opt">))</span>
<span class="hl lin"> 81 </span> <span class="hl kwa">else</span><span class="hl opt">:</span>
<span class="hl lin"> 82 </span> pygame<span class="hl opt">.</span>draw<span class="hl opt">.</span><span class="hl kwd">rect</span><span class="hl opt">(</span>w<span class="hl opt">,</span>RED<span class="hl opt">,(</span>actcol<span class="hl opt">*</span>CELLSIZE<span class="hl opt">+(</span>actcol<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)*</span>WALLSIZE<span class="hl opt">,</span>actrow<span class="hl opt">*</span>CELLSIZE<span class="hl opt">+</span>
<span class="hl lin"> 83 </span> actrow<span class="hl opt">*</span>WALLSIZE<span class="hl opt">,</span>CELLSIZE<span class="hl opt">,</span>WALLSIZE<span class="hl opt">))</span>
<span class="hl lin"> 84 </span>
<span class="hl lin"> 85 </span>
<span class="hl lin"> 86 </span> <span class="hl slc"># add a different color for start and end cells</span>
<span class="hl lin"> 87 </span> startrow <span class="hl opt">=</span> path<span class="hl opt">[</span><span class="hl num">0</span><span class="hl opt">][</span><span class="hl num">0</span><span class="hl opt">]</span>
<span class="hl lin"> 88 </span> startcol <span class="hl opt">=</span> path<span class="hl opt">[</span><span class="hl num">0</span><span class="hl opt">][</span><span class="hl num">1</span><span class="hl opt">]</span>
<span class="hl lin"> 89 </span> endrow <span class="hl opt">=</span> path<span class="hl opt">[-</span><span class="hl num">1</span><span class="hl opt">][</span><span class="hl num">0</span><span class="hl opt">]</span>
<span class="hl lin"> 90 </span> endcol <span class="hl opt">=</span> path<span class="hl opt">[-</span><span class="hl num">1</span><span class="hl opt">][</span><span class="hl num">1</span><span class="hl opt">]</span>
<span class="hl lin"> 91 </span>
<span class="hl lin"> 92 </span> pygame<span class="hl opt">.</span>draw<span class="hl opt">.</span><span class="hl kwd">rect</span><span class="hl opt">(</span>w<span class="hl opt">,</span>BLUE<span class="hl opt">,(</span>startcol<span class="hl opt">*</span>CELLSIZE<span class="hl opt">+(</span>startcol<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)*</span>WALLSIZE<span class="hl opt">,</span>startrow<span class="hl opt">*</span>CELLSIZE<span class="hl opt">+(</span>
<span class="hl lin"> 93 </span> startrow<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)*</span>WALLSIZE<span class="hl opt">,</span>CELLSIZE<span class="hl opt">,</span>CELLSIZE<span class="hl opt">))</span>
<span class="hl lin"> 94 </span> pygame<span class="hl opt">.</span>draw<span class="hl opt">.</span><span class="hl kwd">rect</span><span class="hl opt">(</span>w<span class="hl opt">,</span>GREEN<span class="hl opt">,(</span>endcol<span class="hl opt">*</span>CELLSIZE<span class="hl opt">+(</span>endcol<span class="hl opt">+</span><span class="hl num">1</span><span class="hl opt">)*</span>WALLSIZE<span class="hl opt">,</span>endrow<span class="hl opt">*</span>CELLSIZE<span class="hl opt">+(</span>endrow<span class="hl opt">+</span>
<span class="hl lin"> 95 </span> <span class="hl num">1</span><span class="hl opt">)*</span>WALLSIZE<span class="hl opt">,</span>CELLSIZE<span class="hl opt">,</span>CELLSIZE<span class="hl opt">))</span>
<span class="hl lin"> 96 </span> pygame<span class="hl opt">.</span>display<span class="hl opt">.</span><span class="hl kwd">update</span><span class="hl opt">()</span>
<span class="hl lin"> 97 </span>
<span class="hl lin"> 98 </span>
<span class="hl lin"> 99 </span><span class="hl slc">#================================================================================#</span>
<span class="hl lin">100 </span><span class="hl slc"># Main Program #</span>
<span class="hl lin">101 </span><span class="hl slc">#================================================================================#</span>
<span class="hl lin">102 </span>
<span class="hl lin">103 </span><span class="hl slc"># sizes and colors</span>
<span class="hl lin">104 </span>CELLSIZE <span class="hl opt">=</span> <span class="hl num">5</span>
<span class="hl lin">105 </span>WALLSIZE <span class="hl opt">=</span> <span class="hl num">2</span>
<span class="hl lin">106 </span>
<span class="hl lin">107 </span>WHITE <span class="hl opt">= (</span><span class="hl num">255</span><span class="hl opt">,</span><span class="hl num">255</span><span class="hl opt">,</span><span class="hl num">255</span><span class="hl opt">)</span>
<span class="hl lin">108 </span>BLACK <span class="hl opt">= (</span><span class="hl num">0</span><span class="hl opt">,</span><span class="hl num">0</span><span class="hl opt">,</span><span class="hl num">0</span><span class="hl opt">)</span>
<span class="hl lin">109 </span>RED <span class="hl opt">= (</span><span class="hl num">255</span><span class="hl opt">,</span><span class="hl num">0</span><span class="hl opt">,</span><span class="hl num">0</span><span class="hl opt">)</span>
<span class="hl lin">110 </span>GREEN <span class="hl opt">= (</span><span class="hl num">0</span><span class="hl opt">,</span><span class="hl num">255</span><span class="hl opt">,</span><span class="hl num">0</span><span class="hl opt">)</span>
<span class="hl lin">111 </span>BLUE <span class="hl opt">= (</span><span class="hl num">0</span><span class="hl opt">,</span><span class="hl num">0</span><span class="hl opt">,</span><span class="hl num">255</span><span class="hl opt">)</span>
<span class="hl lin">112 </span>
<span class="hl lin">113 </span><span class="hl slc"># get the maze size from program arguments</span>
<span class="hl lin">114 </span>rows <span class="hl opt">=</span> <span class="hl kwb">int</span><span class="hl opt">(</span>argv<span class="hl opt">[</span><span class="hl num">1</span><span class="hl opt">])</span>
<span class="hl lin">115 </span>cols <span class="hl opt">=</span> <span class="hl kwb">int</span><span class="hl opt">(</span>argv<span class="hl opt">[</span><span class="hl num">2</span><span class="hl opt">])</span>
<span class="hl lin">116 </span>
<span class="hl lin">117 </span><span class="hl slc"># generate random maze, solve it</span>
<span class="hl lin">118 </span>maze <span class="hl opt">=</span> <span class="hl kwd">Maze</span><span class="hl opt">(</span>rows<span class="hl opt">,</span>cols<span class="hl opt">)</span>
<span class="hl lin">119 </span><span class="hl kwa">print</span> maze
<span class="hl lin">120 </span>
<span class="hl lin">121 </span>maze<span class="hl opt">.</span><span class="hl kwd">solve_maze</span><span class="hl opt">()</span>
<span class="hl lin">122 </span><span class="hl slc">#print maze</span>
<span class="hl lin">123 </span><span class="hl slc">#print maze.solutionpath</span>
<span class="hl lin">124 </span>
<span class="hl lin">125 </span><span class="hl slc"># show the maze with the solution path</span>
<span class="hl lin">126 </span>pygame<span class="hl opt">.</span><span class="hl kwd">init</span><span class="hl opt">()</span>
<span class="hl lin">127 </span>window <span class="hl opt">=</span> <span class="hl kwd">maze_window</span><span class="hl opt">(</span>maze<span class="hl opt">)</span>
<span class="hl lin">128 </span><span class="hl kwd">maze_path_window</span><span class="hl opt">(</span>maze<span class="hl opt">,</span>window<span class="hl opt">)</span>
<span class="hl lin">129 </span>
<span class="hl lin">130 </span><span class="hl kwa">while True</span><span class="hl opt">:</span>
<span class="hl lin">131 </span> <span class="hl kwa">for</span> event <span class="hl kwa">in</span> pygame<span class="hl opt">.</span>event<span class="hl opt">.</span><span class="hl kwd">get</span><span class="hl opt">():</span>
<span class="hl lin">132 </span> <span class="hl kwa">if</span> event<span class="hl opt">.</span><span class="hl kwb">type</span> <span class="hl opt">==</span> pygame<span class="hl opt">.</span>QUIT<span class="hl opt">:</span>
<span class="hl lin">133 </span> sys<span class="hl opt">.</span><span class="hl kwd">exit</span><span class="hl opt">()</span>
</pre>
<br />
<h3>
Example Screenshot</h3>
Running "mazeprogram.py" an the shell as follows:<br />
<b>$ ./mazeprogram.py 40 40</b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBYKg-OGvfORqD0HWVVw5s0h6ttW4KnXjN1Rdbmp74AlNODHIrMXS4GSx3vGgst9V-Pw7H4PThvv9xppTBwYynpDMEJzgtWmNjtHIZnYzD-URh70YKHBSABc-PsZ-NNe0nBN09WciE16FC/s1600/2012-07-20-200123_1920x1080_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="360" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBYKg-OGvfORqD0HWVVw5s0h6ttW4KnXjN1Rdbmp74AlNODHIrMXS4GSx3vGgst9V-Pw7H4PThvv9xppTBwYynpDMEJzgtWmNjtHIZnYzD-URh70YKHBSABc-PsZ-NNe0nBN09WciE16FC/s640/2012-07-20-200123_1920x1080_scrot.png" width="640" /></a></div>
<br />
<h3 style="font-weight: normal;">
<b>Generate + Solve Time</b></h3>
<div style="font-weight: normal;">
A graphic comparing maze size with generation + resolution time.<b> </b></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFmKABEit6e-JbwOo6BGOoXFk5yGIY7B0kny4bLhPjj1NPhEpamRTR_wZGUwM-bPEaz3wQPmU9LrurYTXdqUc1gvydmsse5f0h7OJcClgxq4qfAvo3ry4tjUcJTC_skm0IDQYUQGStIC5m/s1600/graphic.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="410" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFmKABEit6e-JbwOo6BGOoXFk5yGIY7B0kny4bLhPjj1NPhEpamRTR_wZGUwM-bPEaz3wQPmU9LrurYTXdqUc1gvydmsse5f0h7OJcClgxq4qfAvo3ry4tjUcJTC_skm0IDQYUQGStIC5m/s640/graphic.png" width="640" /></a></div>
<div style="font-weight: normal;">
<br /></div>
<h3 style="font-weight: normal;">
<b>Nice Vs Ugly Code</b></h3>
In my first try I wrote some code without really thinking about what data structures I needed and how exactly I wanted to solve the maze generation and solution problems. As a result the "ugly" code is extremely slow when maze size increases. Here is a comparison between both codes execution times.<br />
<b>Good Code Vs Bad Code comparison: </b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDx5YafQSumKRcL6wpHcUsXW9p4sIHxrwh3mKAYx_JDX8XVWibZ2lcqKE3KyX2dSiRxmZ3e6jFOJnh0FkfnDUENPm_LXWwO5zz3R4i_OF11oCdv53g1mivcidPyofyv_DuQ-xrXJeZ1pXk/s1600/graphic.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="409" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDx5YafQSumKRcL6wpHcUsXW9p4sIHxrwh3mKAYx_JDX8XVWibZ2lcqKE3KyX2dSiRxmZ3e6jFOJnh0FkfnDUENPm_LXWwO5zz3R4i_OF11oCdv53g1mivcidPyofyv_DuQ-xrXJeZ1pXk/s640/graphic.png" width="640" /></a></div>
Take care of your code!!!<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com8tag:blogger.com,1999:blog-8442453793803646219.post-84555518247677096512012-06-02T19:25:00.000+02:002012-06-02T19:27:12.535+02:00Sed Back References: Defining Regions<div style="text-align: justify;">
Sed has the ability to define regions in the regular expression definition area. This regions can be then back referenced using the special characters "\1" to "\9". To define a region it has to be surrounded with parentheses.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
As an example, lets say you have a nine characters long line that has to be splitted into three three-character regions:</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXRDphJTNtU3X0QmpHbTIoIf3T90CRKPXzM_R9BPLu4A_sC2EUU_dlIvAq2sN5WrbzMsM1FMXH8Jrklc-801IhveeElePmlXfHxiOcBV0LSmojQfXEEQm9ZGavNfYOwmK4t5mGpiNTkpb_/s1600/2012-06-02-190126_802x284_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXRDphJTNtU3X0QmpHbTIoIf3T90CRKPXzM_R9BPLu4A_sC2EUU_dlIvAq2sN5WrbzMsM1FMXH8Jrklc-801IhveeElePmlXfHxiOcBV0LSmojQfXEEQm9ZGavNfYOwmK4t5mGpiNTkpb_/s1600/2012-06-02-190126_802x284_scrot.png" /></a></div>
<br />
<div style="text-align: justify;">
Note I'm passing the "-r" argument to sed, otherwise I would have to escape the parenthesis characters to prevent sed to take them as literals.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Following is a "real life" example. I have a plain text file with email addresses and contact names with this format:</div>
<div style="text-align: justify;">
</div>
<blockquote class="tr_bq">
name_1:last_name_1:mail_1@domain.com<br />
name_2:last_name_2:mail_2@domain.com<br />
name_3:last_name_3:mail_3@domain.com<br />
name_4:last_name_4:mail_4@domain.com</blockquote>
<div style="text-align: justify;">
This could be difficult to read as the file grows. With just one line using sed defining regions, the output of a cat command can be formatted in a "human readable" format:</div>
<div style="text-align: justify;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIECQlAA0cvuq4CXARnshyP-XNoV9PYMCNjSh76JygzJ8UgOCxh4f1P7IVsx19bAjxKHWq6A7bMfRlnKVKNikRMp6FgHSG5zMmtwqx4cQW6D59VBBF5liKiDYz6SUmUKjjWgHJ7yy-YhV5/s1600/2012-06-02-192224_823x296_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIECQlAA0cvuq4CXARnshyP-XNoV9PYMCNjSh76JygzJ8UgOCxh4f1P7IVsx19bAjxKHWq6A7bMfRlnKVKNikRMp6FgHSG5zMmtwqx4cQW6D59VBBF5liKiDYz6SUmUKjjWgHJ7yy-YhV5/s1600/2012-06-02-192224_823x296_scrot.png" /></a></div>
<div style="text-align: justify;">
<br /></div>Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com2tag:blogger.com,1999:blog-8442453793803646219.post-562654984523121472012-06-02T17:05:00.001+02:002012-06-02T17:05:58.696+02:00Thunar Custom Actions: Resize and Rotate Images with Convert<div style="text-align: justify;">
Here I'm posting two simple bash scripts I use along with Thunar Custom Actions to resize and rotate images. Both of them work for on or more selected files.</div>
<div style="text-align: justify;">
Rotated images will be changed in place, resizing images will keep the original file and prepend "resized_to_<percent>" to the resized image, where <percent> indicates the selected reduction percent applied.</div>
<br />
resize script:<br />
<title>res.sh</title>
<style type="text/css">
<!--
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:9pt; font-family:'monospace';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
//-->
</style>
<br />
<pre class="hl"> <span class="hl num">1</span> <span class="hl slc">#!/bin/bash</span>
<span class="hl num">2</span>
<span class="hl num">3</span> PERCENT<span class="hl opt">=</span><span class="hl str">"$1"</span>
<span class="hl num">4</span> <span class="hl kwb">shift</span>
<span class="hl num">5</span> <span class="hl kwa">while</span> <span class="hl opt">((</span> <span class="hl str">"$#"</span> <span class="hl opt">));</span> <span class="hl kwa">do</span>
<span class="hl num">6</span> convert <span class="hl str">"$1"</span> <span class="hl opt">-</span>resize <span class="hl str">"$PERCENT%"</span> <span class="hl opt">-</span>quality <span class="hl num">100</span> <span class="hl str">"resized_to_${PERCENT}%_$1"</span>
<span class="hl num">7</span> <span class="hl kwb">shift</span>
<span class="hl num">8</span> <span class="hl kwa">done</span>
<span class="hl num">9</span>
<span class="hl num">10</span> <span class="hl kwb">exit</span> <span class="hl num">0</span>
</pre>
<br />
rotate script:<br />
<title>rot.sh</title>
<style type="text/css">
<!--
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:9pt; font-family:'monospace';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
//-->
</style>
<br />
<pre class="hl"> <span class="hl num">1</span> <span class="hl slc">#!/bin/bash</span>
<span class="hl num">2</span>
<span class="hl num">3</span>
<span class="hl num">4</span> DIRECTION<span class="hl opt">=</span><span class="hl str">"$1"</span>
<span class="hl num">5</span> <span class="hl kwb">shift</span>
<span class="hl num">6</span>
<span class="hl num">7</span> <span class="hl kwa">case</span> <span class="hl str">"$DIRECTION"</span> <span class="hl kwa">in</span>
<span class="hl num">8</span> <span class="hl str">"r"</span><span class="hl opt">)</span>
<span class="hl num">9</span> <span class="hl kwa">while</span> <span class="hl opt">((</span> <span class="hl str">"$#"</span> <span class="hl opt">));</span> <span class="hl kwa">do</span>
<span class="hl num">10</span> convert <span class="hl opt">-</span>rotate <span class="hl num">90</span> <span class="hl opt">-</span>quality <span class="hl num">100</span> <span class="hl str">"$1"</span> <span class="hl str">"$1"</span>
<span class="hl num">11</span> <span class="hl kwb">shift</span>
<span class="hl num">12</span> <span class="hl kwa">done</span>
<span class="hl num">13</span> <span class="hl opt">;;</span>
<span class="hl num">14</span> <span class="hl str">"l"</span><span class="hl opt">)</span>
<span class="hl num">15</span> <span class="hl kwa">while</span> <span class="hl opt">((</span> <span class="hl str">"$#"</span> <span class="hl opt">));</span> <span class="hl kwa">do</span>
<span class="hl num">16</span> convert <span class="hl opt">-</span>rotate <span class="hl opt">-</span><span class="hl num">90</span> <span class="hl opt">-</span>quality <span class="hl num">100</span> <span class="hl str">"$1"</span> <span class="hl str">"$1"</span>
<span class="hl num">17</span> <span class="hl kwb">shift</span>
<span class="hl num">18</span> <span class="hl kwa">done</span>
<span class="hl num">19</span> <span class="hl opt">;;</span>
<span class="hl num">20</span> <span class="hl kwa">esac</span>
<span class="hl num">21</span>
<span class="hl num">22</span> <span class="hl kwb">exit</span> <span class="hl num">0</span>
</pre>
<h3>
Adding Custom Actions</h3>
Take a look at <a href="http://thunar.xfce.org/pwiki/documentation/custom_actions">this thunar wiki article</a> it describes how to add custom actions to Thunar.<br />
The resize script will need the desired resizing percent as an argument.<br />
The rotate script needs the desired rotate direction (l or r), it always rotates in 90° steps.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuDeDgZXZxWe9ikb1f9Hn6TEn5u5_ME8cjpP-wY29AyVmbZpRiVvlOMUKFS9fRWYjNFBcenUL8T1jSkoI5Owf3bopOo41tYcPx-NHyK5aYWaIyUNxWgm52MeVDRPDspXmohSUvbGVZPTsk/s1600/out.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="341" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuDeDgZXZxWe9ikb1f9Hn6TEn5u5_ME8cjpP-wY29AyVmbZpRiVvlOMUKFS9fRWYjNFBcenUL8T1jSkoI5Owf3bopOo41tYcPx-NHyK5aYWaIyUNxWgm52MeVDRPDspXmohSUvbGVZPTsk/s400/out.png" width="400" /></a></div>Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com3tag:blogger.com,1999:blog-8442453793803646219.post-15181526283428250692012-06-02T16:14:00.000+02:002012-06-02T17:12:38.167+02:00Openbox Window Manager: OblogoutI'm using <a href="https://launchpad.net/oblogout">Oblogout</a> to shutdown, restart, etc. from <a href="http://openbox.org/">Openbox</a>. It looks very nice and it's easy to configure and customize.<br />
<br />
To configure it you will have to edit the "oblogout.conf" file. In <a href="http://www.archlinux.org/">Archlinux</a> this file is placed in the "/etc" directory.<br />
<br />
To shutdown and restart the system i'm using the good old "shutdown" command, "xlock" to lock the screen and "openbox --exit" to log out.<br />
<br />
<title>out.sh</title>
<style type="text/css">
<!--
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:9pt; font-family:'monospace';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
//-->
</style>
<br />
<pre class="hl"> <span class="hl num">1</span> <span class="hl opt">[</span>settings<span class="hl opt">]</span>
<span class="hl num">2</span> usehal <span class="hl opt">=</span> false
<span class="hl num">3</span>
<span class="hl num">4</span> <span class="hl opt">[</span>looks<span class="hl opt">]</span>
<span class="hl num">5</span> opacity <span class="hl opt">=</span> <span class="hl num">70</span>
<span class="hl num">6</span> bgcolor <span class="hl opt">=</span> black
<span class="hl num">7</span> buttontheme <span class="hl opt">=</span> oxygen
<span class="hl num">8</span> buttons <span class="hl opt">=</span> cancel<span class="hl opt">,</span> <span class="hl kwb">logout</span><span class="hl opt">,</span> restart<span class="hl opt">,</span> shutdown<span class="hl opt">,</span> lock
<span class="hl num">9</span>
<span class="hl num">10</span> <span class="hl opt">[</span>shortcuts<span class="hl opt">]</span>
<span class="hl num">11</span> cancel <span class="hl opt">=</span> Escape
<span class="hl num">12</span> shutdown <span class="hl opt">=</span> S
<span class="hl num">13</span> restart <span class="hl opt">=</span> R
<span class="hl num">14</span> <span class="hl kwb">logout</span> <span class="hl opt">=</span> L
<span class="hl num">15</span> lock <span class="hl opt">=</span> K
<span class="hl num">16</span>
<span class="hl num">17</span> <span class="hl opt">[</span>commands<span class="hl opt">]</span>
<span class="hl num">18</span> shutdown <span class="hl opt">=</span> sudo shutdown <span class="hl opt">-</span>h now
<span class="hl num">19</span> restart <span class="hl opt">=</span> sudo shutdown <span class="hl opt">-</span>r now
<span class="hl num">20</span> lock <span class="hl opt">=</span> xlock <span class="hl opt">&</span>
<span class="hl num">21</span> <span class="hl kwb">logout</span> <span class="hl opt">=</span> openbox <span class="hl opt">--</span><span class="hl kwb">exit</span>
</pre>
<br />
To be able to restart/shutdown without root privileges I added following lines at the end of the "/etc/sudoers" file.<br />
<b>Remember to edit the "sudoers" file using "visudo"</b><br />
<br />
<title>out.sh</title>
<style type="text/css">
<!--
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:9pt; font-family:'monospace';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
//-->
</style>
<br />
<pre class="hl"> <span class="hl num">92</span> <span class="hl opt">%</span>power ALL<span class="hl opt">= (</span>ALL<span class="hl opt">)</span> NOPASSWD<span class="hl opt">: /</span>sbin<span class="hl opt">/</span>shutdown
<span class="hl num">93</span> <span class="hl opt">%</span>power ALL<span class="hl opt">= (</span>ALL<span class="hl opt">)</span> NOPASSWD<span class="hl opt">: /</span>sbin<span class="hl opt">/</span>reboot
<span class="hl num">94</span> <span class="hl opt">%</span>power ALL<span class="hl opt">= (</span>ALL<span class="hl opt">)</span> NOPASSWD<span class="hl opt">: /</span>sbin<span class="hl opt">/</span>halt
</pre>
<br />
oblogout screenshot:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNeoMUr_jd7mw_xMDtDDwUt6nP5tTeUPcjhlcNlaT1BA_ggfHa06CgCOx6OZJDofH5d5qDw3TXHBwx3nUA-3euizkYFYMQ6A023DmZFAhGlkj0YbHdfUjrDiPrAXmeOpZU_eSvITTjUarp/s1600/out_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="360" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNeoMUr_jd7mw_xMDtDDwUt6nP5tTeUPcjhlcNlaT1BA_ggfHa06CgCOx6OZJDofH5d5qDw3TXHBwx3nUA-3euizkYFYMQ6A023DmZFAhGlkj0YbHdfUjrDiPrAXmeOpZU_eSvITTjUarp/s640/out_2.png" width="640" /></a></div>
<br />Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com1tag:blogger.com,1999:blog-8442453793803646219.post-84549908736783135282012-05-21T13:20:00.000+02:002012-06-10T20:29:50.173+02:00Sub Directories Tree Script: Sed<div style="text-align: justify;">
When working on the shell sooner or later one realizes that "sed" is a basic tool that <span class="short_text" id="result_box" lang="en"><span class="hps">although it seems to be a bit confusing at first glance, if you learn how to use it, on the long run will help you a lot.</span></span></div>
<div style="text-align: justify;">
<span class="short_text" id="result_box" lang="en"><span class="hps"><br /></span></span></div>
<div style="text-align: justify;">
<span class="short_text" id="result_box" lang="en"><span class="hps">The best way to learn how to use sed for me is by examples. So here is a script that shows all sub directories in a path with a tree-like look.</span></span></div>
<div style="text-align: justify;">
<span class="short_text" id="result_box" lang="en"><span class="hps"><br /></span></span></div>
<div style="text-align: justify;">
<span class="short_text" id="result_box" lang="en"><span class="hps">It's based on a script by Dem Pilafian I found somewhere on the Internet.</span></span></div>
<div style="text-align: justify;">
<span class="short_text" id="result_box" lang="en"><span class="hps"><br /></span></span></div>
<div style="text-align: justify;">
<span class="short_text" id="result_box" lang="en"><span class="hps">The script shows the sub directories tree on the current path if no argument is provided or </span></span><span class="short_text" id="result_box" lang="en"><span class="hps">the sub directories tree of the provided path.</span></span></div>
<div style="text-align: center;">
<span class="short_text" id="result_box" lang="en"><span class="hps"><br /></span></span></div>
<blockquote class="tr_bq">
<div style="text-align: center;">
<span class="short_text" id="result_box" lang="en"><span class="hps"><b><span style="font-family: "Courier New",Courier,monospace;">Usage: ./lstree.sh [<path><path>]</path></span></b></span></span></div>
</blockquote>
Script code:<br />
<title>out.sh</title>
<style type="text/css">
<!--
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:9pt; font-family:'monospace';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
//-->
</style>
<br />
<pre class="hl"> <span class="hl num">1</span> <span class="hl slc">#!/bin/bash</span>
<span class="hl num">2</span>
<span class="hl num">3</span> <span class="hl slc"># if path is provided then list directories in path</span>
<span class="hl num">4</span> <span class="hl kwa">if</span> <span class="hl opt">[ ! -</span>z <span class="hl str">"$1"</span> <span class="hl opt">] && [ ! -</span>d <span class="hl str">"$1"</span> <span class="hl opt">];</span> <span class="hl kwa">then</span>
<span class="hl num">5</span> <span class="hl kwb">echo</span> <span class="hl str">"$1: directory not found"</span>
<span class="hl num">6</span> <span class="hl kwb">exit</span> <span class="hl num">1</span>
<span class="hl num">7</span> <span class="hl kwa">elif</span> <span class="hl opt">[ ! -</span>z <span class="hl str">"$1"</span> <span class="hl opt">];</span> <span class="hl kwa">then</span>
<span class="hl num">8</span> <span class="hl kwb">cd</span> <span class="hl str">"$1"</span>
<span class="hl num">9</span> <span class="hl kwa">fi</span>
<span class="hl num">10</span>
<span class="hl num">11</span> <span class="hl slc"># show actions</span>
<span class="hl num">12</span> <span class="hl kwb">pwd</span>
<span class="hl num">13</span> <span class="hl kwb">echo</span> <span class="hl str">' |'</span>
<span class="hl num">14</span> <span class="hl kwc">ls</span> <span class="hl opt">-</span>R | <span class="hl kwc">grep</span> <span class="hl str">':$'</span> | <span class="hl kwc">sed</span> <span class="hl opt">-</span>r <span class="hl str">'s/:$//'</span> | <span class="hl kwc">sed</span> <span class="hl opt">-</span>r <span class="hl str">'s/[^/]*\//---/g'</span> | <span class="hl kwc">sed</span> <span class="hl opt">-</span>r <span class="hl str">'s/(-*)-/ |\1>/'</span> |
<span class="hl kwc">sed</span> <span class="hl str">'/\./d'</span>
<span class="hl num">15</span>
<span class="hl num">16</span> <span class="hl slc"># check if no folders</span>
<span class="hl num">17</span> <span class="hl kwa">if</span> <span class="hl opt">[</span> <span class="hl str">`ls -F -1 | grep "/" | wc -l`</span> <span class="hl opt">=</span> <span class="hl num">0</span> <span class="hl opt">]</span>
<span class="hl num">18</span> <span class="hl kwa">then</span> <span class="hl kwb">echo</span> <span class="hl str">"-> no sub-directories"</span>
<span class="hl num">19</span> <span class="hl kwa">fi</span>
<span class="hl num">20</span>
<span class="hl num">21</span> <span class="hl kwb">exit</span> <span class="hl num">0</span>
</pre>
Sample output:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiB_awXBAVzYEhDa3Ynh1TQJppgpY1GSsxZZxVp8CP_6reIONCaXEKcacFvVm1dkfo3GJGaRYCGBGitKN4Fe1hdQjlXitDeNvWgaqJel1PkzpO__u9pQGqEtDFWAy-lOSGyddxlqI_TtnIy/s1600/2012-05-21-123332_588x354_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiB_awXBAVzYEhDa3Ynh1TQJppgpY1GSsxZZxVp8CP_6reIONCaXEKcacFvVm1dkfo3GJGaRYCGBGitKN4Fe1hdQjlXitDeNvWgaqJel1PkzpO__u9pQGqEtDFWAy-lOSGyddxlqI_TtnIy/s1600/2012-05-21-123332_588x354_scrot.png" /></a></div>
<br />
Line 14 is the line that does all the "magic":<br />
<br />
<ul>
<li>sed #1: removes the trailing colons</li>
<li>sed #2: replaces sub directories with "---"</li>
<li>sed #3: replaces the first and the last "-" with "|" and ">" respectively</li>
<li>sed #4: removes the "." line (current directory)</li>
</ul>
<br />
<div style="text-align: justify;">
Sometimes it is hard for me to figure out what is the regular expression defined in a sed command actually matching. </div>
<div style="text-align: justify;">
Lets take the 2nd sed and pipe a fake path to it:</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRNxmglzxQ0pU31Z-x5HtRpoCkG4X1Lidksq93mQifDC9vIgJWs_H_jgnJuc0OFNnac1hlSJG_vvAata-E3yY_bu2ycowI12clduMzmtDUeORMhYUEesBYjbgMMYV5FT0TS2jCBeICoP4w/s1600/2012-05-21-130341_588x88_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRNxmglzxQ0pU31Z-x5HtRpoCkG4X1Lidksq93mQifDC9vIgJWs_H_jgnJuc0OFNnac1hlSJG_vvAata-E3yY_bu2ycowI12clduMzmtDUeORMhYUEesBYjbgMMYV5FT0TS2jCBeICoP4w/s1600/2012-05-21-130341_588x88_scrot.png" /></a></div>
<br />
<div style="text-align: justify;">
All regular expression matches have been replaced because of the "g" at the end of the sed command. Now, replacing the "g" with numbers starting from 1 to N, one can see what are the single substitutions that have been made:</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUR_kp6aubjSvLHoo56dVl_jUUJNnSVikEwQI1JFAc0MfE4mvr5r4Fd8xXy0d23SSKMrM8ITF3Z51H3WVfdX5maNSraKAUqBn-rmp2va8AuXx4jq57pwbJR9m02Pd3_VD5vHVW7FXb5ZGY/s1600/2012-05-21-130808_588x200_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUR_kp6aubjSvLHoo56dVl_jUUJNnSVikEwQI1JFAc0MfE4mvr5r4Fd8xXy0d23SSKMrM8ITF3Z51H3WVfdX5maNSraKAUqBn-rmp2va8AuXx4jq57pwbJR9m02Pd3_VD5vHVW7FXb5ZGY/s1600/2012-05-21-130808_588x200_scrot.png" /></a></div>
<br />
<br />
That's a simple way to check what the regular expression defined in the sed command is actually matching.<br />
<br />
As I said at the beginning of this post, spend some time learning sed!<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com1tag:blogger.com,1999:blog-8442453793803646219.post-15981730870686058002012-05-16T22:02:00.001+02:002012-05-16T22:15:43.582+02:00Painless Backup with Bash Script<div style="text-align: justify;">
This is a bash script I made to make my files backup process a little bit easier. The script has four basic options: </div>
<ol>
<li>Show usage</li>
<li>Create backup</li>
<li>Save the backup on my backup drive on remote machine</li>
<li>Retrieve backup from remote machine</li>
</ol>
<br />
<div style="text-align: justify;">
The script is written to backup the files and directories I want in my computer but it should be pretty easy to configure it to suit your needs.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
I don't want this post to be too long so I'm not posting here the whole text. If you want to download the whole script, <a href="http://wfurl.com/7edd36b">here</a> is the link.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Run the script with the "b" option to see a brief description of usage, options and actions.<br />
Here are just a few comments about how it works and how to modify it to make you own backups.</div>
<h3 style="text-align: justify;">
<br />Configuration</h3>
<div style="text-align: justify;">
Lines 23-52 determine what files and directories to backup, what programs to use, etc. Modify these lines to suit your needs.</div>
<title>conf.sh</title>
<style type="text/css">
<!--
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:9pt; font-family:'monospace';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
//-->
</style>
<br />
<pre class="hl"> <span class="hl num">23</span> DATE<span class="hl opt">=</span>$<span class="hl opt">(</span><span class="hl kwc">date</span> <span class="hl opt">--</span>rfc<span class="hl opt">-</span><span class="hl num">3339</span><span class="hl opt">=</span><span class="hl kwc">date</span><span class="hl opt">)</span>
<span class="hl num">24</span> BASE<span class="hl opt">=</span><span class="hl str">"$HOME"</span>
<span class="hl num">25</span> BASE_SYS<span class="hl opt">=</span><span class="hl str">"/etc"</span>
<span class="hl num">26</span> HOME_CONF_DIR<span class="hl opt">=</span><span class="hl str">".config"</span>
<span class="hl num">27</span> BACKUP_DIR<span class="hl opt">=</span><span class="hl str">"backup"</span>
<span class="hl num">28</span> SYNC_DIR<span class="hl opt">=</span><span class="hl str">"sync"</span>
<span class="hl num">29</span> DOCS_DIR<span class="hl opt">=</span><span class="hl str">"documents"</span>
<span class="hl num">30</span> DOWN_DIR<span class="hl opt">=</span><span class="hl str">"downloads"</span>
<span class="hl num">31</span> PACK_DIR<span class="hl opt">=</span><span class="hl str">"packages"</span>
<span class="hl num">32</span> PICS_DIR<span class="hl opt">=</span><span class="hl str">"pictures"</span>
<span class="hl num">33</span> SCRIPTS_DIR<span class="hl opt">=</span><span class="hl str">"scripts"</span>
<span class="hl num">34</span> VIDS_DIR<span class="hl opt">=</span><span class="hl str">"videos"</span>
<span class="hl num">35</span> LOCAL_HOST<span class="hl opt">=</span><span class="hl str">"$(uname -n)"</span>
<span class="hl num">36</span> BACKUP_HOST<span class="hl opt">=</span><span class="hl str">"linuxmachine"</span>
<span class="hl num">37</span> BACKUP_HOST_BASE_DIR<span class="hl opt">=</span><span class="hl str">"/mnt/backup"</span>
<span class="hl num">38</span> TAR_OPTS<span class="hl opt">=</span><span class="hl str">"cf"</span>
<span class="hl num">39</span> SYNC_OPTS<span class="hl opt">=</span><span class="hl str">"-avz --human-readable --progress"</span>
<span class="hl num">40</span> BZIP_OPTS<span class="hl opt">=</span><span class="hl str">"-9 -v -f"</span>
<span class="hl num">41</span> MIN_PARM_NUM<span class="hl opt">=</span><span class="hl num">1</span>
<span class="hl num">42</span> MAX_PARM_NUM<span class="hl opt">=</span><span class="hl num">2</span>
<span class="hl num">43</span> PROGS_LIST<span class="hl opt">=</span><span class="hl str">"bzip2 rsync"</span>
<span class="hl num">44</span>
<span class="hl num">45</span> <span class="hl slc"># options</span>
<span class="hl num">46</span> VALID_MODES<span class="hl opt">=(</span>b s r h<span class="hl opt">)</span>
<span class="hl num">47</span> VALID_ACTIONS<span class="hl opt">=(</span>home documents downloads packages pictures scripts videos conffileshome
conffilessys conffilesall full<span class="hl opt">)</span>
<span class="hl num">48</span>
<span class="hl num">49</span> <span class="hl slc"># What will be backed up. Files and directories</span>
<span class="hl num">50</span> CONF_DIRS_HOME<span class="hl opt">=</span>$<span class="hl opt">(</span><span class="hl kwb">echo</span> <span class="hl str">"$HOME_CONF_DIR/"</span><span class="hl opt">{</span>openbox<span class="hl opt">,</span>tint2<span class="hl opt">,</span>conky<span class="hl opt">})</span>
<span class="hl num">51</span> CONF_FILES_HOME<span class="hl opt">=</span><span class="hl str">".Xresources .xinitrc"</span>
<span class="hl num">52</span> CONF_FILES_SYS<span class="hl opt">=</span>$<span class="hl opt">(</span><span class="hl kwb">echo</span> <span class="hl str">"$BASE_SYS/"</span><span class="hl opt">{</span>mkinitcpio.conf<span class="hl opt">,</span>inittab<span class="hl opt">,</span>fstab<span class="hl opt">,</span>vimrc<span class="hl opt">,</span>rc.conf<span class="hl opt">,</span>bash.bashrc<span class="hl opt">,</span>
modprobe.d<span class="hl opt">/</span>modprobe.conf<span class="hl opt">})</span>
</pre>
<div style="text-align: justify;">
<br />
<h3>
Backing up</h3>
Making a "full" backup which includes home directories, user's configuration files and system wide configuration files:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5XiU5Yf7ElgDmSkJvqL7fCaqqxsSvqbCsTyHuTMxoIIOPqaGhHRPAIhyFHPkxAvg6yaECkbI5alzC7VqL-tDw6KaJWzj_BUZw-6XTtdl6M1_EyFzmjphWXPzxInbKWGE0s6vQKBjQVN6T/s1600/2012-05-16-212644_884x536_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="387" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5XiU5Yf7ElgDmSkJvqL7fCaqqxsSvqbCsTyHuTMxoIIOPqaGhHRPAIhyFHPkxAvg6yaECkbI5alzC7VqL-tDw6KaJWzj_BUZw-6XTtdl6M1_EyFzmjphWXPzxInbKWGE0s6vQKBjQVN6T/s640/2012-05-16-212644_884x536_scrot.png" width="640" /></a></div>
<h3>
Sync with remote host</h3>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjd4_cCmlPbG1rwFWYLPbHGQ2MSoGzXk2x10Q9pjCgBN0xXeWaQKL1643ke_lgomm66Ik7mffq7qPF3Q7nXDCHYmgCA8Fe0cVv7WpnPh1Ze4VwWJ-ZI1FylmmyvOrpqGkXjyNUeUscKF_Id/s1600/2012-05-16-212911_884x396_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="284" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjd4_cCmlPbG1rwFWYLPbHGQ2MSoGzXk2x10Q9pjCgBN0xXeWaQKL1643ke_lgomm66Ik7mffq7qPF3Q7nXDCHYmgCA8Fe0cVv7WpnPh1Ze4VwWJ-ZI1FylmmyvOrpqGkXjyNUeUscKF_Id/s640/2012-05-16-212911_884x396_scrot.png" width="640" /></a></div>
<h3>
Retrieving backup from remote host</h3>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgA43agpL7XPlQyaQZyAHYlwl8zTyh0jIkdSDBgiNohxxGi_1ygNkIxQN2v1IqfSrLhOZf8uBo7zpMOjlvoZZpnVd8oJZRvnXqAZ2w2mI2kCwevx_Rs2UuL7uaW1ceN2MLBoVEENpJKvVvY/s1600/2012-05-16-213255_884x396_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="284" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgA43agpL7XPlQyaQZyAHYlwl8zTyh0jIkdSDBgiNohxxGi_1ygNkIxQN2v1IqfSrLhOZf8uBo7zpMOjlvoZZpnVd8oJZRvnXqAZ2w2mI2kCwevx_Rs2UuL7uaW1ceN2MLBoVEENpJKvVvY/s640/2012-05-16-213255_884x396_scrot.png" width="640" /></a></div>
<h3>
Main part description</h3>
Basically the script works as follows:<br />
<ol>
<li>parameter check</li>
<li>options check</li>
<li>check programs</li>
<li>perform requested action</li>
</ol>
These steps correspond to the last lines on the script:</div>
<div style="text-align: justify;">
<br /></div>
<title>main.sh</title>
<style type="text/css">
<!--
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:9pt; font-family:'monospace';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
//-->
</style>
<br />
<pre class="hl"> <span class="hl num">254</span> <span class="hl slc">#####################################################################</span>
<span class="hl num">255</span> <span class="hl slc">########################## parameter checks #########################</span>
<span class="hl num">256</span> <span class="hl slc">#####################################################################</span>
<span class="hl num">257</span>
<span class="hl num">258</span> <span class="hl slc"># check parameters and programs</span>
<span class="hl num">259</span> check_parm_num <span class="hl kwd">$# $MIN_PARM_NUM $MAX_PARM_NUM</span>
<span class="hl num">260</span> <span class="hl opt">[[</span> $? <span class="hl opt">!=</span> <span class="hl num">0</span> <span class="hl opt">]] &&</span> usage
<span class="hl num">261</span> check_valid_parm <span class="hl kwd">$1 $2</span>
<span class="hl num">262</span> <span class="hl opt">[[</span> $? <span class="hl opt">!=</span> <span class="hl num">0</span> <span class="hl opt">]] &&</span> usage
<span class="hl num">263</span> check_programs <span class="hl str">"$PROGS_LIST"</span>
<span class="hl num">264</span>
<span class="hl num">265</span> <span class="hl slc">#####################################################################</span>
<span class="hl num">266</span> <span class="hl slc">############################# main part #############################</span>
<span class="hl num">267</span> <span class="hl slc">#####################################################################</span>
<span class="hl num">268</span>
<span class="hl num">269</span>
<span class="hl num">270</span> <span class="hl slc"># builds the list of actions to be done (only needed when backing up)</span>
<span class="hl num">271</span> <span class="hl opt">[[</span> <span class="hl str">"$1"</span> <span class="hl opt">==</span> <span class="hl str">"b"</span> <span class="hl opt">]] &&</span> build_action_list <span class="hl str">"$2"</span>
<span class="hl num">272</span>
<span class="hl num">273</span> <span class="hl slc"># perform requested actions</span>
<span class="hl num">274</span> <span class="hl kwa">case</span> <span class="hl str">"$1"</span> <span class="hl kwa">in</span>
<span class="hl num">275</span> <span class="hl str">"b"</span><span class="hl opt">)</span>
<span class="hl num">276</span> <span class="hl slc"># create backup directory for current date and localhost</span>
<span class="hl num">277</span> <span class="hl opt">[[ -</span>d <span class="hl str">"$BASE/$BACKUP_DIR/$LOCAL_HOST/$DATE"</span> <span class="hl opt">]]</span> || mkdir <span class="hl opt">-</span>p
<span class="hl str">"$BASE/$BACKUP_DIR/$LOCAL_HOST/$DATE"</span>
<span class="hl num">278</span> <span class="hl kwb">cd</span> <span class="hl str">"$BASE/$BACKUP_DIR/$LOCAL_HOST/$DATE"</span>
<span class="hl num">279</span> <span class="hl kwa">for</span> action <span class="hl kwa">in</span> <span class="hl kwd">$ACTION_LIST</span><span class="hl opt">;</span> <span class="hl kwa">do</span> backup <span class="hl str">"$action"</span><span class="hl opt">;</span> <span class="hl kwa">done</span>
<span class="hl num">280</span> <span class="hl opt">;;</span>
<span class="hl num">281</span> <span class="hl str">"s"</span><span class="hl opt">)</span>
<span class="hl num">282</span> sync <span class="hl str">"$BACKUP_HOST"</span> <span class="hl str">"$BACKUP_HOST_BASE_DIR"</span> <span class="hl str">"$LOCAL_HOST"</span>
<span class="hl num">283</span> <span class="hl opt">;;</span>
<span class="hl num">284</span> <span class="hl str">"r"</span><span class="hl opt">)</span>
<span class="hl num">285</span> retrieve <span class="hl str">"$BACKUP_HOST"</span> <span class="hl str">"$BACKUP_HOST_BASE_DIR"</span> <span class="hl str">"$BACKUP_DIR"</span> <span class="hl str">"$LOCAL_HOST"</span>
<span class="hl num">286</span> <span class="hl opt">;;</span>
<span class="hl num">287</span> <span class="hl str">"h"</span><span class="hl opt">)</span>
<span class="hl num">288</span> usage
<span class="hl num">289</span> <span class="hl opt">;;</span>
<span class="hl num">290</span> <span class="hl kwa">esac</span>
</pre>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
I tried to keep the main part as small and clear as possible by using functions. This functions are commented including parameters that they need and what they do.<br />
For instance here is the "build_action_list":<br />
<br />
<title>build_action_list.sh</title>
<style type="text/css">
<!--
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:9pt; font-family:'monospace';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
//-->
</style>
<br />
<pre class="hl"> <span class="hl num">59</span> <span class="hl slc">#builds a list of "actions" (what is going to be backed up)</span>
<span class="hl num">60</span> <span class="hl slc"># $1: action</span>
<span class="hl num">61</span> <span class="hl kwa">function</span> build_action_list <span class="hl opt">() {</span>
<span class="hl num">62</span> <span class="hl kwa">case</span> <span class="hl str">"$1"</span> <span class="hl kwa">in</span>
<span class="hl num">63</span> <span class="hl str">"full"</span><span class="hl opt">)</span>
<span class="hl num">64</span> ACTION_LIST<span class="hl opt">=</span><span class="hl str">"documents downloads packages pictures scripts videos conffileshome </span>
<span class="hl str"> conffilessys"</span>
<span class="hl num">65</span> <span class="hl opt">;;</span>
<span class="hl num">66</span> <span class="hl str">"home"</span><span class="hl opt">)</span>
<span class="hl num">67</span> ACTION_LIST<span class="hl opt">=</span><span class="hl str">"documents downloads packages pictures scripts videos"</span>
<span class="hl num">68</span> <span class="hl opt">;;</span>
<span class="hl num">69</span> <span class="hl str">"conffilesall"</span><span class="hl opt">)</span>
<span class="hl num">70</span> ACTION_LIST<span class="hl opt">=</span><span class="hl str">"conffileshome conffilessys"</span>
<span class="hl num">71</span> <span class="hl opt">;;</span>
<span class="hl num">72</span> <span class="hl opt">*)</span>
<span class="hl num">73</span> ACTION_LIST<span class="hl opt">=</span><span class="hl str">"$1"</span>
<span class="hl num">74</span> <span class="hl opt">;;</span>
<span class="hl num">75</span> <span class="hl kwa">esac</span>
<span class="hl num">76</span>
<span class="hl num">77</span> <span class="hl kwb">return</span> <span class="hl num">0</span>
<span class="hl num">78</span> <span class="hl opt">}</span>
</pre>
<br />
Any comments or questions?<br />
Hope this will make your backing up a little bit easier.</div>Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com1tag:blogger.com,1999:blog-8442453793803646219.post-79622432361201640502012-05-15T18:29:00.001+02:002012-05-15T18:58:25.214+02:00track gmail unread messages with conky in two steps<div style="text-align: justify;">
<b>Step 1: </b></div>
<div style="text-align: justify;">
Here is how you can make <a href="http://conky.sourceforge.net/">conky</a> show how many unread messages you have in your gmail accounts. In my case I wanted to track two gmail accounts so I added the following lines to my conkyrc configuration file.</div>
<blockquote class="tr_bq">
<br />
<div style="background-color: #cccccc;">
<span style="font-family: "Courier New",Courier,monospace;">GMAIL</span><span style="font-family: "Courier New",Courier,monospace;"> </span></div>
<div style="background-color: #cccccc;">
<span style="font-family: "Courier New",Courier,monospace;"><user_1>@gmail.com $alignr ${execi 30 ~/scripts/gmail/checkgmail.sh '<user_1>' '<password_1>'}</span></div>
<div style="background-color: #cccccc;">
<span style="font-family: "Courier New",Courier,monospace;"><account_2>@gmail.com $alignr ${execi 30 ~/scripts/gmail/checkgmail.sh '<user_name_2>' '<password_2>'}</span></div>
</blockquote>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The "execi 30" tells conky to run the "checkgmail.sh" script every 30 seconds.<br />
<div style="text-align: justify;">
Change <user_N> and <password_N> with your gmail username and password.</div>
<div style="text-align: justify;">
<br />
<b><span style="color: black;">Step 2:</span></b></div>
</div>
<div style="text-align: justify;">
In addition to the above mentioned lines added to the conky configuration file I slightly modified <a href="http://crunchbanglinux.org/forums/post/168025/#p168025">this script</a> I found on the <a href="http://crunchbanglinux.org/">crunchbang linux</a> forums.It seems there are some kind of problem in Germany with "gmail" not beeing allowed to use this name, so this is the modified version of the script that works for me in Germany:</div>
<title>./checkgmail.sh</title>
<style type="text/css">
<!--
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:8pt; font-family:'monospace';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
//-->
</style>
<br />
<pre class="hl"><span class="hl slc">#!/bin/bash</span>
wget <span class="hl opt">-</span>q <span class="hl opt">-</span>O <span class="hl opt">-</span> https<span class="hl opt">://</span><span class="hl kwc">mail</span>.google.com<span class="hl opt">/</span><span class="hl kwc">mail</span><span class="hl opt">/</span>feed<span class="hl opt">/</span>atom <span class="hl opt">--</span>http<span class="hl opt">-</span>user<span class="hl opt">=</span><span class="hl str">"$1"</span>@gmail.com <span class="hl opt">--</span>http<span class="hl opt">-</span>password<span class="hl opt">=</span><span class="hl str">"$2"</span> <span class="hl opt">-</span>
<span class="hl opt">-</span>no<span class="hl opt">-</span>check<span class="hl opt">-</span>certificate | <span class="hl kwc">grep</span> fullcount | <span class="hl kwc">sed</span> <span class="hl str">'s/[^0-9]//g'</span>
<span class="hl kwb">exit</span>
</pre>
<br />
It takes the gmail account name and the password as parameters of the conkyrc file. Don't forget to make the script executable.<br />
Et violĂ , the lines with gmail accounts appear at the bottom of conky showing the number of unread messages for each gmail account:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXBsivM4GMWLIoZtNrZXcEx6qiZGxY9i1wBQ1f07i_B3xpA3rR2Ujc4Vcrht74NAHwqM-GaW1VOghaCIAqM6HXg4CDWY-ekm9bfYSmMw0xAzAW32af2H6pglKsek8nLXMWsgLXGHjznVBa/s1600/2012-05-15-181454_1920x1080_scrot.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXBsivM4GMWLIoZtNrZXcEx6qiZGxY9i1wBQ1f07i_B3xpA3rR2Ujc4Vcrht74NAHwqM-GaW1VOghaCIAqM6HXg4CDWY-ekm9bfYSmMw0xAzAW32af2H6pglKsek8nLXMWsgLXGHjznVBa/s1600/2012-05-15-181454_1920x1080_scrot.jpg" /></a></div>
<br />Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com1tag:blogger.com,1999:blog-8442453793803646219.post-75896183102326681772012-05-12T21:54:00.000+02:002012-05-12T21:54:48.086+02:00Openbox Window Manager: Keyboard Shortcuts<br />
Openbox uses keyboard shurtcuts defined in the "rc.xml" file usually located in your /home/USER/.config/openbox/ directory.<b> </b>Following shortcut examples include raise/lower volume with amixer, open a terminal with terminator, take a screenshot with scrot, open thunar file manager and exit openbox.<br />
<title>out.xml</title>
<style type="text/css">
<!--
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:8pt; font-family:'monospace';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
//-->
</style>
<br />
<pre class="hl"> <span class="hl com"><!-- My keybindings --></span>
<span class="hl kwa"><keybind</span> <span class="hl kwb">key</span>=<span class="hl str">"W-t"</span><span class="hl kwa">></span>
<span class="hl kwa"><action</span> <span class="hl kwb">name</span>=<span class="hl str">"execute"</span><span class="hl kwa">></span>
<span class="hl kwa"><execute></span>terminator -m<span class="hl kwa"></execute></span>
<span class="hl kwa"></action></span>
<span class="hl kwa"></keybind></span>
<span class="hl kwa"><keybind</span> <span class="hl kwb">key</span>=<span class="hl str">"W-KP_Add"</span><span class="hl kwa">></span>
<span class="hl kwa"><action</span> <span class="hl kwb">name</span>=<span class="hl str">"execute"</span><span class="hl kwa">></span>
<span class="hl kwa"><execute></span>amixer sset Master <span class="hl num">5</span>+<span class="hl kwa"></execute></span>
<span class="hl kwa"></action></span>
<span class="hl kwa"></keybind></span>
<span class="hl kwa"><keybind</span> <span class="hl kwb">key</span>=<span class="hl str">"W-KP_Subtract"</span><span class="hl kwa">></span>
<span class="hl kwa"><action</span> <span class="hl kwb">name</span>=<span class="hl str">"execute"</span><span class="hl kwa">></span>
<span class="hl kwa"><execute></span>amixer sset Master <span class="hl num">5</span>-<span class="hl kwa"></execute></span>
<span class="hl kwa"></action></span>
<span class="hl kwa"></keybind></span>
<span class="hl kwa"><keybind</span> <span class="hl kwb">key</span>=<span class="hl str">"C-A-BackSpace"</span><span class="hl kwa">></span>
<span class="hl kwa"><action</span> <span class="hl kwb">name</span>=<span class="hl str">"execute"</span><span class="hl kwa">></span>
<span class="hl kwa"><execute></span>openbox --exit<span class="hl kwa"></execute></span>
<span class="hl kwa"></action></span>
<span class="hl kwa"></keybind></span>
<span class="hl kwa"><keybind</span> <span class="hl kwb">key</span>=<span class="hl str">"W-f"</span><span class="hl kwa">></span>
<span class="hl kwa"><action</span> <span class="hl kwb">name</span>=<span class="hl str">"execute"</span><span class="hl kwa">></span>
<span class="hl kwa"><execute></span>thunar<span class="hl kwa"></execute></span>
<span class="hl kwa"></action></span>
<span class="hl kwa"></keybind></span>
<span class="hl kwa"><keybind</span> <span class="hl kwb">key</span>=<span class="hl str">"Print"</span><span class="hl kwa">></span>
<span class="hl kwa"><action</span> <span class="hl kwb">name</span>=<span class="hl str">"execute"</span><span class="hl kwa">></span>
<span class="hl kwa"><execute></span>/usr/bin/scrot<span class="hl kwa"></execute></span>
<span class="hl kwa"></action></span>
<span class="hl kwa"></keybind></span>
</pre>
<br />
After modifying the "rc.xml" file remember to run "openbox --reconfigure" on the shell to make changes take effect.Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com2tag:blogger.com,1999:blog-8442453793803646219.post-5202403580745125052012-05-06T20:27:00.000+02:002012-05-06T20:27:11.081+02:00sed basics<div style="text-align: justify;">
If you often have to modify some configuration files like fstab, menu.lst, bashrc, etc by removing lines, change some device or comment/uncomment one or more lines, "sed" will be the simplest and fastest way to do so without needing to open the files with an editor.</div>
<div style="text-align: justify;">
But first you will have to know a few thing about the "sed" command.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
One of the most common uses o f "sed" is to replace one string by another, maybe a device name or the path for something.</div>
<div style="text-align: justify;">
<br />
From the sed man page:</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<span style="font-family: "Courier New",Courier,monospace;">s/regexp/replacement/</span><br />
<span style="font-family: "Courier New",Courier,monospace;"> Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement.</span><span style="font-family: "Courier New",Courier,monospace;"></span></div>
<div style="text-align: justify;">
</div>
<div style="text-align: justify;">
<br />
<br />
Here is an example changing /dev/sda with /dev/sdb in /etc/fstab:</div>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMBbHYSQRKpw1IvRmf6oXqlTvopL2Z6lcPk_qxmTNYPCxIyUbK_Dr9G8HU84uGaajczeVTK4KhjvY_VUjYh0JzZPPJH4rjple_S5Jv0DKVjsc2-0AYmajURw4iUrKYvFnjl54m-ErWvfu9/s1600/2012-05-06-183307_756x452_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMBbHYSQRKpw1IvRmf6oXqlTvopL2Z6lcPk_qxmTNYPCxIyUbK_Dr9G8HU84uGaajczeVTK4KhjvY_VUjYh0JzZPPJH4rjple_S5Jv0DKVjsc2-0AYmajURw4iUrKYvFnjl54m-ErWvfu9/s1600/2012-05-06-183307_756x452_scrot.png" /></a></div>
<br />
Now lets comment out my samba share line:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiD7oJDrrsQ78fjSAFljo3WfOZLdkpNBe1v3o5n50QyLgodSRj3YlzT6w1niEsdhqYpwTeCp7FX6TvNGcI3JRUwpOYkYBbpJbkgDI3qDmxK9YXdgB7B6xnBT7SSUGF281iiQ-turKvQcyj0/s1600/2012-05-06-184855_756x452_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiD7oJDrrsQ78fjSAFljo3WfOZLdkpNBe1v3o5n50QyLgodSRj3YlzT6w1niEsdhqYpwTeCp7FX6TvNGcI3JRUwpOYkYBbpJbkgDI3qDmxK9YXdgB7B6xnBT7SSUGF281iiQ-turKvQcyj0/s1600/2012-05-06-184855_756x452_scrot.png" /></a></div>
Note you will have to escape the "/" characters with a leading "\".<br />
<br />
Deleting empty lines is just as easy as follows:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5iS6Rm0LN_eCmTIBMjyodGTkn4yH34IzzPnyc97qlqWFB-_PPCV8rAgLSXgEG3-5XcJkbAh1opTmLJVyauUETW-MvLpn-ntjcn_GhyphenhyphenPb7rhwfbRN3gUWtT-BFk4m2ZLpGBq3LKe3CxU-9/s1600/2012-05-06-190104_756x452_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5iS6Rm0LN_eCmTIBMjyodGTkn4yH34IzzPnyc97qlqWFB-_PPCV8rAgLSXgEG3-5XcJkbAh1opTmLJVyauUETW-MvLpn-ntjcn_GhyphenhyphenPb7rhwfbRN3gUWtT-BFk4m2ZLpGBq3LKe3CxU-9/s1600/2012-05-06-190104_756x452_scrot.png" /></a></div>
<br />
Note the "^" character stands for "Line start" and "$" for "End of line" so "^$" means "blank line". Those special character are oftern used in regular expression matching. Take a look at my <a href="http://thelinuxchronicles.blogspot.de/2012/05/road-to-lpic-grep-with-regular.html">grep post</a> to see more examples of using regular expessions.<br />
<br />
You can also make sed to work on a specific line or range. I created a file with binary numbers 0-16 to work on it.<br />
I will use "nl" to show line numbers from now on to make it easier to see what lines where affected by sed.<br />
<br />
Deleting line 8 (0111):<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdqnowZWgYE7Nk3vVPmSfWP1oYH3tRWEPsK4D64iMSc99j4zXP12w1SWn44Tlo0WnX5osF9S-6pakhoWMYSZvofcGpHsocTf8uLKuBdcYYxQ4wD-nqYk4sMU6F6-xBKkdld7jPI1oEELbz/s1600/2012-05-06-191858_748x564_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdqnowZWgYE7Nk3vVPmSfWP1oYH3tRWEPsK4D64iMSc99j4zXP12w1SWn44Tlo0WnX5osF9S-6pakhoWMYSZvofcGpHsocTf8uLKuBdcYYxQ4wD-nqYk4sMU6F6-xBKkdld7jPI1oEELbz/s1600/2012-05-06-191858_748x564_scrot.png" /></a></div>
<br />
<br />
Deleting range 4-13:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbN5rKsSho87wVKpTjWuw9f6laZ2ljmg7vdHGvyYJTkdark8boO350Mj_hZcP2deOprPfqVieB7tx0YCO05WulDJpzsac1mKVScswVntssfFJXo75VhfXIppp_y8FV_qtxQqK3YWlxj7s-/s1600/2012-05-06-192102_748x424_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbN5rKsSho87wVKpTjWuw9f6laZ2ljmg7vdHGvyYJTkdark8boO350Mj_hZcP2deOprPfqVieB7tx0YCO05WulDJpzsac1mKVScswVntssfFJXo75VhfXIppp_y8FV_qtxQqK3YWlxj7s-/s1600/2012-05-06-192102_748x424_scrot.png" /></a></div>
<br />
If there is a string that repeats many times and you want to change just one specific apparition on a specific line:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMQuZLG1gihS_DsH6HZzfq9sHWSW0L21vpVwoG0-Xh4kJc-rECH9xq6B9-SueWtDWXQQRIBVBHUNHDHxLur_UpSZI6nBl7BPFYUqaIi1AxCVd5MV9p0jMmF6u2i4sN7ISQ4x-MVcOqN1qe/s1600/2012-05-06-200808_556x368_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMQuZLG1gihS_DsH6HZzfq9sHWSW0L21vpVwoG0-Xh4kJc-rECH9xq6B9-SueWtDWXQQRIBVBHUNHDHxLur_UpSZI6nBl7BPFYUqaIi1AxCVd5MV9p0jMmF6u2i4sN7ISQ4x-MVcOqN1qe/s1600/2012-05-06-200808_556x368_scrot.png" /></a></div>
<br />
Lets change "FF" whith "--"on range 2-3 3rd apparition and range 5-9 6th apparition:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbPRW8AykjNT19gfJDuUNz85zoDrh_wgypMJLULAES4AwnS6WIWIHk84KkNcd2StKg2X-m5L88WF4dzAtpDgl8SE9IwiqhvndkpB575nXbp5eZ3iTCjz0rocWXa0OvOOOIu1J55GNCIoKH/s1600/2012-05-06-201312_564x368_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbPRW8AykjNT19gfJDuUNz85zoDrh_wgypMJLULAES4AwnS6WIWIHk84KkNcd2StKg2X-m5L88WF4dzAtpDgl8SE9IwiqhvndkpB575nXbp5eZ3iTCjz0rocWXa0OvOOOIu1J55GNCIoKH/s1600/2012-05-06-201312_564x368_scrot.png" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
Note you can concatenate sed commands with "-e".<br />
<br />
<br />
Sed is a very powerfull tool and can do much more complex tasks than the ones exposed in this post. It will take some time to learn his usage but on the long run using sed will speed up many repetitive tasks on the command line.Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com1tag:blogger.com,1999:blog-8442453793803646219.post-25493334132978517132012-05-06T20:26:00.000+02:002012-05-06T20:26:21.110+02:00Playing Radio Stream with Bash ScriptSmall bash script I wrote to play radio streams trough mplayer. It shows a simple radio list where one can choose the radio stream to be played.<br />
I wrote this a long time ago and it has still some thing written in spanish but it is cery simple and you will find it pretty straight forward.<br />
<br />
<br />
<title>radio.sh</title>
<style type="text/css">
<!--
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:8pt; font-family:'monospace';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
//-->
</style>
<br />
<pre class="hl"><span class="hl slc">#!/bin/bash</span>
<span class="hl slc">#Emisoras</span>
SER<span class="hl opt">=</span><span class="hl str">"http://194.169.201.177:8085/liveser.mp3"</span>
AWESOME80<span class="hl opt">=</span><span class="hl str">"http://uplink.duplexfx.com:8000"</span>
LITE80<span class="hl opt">=</span><span class="hl str">"http://uplink.duplexfx.com:8040"</span>
HAIRBAND<span class="hl opt">=</span><span class="hl str">"http://uplink.duplexfx.com:8014"</span>
THEHEART<span class="hl opt">=</span><span class="hl str">"http://uplink.duplexfx.com:8006"</span>
BEATLES<span class="hl opt">=</span><span class="hl str">"http://uplink.duplexfx.com:8062"</span>
GOODTIMEOLDIES<span class="hl opt">=</span><span class="hl str">"http://uplink.duplexfx.com:8046"</span>
CLASSICALGUITAR<span class="hl opt">=</span><span class="hl str">"http://uplink.duplexfx.com:8020"</span>
<span class="hl slc">#Comprobar mplayer instalado</span>
PLAYER<span class="hl opt">=</span>$<span class="hl opt">(</span><span class="hl kwc">which</span> mplayer <span class="hl num">2</span><span class="hl opt">> /</span>dev<span class="hl opt">/</span>null<span class="hl opt">)</span>
<span class="hl kwa">if</span> <span class="hl opt">[ -</span>z <span class="hl str">"$PLAYER"</span> <span class="hl opt">];</span> <span class="hl kwa">then</span>
<span class="hl kwb">printf</span> <span class="hl str">"Instala mplayer!</span><span class="hl esc">\n</span><span class="hl str">"</span>
<span class="hl kwb">exit</span> <span class="hl num">1</span>
<span class="hl kwa">fi</span>
DIALOG<span class="hl opt">=</span>$<span class="hl opt">(</span><span class="hl kwc">which</span> dialog <span class="hl num">2</span><span class="hl opt">> /</span>dev<span class="hl opt">/</span>null<span class="hl opt">)</span>
<span class="hl kwa">if</span> <span class="hl opt">[ -</span>z <span class="hl str">"$DIALOG"</span> <span class="hl opt">];</span> <span class="hl kwa">then</span>
<span class="hl kwb">printf</span> <span class="hl str">"Instala dialog!</span><span class="hl esc">\n</span><span class="hl str">"</span>
<span class="hl kwb">exit</span> <span class="hl num">1</span>
<span class="hl kwa">fi</span>
<span class="hl slc">#Terminar con mplayer</span>
killall <span class="hl num">2</span><span class="hl opt">> /</span>dev<span class="hl opt">/</span>null <span class="hl str">"$PLAYER"</span>
<span class="hl slc">#Seleccionar emisora</span>
EMISORA<span class="hl opt">=</span>$<span class="hl opt">(</span><span class="hl kwd">$DIALOG</span> <span class="hl opt">--</span>stdout <span class="hl opt">--</span>title <span class="hl str">"Radio"</span> <span class="hl opt">--</span>radiolist <span class="hl str">"Emisoras"</span> <span class="hl num">0 0 0</span> <span class="hl str">"Cadena SER"</span> <span class="hl str">""</span> ON <span class="hl str">"Awesome </span>
<span class="hl str">80's"</span> <span class="hl str">""</span> OFF <span class="hl str">"Lite 80's"</span> <span class="hl str">""</span> OFF <span class="hl str">"80's Hairband"</span> <span class="hl str">""</span> OFF <span class="hl str">"The Heart"</span> <span class="hl str">""</span> OFF <span class="hl str">"Beatles"</span> <span class="hl str">""</span> OFF <span class="hl str">"Good </span>
<span class="hl str">Time Oldies"</span> <span class="hl str">""</span> OFF <span class="hl str">"Classical Guitar"</span> <span class="hl str">""</span> OFF<span class="hl opt">)</span>
<span class="hl kwc">clear</span>
<span class="hl kwa">case</span> <span class="hl kwd">$EMISORA</span> <span class="hl kwa">in</span>
<span class="hl str">"Cadena SER"</span><span class="hl opt">)</span>
<span class="hl kwb">printf</span> <span class="hl str">"Playing Cadena SER</span><span class="hl esc">\n</span><span class="hl str">"</span>
<span class="hl str">"$PLAYER"</span> <span class="hl num">2</span><span class="hl opt">>/</span>dev<span class="hl opt">/</span>null <span class="hl str">"$SER"</span> <span class="hl opt">;;</span>
<span class="hl str">"Awesome 80's"</span><span class="hl opt">)</span>
<span class="hl kwb">printf</span> <span class="hl str">"Playing Awesone 80's</span><span class="hl esc">\n</span><span class="hl str">"</span>
<span class="hl str">"$PLAYER"</span> <span class="hl num">2</span><span class="hl opt">>/</span>dev<span class="hl opt">/</span>null <span class="hl str">"$AWESOME80"</span> <span class="hl opt">;;</span>
<span class="hl str">"Lite 80's"</span><span class="hl opt">)</span>
<span class="hl kwb">printf</span> <span class="hl str">"Playing Lite 80's</span><span class="hl esc">\n</span><span class="hl str">"</span>
<span class="hl str">"$PLAYER"</span> <span class="hl num">2</span><span class="hl opt">>/</span>dev<span class="hl opt">/</span>null <span class="hl str">"$LITE80"</span> <span class="hl opt">;;</span>
<span class="hl str">"80's Hairband"</span><span class="hl opt">)</span>
<span class="hl kwb">printf</span> <span class="hl str">"Playing 80's Hairband</span><span class="hl esc">\n</span><span class="hl str">"</span>
<span class="hl str">"$PLAYER"</span> <span class="hl num">2</span><span class="hl opt">>/</span>dev<span class="hl opt">/</span>null <span class="hl str">"$HAIRBAND"</span> <span class="hl opt">;;</span>
<span class="hl str">"The Heart"</span><span class="hl opt">)</span>
<span class="hl kwb">printf</span> <span class="hl str">"Playing The Heart</span><span class="hl esc">\n</span><span class="hl str">"</span>
<span class="hl str">"$PLAYER"</span> <span class="hl num">2</span><span class="hl opt">>/</span>dev<span class="hl opt">/</span>null <span class="hl str">"$THEHEART"</span> <span class="hl opt">;;</span>
<span class="hl str">"Beatles"</span><span class="hl opt">)</span>
<span class="hl kwb">printf</span> <span class="hl str">"Playing Beatles</span><span class="hl esc">\n</span><span class="hl str">"</span>
<span class="hl str">"$PLAYER"</span> <span class="hl num">2</span><span class="hl opt">>/</span>dev<span class="hl opt">/</span>null <span class="hl str">"$BEATLES"</span> <span class="hl opt">;;</span>
<span class="hl str">"Good Time Oldies"</span><span class="hl opt">)</span>
<span class="hl kwb">printf</span> <span class="hl str">"Playing Good Time Oldies</span><span class="hl esc">\n</span><span class="hl str">"</span>
<span class="hl str">"$PLAYER"</span> <span class="hl num">2</span><span class="hl opt">>/</span>dev<span class="hl opt">/</span>null <span class="hl str">"$GOODTIMEOLDIES"</span> <span class="hl opt">;;</span>
<span class="hl str">"Classical Guitar"</span><span class="hl opt">)</span>
<span class="hl kwb">printf</span> <span class="hl str">"Playing Classical Guitar</span><span class="hl esc">\n</span><span class="hl str">"</span>
<span class="hl str">"$PLAYER"</span> <span class="hl num">2</span><span class="hl opt">>/</span>dev<span class="hl opt">/</span>null <span class="hl str">"$CLASSICALGUITAR"</span> <span class="hl opt">;;</span>
<span class="hl opt">*)</span>
<span class="hl kwb">printf</span> <span class="hl str">"No se ha encontrado la emisora.</span><span class="hl esc">\n</span><span class="hl str">"</span> <span class="hl opt">;;</span>
<span class="hl kwa">esac</span>
<span class="hl kwb">exit</span> <span class="hl num">0</span>
</pre>
Running the script:
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVByJuOCgV2NZzmedfb2oCX4in_H20v_WNOpl_UQWzck-FP05ztdgNJWfuTmwOB_onz7Z9uqSCCDfNWDtoTB8sI88SbNlIjI0UnXQy_CH-5yw4YrphaVjPifhNThPA9LwXSwfgIFa1oZMW/s1600/2012-05-05-091137_1920x1080_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="360" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVByJuOCgV2NZzmedfb2oCX4in_H20v_WNOpl_UQWzck-FP05ztdgNJWfuTmwOB_onz7Z9uqSCCDfNWDtoTB8sI88SbNlIjI0UnXQy_CH-5yw4YrphaVjPifhNThPA9LwXSwfgIFa1oZMW/s640/2012-05-05-091137_1920x1080_scrot.png" width="640" /></a></div>Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com5tag:blogger.com,1999:blog-8442453793803646219.post-53265941853065482122012-05-05T08:41:00.000+02:002012-05-06T20:26:47.389+02:00grep with regular expressions<div style="text-align: justify;">
</div>
<div style="text-align: justify;">
If you are famliar with the "grep" command for searhching for line containig a particular character sequence in a file or filtering a command output like "ls" or "ps" you may found out that sometimes just passing to grep a plain string is insufficient for you.</div>
<div style="text-align: justify;">
There is a more advanced way for searching patterns by using regular expresions.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Here is a simple script i used to learn the basics abaout how regular expressions are used along with grep: </div>
<br />
<title>regexp/regex.sh</title>
<style type="text/css">
<!--
body.hl { background-color:#2a2b2f; }
pre.hl { color:#e1e0e5; background-color:#2a2b2f; font-size:8pt; font-family:'monospace';}
.hl.num { color:#d1c79e; }
.hl.esc { color:#9ab2c8; }
.hl.str { color:#d1c79e; }
.hl.pps { color:#d1c79e; }
.hl.slc { color:#67686b; }
.hl.com { color:#67686b; }
.hl.ppc { color:#77cf63; }
.hl.opt { color:#e1e0e5; }
.hl.lin { color:#ffff99; }
.hl.kwa { color:#62acce; font-weight:bold; }
.hl.kwb { color:#e6ac32; }
.hl.kwc { color:#cf8563; }
.hl.kwd { color:#bb63cf; }
//-->
</style>
<br />
<pre class="hl"><span class="hl slc">#!/bin/bash</span>
<span class="hl kwb">echo</span> <span class="hl opt">-</span>e <span class="hl opt">{</span><span class="hl num">0</span><span class="hl opt">,</span><span class="hl num">1</span><span class="hl opt">,</span><span class="hl num">2</span><span class="hl opt">,</span><span class="hl num">3</span><span class="hl opt">,</span><span class="hl num">4</span><span class="hl opt">,</span><span class="hl num">5</span><span class="hl opt">,</span><span class="hl num">6</span><span class="hl opt">,</span><span class="hl num">7</span><span class="hl opt">,</span><span class="hl num">8</span><span class="hl opt">,</span><span class="hl num">9</span><span class="hl opt">,</span>A<span class="hl opt">,</span>B<span class="hl opt">,</span>C<span class="hl opt">,</span>D<span class="hl opt">,</span>E<span class="hl opt">,</span>F<span class="hl opt">}{</span><span class="hl num">0</span><span class="hl opt">,</span><span class="hl num">1</span><span class="hl opt">,</span><span class="hl num">2</span><span class="hl opt">,</span><span class="hl num">3</span><span class="hl opt">,</span><span class="hl num">4</span><span class="hl opt">,</span><span class="hl num">5</span><span class="hl opt">,</span><span class="hl num">6</span><span class="hl opt">,</span><span class="hl num">7</span><span class="hl opt">,</span><span class="hl num">8</span><span class="hl opt">,</span><span class="hl num">9</span><span class="hl opt">,</span>A<span class="hl opt">,</span>B<span class="hl opt">,</span>C<span class="hl opt">,</span>D<span class="hl opt">,</span>E<span class="hl opt">,</span>F<span class="hl opt">}{</span><span class="hl num">0</span><span class="hl opt">,</span><span class="hl num">1</span><span class="hl opt">,</span><span class="hl num">2</span><span class="hl opt">,</span><span class="hl num">3</span><span class="hl opt">,</span><span class="hl num">4</span><span class="hl opt">,</span><span class="hl num">5</span><span class="hl opt">,</span><span class="hl num">6</span><span class="hl opt">,</span><span class="hl num">7</span><span class="hl opt">,</span><span class="hl num">8</span><span class="hl opt">,</span><span class="hl num">9</span><span class="hl opt">,</span>A<span class="hl opt">,</span>B<span class="hl opt">,</span>C<span class="hl opt">,</span>
D<span class="hl opt">,</span>E<span class="hl opt">,</span>F<span class="hl opt">}{</span><span class="hl num">0</span><span class="hl opt">,</span><span class="hl num">1</span><span class="hl opt">,</span><span class="hl num">2</span><span class="hl opt">,</span><span class="hl num">3</span><span class="hl opt">,</span><span class="hl num">4</span><span class="hl opt">,</span><span class="hl num">5</span><span class="hl opt">,</span><span class="hl num">6</span><span class="hl opt">,</span><span class="hl num">7</span><span class="hl opt">,</span><span class="hl num">8</span><span class="hl opt">,</span><span class="hl num">9</span><span class="hl opt">,</span>A<span class="hl opt">,</span>B<span class="hl opt">,</span>C<span class="hl opt">,</span>D<span class="hl opt">,</span>E<span class="hl opt">,</span>F<span class="hl opt">}</span><span class="hl str">"</span><span class="hl esc">\n</span><span class="hl str">"</span> | <span class="hl kwc">sed</span> <span class="hl opt">/</span>^$<span class="hl opt">/</span>d | <span class="hl kwc">sed</span> s<span class="hl opt">/</span>^\ <span class="hl opt">// ></span> hex
<span class="hl slc"># lines containig AA somewhere</span>
<span class="hl kwb">echo</span> <span class="hl str">"lines containig AAA somewhere"</span>
<span class="hl kwb">echo</span> <span class="hl str">"beginning with no 1,3,5,7,9,C,D or E and ending with no 0-5"</span>
<span class="hl kwc">grep</span> <span class="hl opt">--</span>color<span class="hl opt">=</span>auto <span class="hl opt">-</span>n <span class="hl opt">-</span>E <span class="hl str">'^[^13579CDE]?AAA[^012345]?$'</span> hex
<span class="hl slc"># lines starting with AAA and contain no digit</span>
<span class="hl kwb">echo</span> <span class="hl opt">-</span>e <span class="hl str">"</span><span class="hl esc">\n</span><span class="hl str">lines starting with AAA and contain no digit at the end"</span>
<span class="hl kwc">grep</span> <span class="hl opt">--</span>color<span class="hl opt">=</span>auto <span class="hl opt">-</span>n <span class="hl opt">-</span>E <span class="hl str">'^AAA[^[:digit:]]'</span> hex
<span class="hl slc"># lines starting with 3-5 and ending with BBB</span>
<span class="hl kwb">echo</span> <span class="hl opt">-</span>e <span class="hl str">"</span><span class="hl esc">\n</span><span class="hl str">lines ending with BBB"</span>
<span class="hl kwc">grep</span> <span class="hl opt">--</span>color<span class="hl opt">=</span>auto <span class="hl opt">-</span>n <span class="hl opt">-</span>E <span class="hl str">'[3-5]BBB$'</span> hex
<span class="hl slc"># lines beginning with no less than two 1 and no more than three 1</span>
<span class="hl slc"># followed by at least 1 D or more</span>
<span class="hl kwb">echo</span> <span class="hl opt">-</span>e <span class="hl str">"</span><span class="hl esc">\n</span><span class="hl str">lines beginning with no less than two 1 and no more than three 1</span><span class="hl esc">\n</span><span class="hl str">followed by at least 1 </span>
<span class="hl str">D or more"</span>
<span class="hl kwc">grep</span> <span class="hl opt">--</span>color<span class="hl opt">=</span>auto <span class="hl opt">-</span>n <span class="hl opt">-</span>E <span class="hl str">'^1{2,3}D+'</span> hex
<span class="hl slc"># lines starting either with 3BB or 3CF ending with 6 to 9</span>
<span class="hl kwb">echo</span> <span class="hl opt">-</span>e <span class="hl str">"</span><span class="hl esc">\n</span><span class="hl str">lines starting either with 3BB or 3CF"</span>
<span class="hl kwc">grep</span> <span class="hl opt">--</span>color<span class="hl opt">=</span>auto <span class="hl opt">-</span>n <span class="hl opt">-</span>E <span class="hl str">'^3(BB|CF)[6-9]'</span> hex
<span class="hl slc"># lines with 4 or 6 only</span>
<span class="hl kwb">echo</span> <span class="hl opt">-</span>e <span class="hl str">"</span><span class="hl esc">\n</span><span class="hl str">lines with 4 or 6 only"</span>
<span class="hl kwc">grep</span> <span class="hl opt">--</span>color<span class="hl opt">=</span>auto <span class="hl opt">-</span>n <span class="hl opt">-</span>E <span class="hl str">'[4]{4}|[6]{4}'</span> hex
<span class="hl kwb">exit</span> <span class="hl num">0</span> </pre>
<br />
The script creates a file with one 16-bit hexadecimal number per line from 0000 to FFFF and performs several grep commands on it.<br />
Options i used for grep were "-n" for showing the line numbers (green on screenshot) and "-E" to use extended regular expressions.<br />
Grep with "-E" option should behave the same way as "egrep" does.<br />
<br />
Running the script throws following output to the terminal: <br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj8UhG2iRIKyEPW2W9VwtX5pVcY5VCkcElfgxqtSc-EkXKt7n1SPZFnGz-oy74wbNvkaPANB5GdD3lcaOsX7ON3oCpQ331gsfK9Mau-ruD877gvFQT2V7DC6ErOo_I5UlkaR26ulyiCKpQ9/s1600/2012-05-04-232417_1920x1080_scrot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="360" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj8UhG2iRIKyEPW2W9VwtX5pVcY5VCkcElfgxqtSc-EkXKt7n1SPZFnGz-oy74wbNvkaPANB5GdD3lcaOsX7ON3oCpQ331gsfK9Mau-ruD877gvFQT2V7DC6ErOo_I5UlkaR26ulyiCKpQ9/s640/2012-05-04-232417_1920x1080_scrot.png" width="640" /></a></div>
<br />
There are a few more lines that didn't fit on the screenshot.Anonymoushttp://www.blogger.com/profile/14395323624758834367noreply@blogger.com0