Hints and tips

These are some helpful hints on various topics.

 

1 2 3 4 5 6

Dash in regular explession

There is something you should know if you want to use a dash in ereg() matching. For example if you want to check if a string contains only letters, digits, dots and dashes, you should put the dash at the end of the list. Also unlike the dot don't put a backslash before it:
ereg('^[a-zA-Z0-9\.-]+$', $string)

Copy Volume serial number of a disk

When you make a copy of a disk using Copy Disk you get identical clone, except for a small detail: Volume serial number.

Volume serial number

There are some anti-copy protections based on this. Here is how to change this number under Linux:

Using mdir tool from package mtools you can see the serial number of the original disk, for example 173c-10db
Put the copied disk into the flopy device and save its volume serial number to the file  floppy.vsn using this command:
    dd if=/dev/floppy bs=1 count=4 skip=39  > floppy.vsn
Edit this file with some hex editor for example hexedit, and put the original number in it. You should be careful - bytes are copied backwards. So if you want to write number 173c-10db, you should type db 10 3c 17 into this file.
Save the number back to the copy:
    dd of=/dev/floppy bs=1 count=4 skip=39  < floppy.vsn

Search and replace in bash scripting

To get the value of variable X with all occurances of 'yy' replaced by 'zz' use the following expression:

${X//yy/zz}

For example:

ORIG="foo to the bar"
REPLACED=${ORIG//bar/baz}
echo $REPLACED

The result is:
foo to the baz

Argument list too long, or how to use xargs

Suppose you want to remove all jpeg files from several directories. You use
rm -f `find . -name *.jpg -type f`
and get "Argument list too long". What you have to do is to use xargs to automatically split the long list to several shorter and pass them to rm:
find . -name *.jpg -type f | xargs rm -f
This way several rm commands are executed, but your files are finally gone.

Starting additional X servers

To start additional X server use this command:
 startx -- :1
where :1 should be unique for every new server, meaning the second one will be :2 and so on. To switch between servers use Ctrl + Alt + F7, F8 and so on.



Creating files with fixed length and random content

Handy command to create file with fixed length and random content is  dd. Some examples:

File test1.bin, 10k size, containing only zero bytes:
 dd bs=1024 count=10 if=/dev/zero of=test1.bin

File test2.bin, 100k size and random content:
 dd bs=1024 count=100 if=/dev/random of=test2.bin

Above example is rather slow, especially for large files, so here's another way to generate pseudo-random file:
 dd bs=1024 count=100 if=/dev/hda of=test2.bin skip=1000
This will copy 100k from the hard drive 1М after the beginning.

Getting the last word of bash string

To get the last word of a bash string use awk '{print $NF}':

echo "one two three" | awk '{print $NF}'

returns

three

1 2 3 4 5 6