Hints and tips

These are some helpful hints on various topics.

 

1 2 3 4 5 6

Oracle dual table or how to migrate Mysql tableless queries

If you are familiar with Mysql, you probably use sometimes tableless queries like this:
 
SELECT 12 * (1 + 10);
 
Well, if you're migrating to Oracle you'll be surprised to find that those queries are not allowed here. The solution? Use the dual table like this:
 
SELECT 12 * (1 + 10) from dual
 
Dual is special table that is automatically created. Its schema has only one column called dummy, and is populated with just one row with value 'X'. In short - this makes it very useful for our case.

Locating where class is loaded from


When debugging Java application which contains of multiple multiple jars there are a number of cases where one gets lost in all the jars. Then is vital to know where a third party class is loaded from to know its exact version. To get the load location of StrangeClass, just evaluate the following expression in your runtime:

URL location = StrangeClass.class.getProtectionDomain().getCodeSource().getLocation();

Iterating filenames with spaces in bash


Usually when you write some bash script and need to iterate through some files, you use constructions like those:

for file in /dir/*.c
do
    echo $file
done


or

for file in `find /dir -name *.c`
do
    echo $file
done


Both approaches fail when it comes to files with spaces, like "file with space.c". In this case you iterate over all three parts "file", "with" and "space.c" instead the file itself. To cope with this use:

find /dir -name *.c | while read file
do
    echo $file
done

/dev/null in Windows


Analogue for the /dev/null file on Windows is NUL. So the command
 
./my_prog 2>/dev/null
translates to
 
my_prog.exe 2>NUL

Perl inline replacement on Windows


Replacing some string in multiple files is one pretty feature of Perl. Just execute
 
perl -p -i -e 's/SEARCH/REPLACE/g' file.txt
and all SEARCH in file.txt transforms to REPLACE. On Windows there's a catch: this does not work with single quotes. So to get it working use
 
perl -p -i -e "s/SEARCH/REPLACE/g" file.txt


Add easily "sleep" functionality to your Linux box with installing zero new packages

If you want to be able to turn of your computer after a number of minutes you have a plenty of programs to choose from. Here's an easy way to do this with just using bash and at. Add the following to your .bashrc:
 
sleeper() {
    echo 'init 0' | sudo at now + $1 minutes
}

 
The just use it - set you desktop to poweroff in 50 minutes:
 
> sleeper 50

Use pm-suspend or pm-hibernate instead of init 0 to suspend or hibernate.


Getting full directory path in bash and cmd prompt

To get the absolute path for the current or any other realtively addressed folder, for example the parent, use the following constructs:

bash

parent_dir=`cd .. && pwd`

cmd


set current_dir=%CD%
cd ..
set parent_dir=%CD%
cd %current_dir%


1 2 3 4 5 6