Main menu:

Site search

Categories

May 2013
M T W T F S S
« Oct    
 12345
6789101112
13141516171819
20212223242526
2728293031  

Tags

Blogroll

Removing white margin around figures generated in Matlab

A very good post on how to do this can be found at this blog.

Share

How to fix: Bash History not working between sessions

Hi,

I run into a problem the other day, with my Bash history not working between terminal sessions. If i had one session open and typed some commands, then those commands wouldn’t appear in another session, or even after i closed the current terminal session.

It seems that the problem was due to wrong permissions in the /home/user/.bashrc_history file. The owner of the file was the system root and not my user account. To check if this is true just do the following listing:

[bash]

la -ls /home/<user>/.bashrc_history

[/bash]

If you get the following it seems that the root is the owner of the history file, therefore anything that you write on the terminal cannot be appended to that file.

[bash]
$ ls -la /home/<user>/.bash_history
-rw——- 1 root root 210 2010-11-23 16:32 /home/<user>/.bash_history
[/bash]

To fix this just change user and group ownership for the history file as follows:

[bash]
sudo chown <user>:<user> /home/<user>/.bash_history
[/bash]

Share

Iterating a HashMap in Java

The following code snippet shows how to iterate a HashMap in Java:

[java]

//Instantiate a new hashmap with integer objects as key and value respectively:

Map<Integer,Integer> map = new HashMap<Integer,Integer>();

// Obtain the entry set, since it avoids the HashMap.get(key) lookup that would otherwise be called if we were accessing the value of a HashMap entry, using a key that was retrieved from a keySet iterator

Set<Entry<Integer, Integer>> entrySet = map.entrySet();
Iterator<Entry<Integer, Activity>> iterator = activitySet.iterator();

while ( iterator.hasNext() ){
// Get the key.
Integer key = iterator.next().getKey();
// Get the value.
Integer value = iterator.next().getValue();
// Print them on the standard output.
System.out.println( "Key: " + key + " Value: " + value );
}

[/java]

Share

Generating Positive Random Integers in Java

If often get the question how can we obtain positive, random numbers in Java. The following is a small Java code snippet on how to achieve this:

[java]
// Instantiate a new random number generator.

Random rand = new Random();

// This will pick a random integer in the range of  0  – 2147483647 (2^31-1) inclusive the 0. The upper limit number will not be picked.

int posRandInt = rand.nextInt( Integer.MAX_VALUE );

// To return a random integer starting from 1, simply add 1 to the result of the nextInt() method.

posRandInt = rand.nextInt( Integer.MAX_VALUE ) + 1;
[/java]

Share

Remove all files in subdirectories that have a specific extension

The following command uses the power of the Linux find application in order to scan all sub-directories in a given directory and delete all the files that match a specific extension.

[bash]
find . -type f -name "*.txt~" -exec rm -f {} \;
[/bash]

Share