Sunday, April 25, 2010

bash: send a command to a screen session

"Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells." It is a very useful tool, that I often use, as it allows me to run "multiple terminals in one" even when I log out from the system.

So how to send command to screen session from other terminal. In other words how to remote-control a screen session from within a script. This post is useful for this.

In particular, I wanted to execute matlab scripts in screen windows. To do this I used the following:
1. Create screen sessionscreen -S matlab2. Create few windows in the screen session using Ctr-a c
3. Send matlab command to each as followsfor ((i=0;i<$NO_WINDOWS;i+=1)); do
echo $i
sleep 2
screen -S matlab -p $i -X exec matlab -nodesktop -r "bashCalculations('part',$i+1)"
done
where bashCalculations is my matlab script bashCalculations.m that takes part argument. So,, each screen window executes different part of my script.

Saturday, April 24, 2010

Ubuntu 10.04: How to remove chat and mail icons (indicators) from system tray

The 10.04 LTS release of Ubuntu seems to be very, very good. However, for some strange reason, creators of this release made some strange decisions about Ubuntu's look-and-feel. One of the things that annoyed me was the assumption that every Ubuntu user, uses Twitter, Facebook or other social network. Thus, they incorporated the tools for using social networking sites into the distibution. They did it by adding chat and status icons/notifiers into Gnome system try. However, what they did not do, was the ability to easily remove them. For example, when you remove massage notifier (mail icon) using "Remove From Panel" option, the volume control icon also disappears.
Thus you need to remove these icons some other way, which in this case, is to remove two packages: indicator-me and indicator-messages:sudo apt-get remove indicator-me indicator-messagesAfter that just log-out and log-into the gnome session and the icons should be gone:

EDIT
It also should work for Ubuntu 11.04 (Natty Narwhal).

Monday, March 08, 2010

Matlab: Convert numerical array to cell with string representation of numbers

Short Matlab script that takes an array (i.e. vector) of numbers and returns cell with fileds that are string representation of the numbers in the array.For example: >> a=[1 2 3.24 -43 4.56];
>> numarray2cellstring(a)

ans =

'1' '2' '3.24' '-43' '4.56'

Wednesday, March 03, 2010

imagemagick: crop all images

Example:convert rose: -crop 40x30+10+10 +repage repage.gif

Friday, February 26, 2010

Change font and font size in Fluxbox

In Fluxbox 1.1 a font and a font size can be changed by editing a overlay file, usually located in ~/.fluxbox/. So just add the following lines to this file and restart fluxboxmenu.title.font: sans-12:bold
menu.frame.font: sans-12:bold
toolbar.clock.font: sans-10:bold
toolbar.workspace.font: sans-12:bold
*font: sans-12
Of course you have to select the font you like or the size.

Thursday, February 25, 2010

bash: split string to array

There are few possible ways of splitting a string with delimiter-separated values to an array in Bash. Here I present five of them.

1. Using tr command

Output:> [123]
> [456]
> [567]
> [5]
> [343]
This method will produce incorrect array due to space in "567 5", but it works fine if there are no spaces in the $STR variable.

2. Using IFS (Internal Field Separator) variable

Output:> [123]
> [456]
> [567 5]
> [343]
This method works fine even if there are spaces.

3. Using read command

Output:> [123]
> [456]
> [567 5]
> [343]
This method also works fine even though there are spaces in the $STR variable.

4. Using sed command

Output:> [123]
> [456]
> [567]
> [5]
> [343]
This method will also produce incorrect array due to space in "567 5", but it works fine if there are no spaces in the $STR variable.

5. Using set command

Output:> [123]
> [456]
> [567 5]
> [343]
This method also works fine.

References

This post was mainly inspired by this StackOverflow question and my need to perform string 2 array conversions in bash.

Wednesday, February 24, 2010

bash: read a specific line or/and a column from a file

If we have column-row wise txt files, we can use awk and sed programs to read the rows and columns of such files.

Read specific line

To read a specific line from a txt file, the following commands can be usedawk 'NR==12' file.txt sed -n '12p' file.txt In both cases, line 12 will be returned.

Read specific column

awk '{print $1, $2}' file The code will return first and second columns from file.txt

Read specific column and line

Additionally, using awk we can get a specific column(s) in of a given line as followsawk 'NR==12 {print $1, $2}' file.txtThe Code will return first and second columns form a line no. 12.

Sunday, February 21, 2010

xampp: Compiling mod_bw.c and getting the error about undefined symbol: apr_atomic_cas

When I was compiling mod_bw.c 0.7 (apxs -i -a -c mod_bw.c) for xampp 1.7.1 on Ubuntu 9.10 and restarting lampp I was getting the following error:httpd: Syntax error on line 129 of /opt/lampp/etc/httpd.conf: Cannot load /opt/lampp/modules/mod_bw.so into server: /opt/lampp/modules/mod_bw.so: undefined symbol: apr_atomic_casThe solution was to edit the mod_bw.c and comment out or remove the following lines:#if (APR_MAJOR_VERSION < 1) #define apr_atomic_inc32 apr_atomic_inc #define apr_atomic_dec32 apr_atomic_dec #define apr_atomic_add32 apr_atomic_add #define apr_atomic_cas32 apr_atomic_cas #define apr_atomic_set32 apr_atomic_set #endifand to recompile the file.

Unfortunately, there was no difference in the file upload speed.

Saturday, February 20, 2010

ImageMagick: add text to many images

Lets assume that we have 800x600 pixels images and we want to add an authors name to the lower right corner. We can do this using the following command (the command overwrites the original files!)
mogrify -fill white -pointsize 16 -annotate +685+590 'Authors Name' *.JPG

Thursday, February 18, 2010

ImageMagick: split one image to smaller images

Lets assume that we have a 1024x1024 pixel image called test.tif and that we want to split this image into 64x64 non-overlapping images called test001.tif, test002.tif, test003.tiff,....
We can do this using this command from
convert -crop 64x64 +repage test.tif test%02d.tif