A bit about

Hello, everyone! All you can see below is just my bank of information. Some material I've found in the fathomless net, some I've learned myself. Don't think all of the information here is right or actual, but may be it could be of use for you :) All feedback is welcome, especially constructive ones :)

Sunday, November 22, 2009

MS SQL: Drop all views

When updating database it's sometimes hard to track all minor changes in lots of tables, views and other database elements. In case of tables, imho, we don't have lots of options and we are to track all changes carefully (not to damage data). On the other hand views, functions and stored procedures don't contain any data and we have more freedom.

That's why I used to drop and create all views when updating them. It's not too slow and scripts are not very large. Also, MS SQL Server can generate one script for creation of all views automatically and as result we have just a complete snapshot of them.

The problem is that before applying script on another server, we have to delete all previous views. There is no a built-in procedure to do this and I found a script and modified it a little bit:
declare @name varchar(100)
declare @sqlstring nvarchar(1000)

declare SPViews_cursor cursor for
SELECT sysobjects.name
FROM sysobjects
where OBJECTPROPERTY(sysobjects.id, N'IsView') = 1

open SPViews_cursor

fetch next from SPViews_cursor into @name

while @@fetch_status = 0
begin
set @sqlstring = 'drop view ' + @name
exec sp_executesql @sqlstring
set @sqlstring = ' '
fetch next from SPViews_cursor into @name
end

close SPViews_cursor
deallocate SPViews_cursor

Thursday, October 22, 2009

QT: Window in the center of screen

The problem is how to position Qt windows in the center of screen. Found the solution here. It's a Russian site, but I hope the code is visible. There are just two similar codes for positioning. I liked the second one :) It's shorter and clearer.
QRect frect = frameGeometry();
frect.moveCenter(QDesktopWidget().availableGeometry().center());
move(frect.topLeft())

I assume that this code is placed inside a constructor of the window which is positioned.

Linux: start remote detached screen

Screen is very useful tool in case when it's necessary to start remote command without wait for its completeness. But by default it doesn't allow to detach screen without an actual terminal:
$ ssh Server "screen -m -d echo test"
Must be connected to a terminal.

The solution is to use ssh's -t flag:
$ ssh -t Server "screen -m -d echo test"

Tuesday, October 20, 2009

Linux: ps output fomat

It took for me some time to find the solution, because "ps --help" doesn't print the full information especially about output format. I often need to obtain some process ID from script, based on command name and its arguments.

The first solution is to parse "ps -aux". But it's not convenient because pid is not the first column. I found command in the man page:
$ ps -eo pid,comm,args
It allows one to define certain columns. Possible keys are:
pcpu; group; ppid; user; args; comm; rgroup; nice; pid; pgid; etime; ruser; time; tty; vsz

Thursday, October 15, 2009

MS SQL: Drop view if it exists

Microsoft recommends the following way of dropping database view without an error message if the view doesn't exist (http://msdn.microsoft.com/en-us/library/aa258835%28SQL.80%29.aspx):
IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_NAME = 'View_myview')
DROP VIEW dbo.View_myview
GO

Thursday, August 27, 2009

Broadcom Wi-Fi adapter on Linux

Some laptops have integrated Wi-Fi adapter which Linux doesn't recognize by default. Probably because the drivers are proprietary. Ubuntu suggests automatic installation of the driver but does it optionally. Lots of other distributives don't do this at all.

I'll describe the process of driver installation for my Wi-Fi adapter in PCLinux. As I could understand on forums, this type of adapter is widely spread and installation process is almost the same for all distributives.

First of all, let's check that system has found our adapter:
$ lspci | grep Wireless
05:02.0 Network controller: Broadcom Corporation BCM4318 [AirForce One 54g] 802.11g Wireless LAN Controller (rev 02)

Now lets install fwcutter:
$ sudo apt-get install bcm43xx-fwcutter
Then download driver from here, extract the archive and install the firmware:
$ tar -xjf broadcom-wl-4.150.10.5.tar.bz2
$ cd broadcom-wl-4.150.10.5/driver
$ sudo b43-fwcutter -w /lib/firmware wl_apsta_mimo.o

After all these steps Wi-Fi should start working. At worst, you can try to reboot your laptop.

Wednesday, August 26, 2009

BASH: autocompletion after the first <TAB>

Lots of modern Linux distributives prefer this variant of autocompletion. You type a letter, push 'Tab' and receive the list of options. But I don't like this. I like when options appear after the second push of 'Tab'.

It is controlled by show-all-if-ambiguous parameter of inputrc. For double-Tab style (which I prefer) the command will look like:
set show-all-if-ambiguous off
This should be placed into /etc/inputrc (if you have privileges and want system wide setting) or in your local ~/.inputrc. But in the second case you should also add
unset INPUTRC;
to you ~/.bashrc

Tuesday, August 25, 2009

Aliases in VIM

I usually have a problem with VIM commands ":w" and ":q". I type them very quickly and don't have time to release Shift key. As result I get error messages which inform me that commands ":W" and ":Q" don't exist.

That's why I decided to create an alias for them. They are created by the following command:
command <new_alias> <source_command>
For example, my ~/.vimrc looks like this:
set nobackup
command W w
command Q q

So I obtain correct result regardless state of Shift key.

Monday, August 10, 2009

Firefox: redirection to "jar:file//"

Recently my favorite browser Firefox get used to redirect me to "jar:file//..." with local address of some files. "Backward" button didn't work in this case and it really annoyed me.

The reason of this problem was additional plug-in called Ask.com. When I uninstalled it, the useless redirection disappeared.

Tuesday, August 4, 2009

Windows: List of network shares

In order to see the list of all current network shares on the local computer and where they point to, imho, the following command is the most useful:
>net share

Share name Resource Remark

-------------------------------------------------
ADMIN$ C:\Windows Remote Admin
C$ C:\ Default share
D$ D:\ Default share
IPC$ Remote IPC

Friday, July 31, 2009

Debian: "Waiting for root file system..."

After installation of Debian RC4 on my server I noticed that sometimes in hangs on boot with message like "Waiting for root file system...". I didn't know how to deal with this problem and as long as it appeared not often I didn't pay attention to this.

But then I decided to upgrade my kernel to the latest version (2.6.30.3 at that moment) and the new version hanged each time. I spend some time in Google and finally found the solution here http://www.debianhelp.org/node/11653. Big thanks to this guy for full description.

The problem hides in conflicts of kernel's devices naming convention and udev's one. For example, in my grub/menu.lst the root partition is /dev/hda1 while kernel considers it as /dev/sda1. I've solved this problem by using UUID's of devices.
$ blkid /dev/hda1
/dev/hda1: UUID="38ed6c23-3908-49f6-81eb-9945a173a60a" TYPE="ext3"

And then just typed this identifier into /etc/fstab and /boot/grub/menu.lst:

fstab:
{...}
UUID=38ed6c23-3908-49f6-81eb-9945a173a60a / ext3 defaults,errors=remount-ro 0 1
{...}

/boot/grub/menu.lst:
{...}
title Debian GNU/Linux, kernel 2.6.30.3
root (hd0,0)
kernel /boot/vmlinuz-2.6.30.3 root=UUID=38ed6c23-3908-49f6-81eb-9945a173a60a ro
initrd /boot/initrd.img-2.6.30.3
savedefault
{...}

Tuesday, July 28, 2009

NAT configuration in Debian

Currently my home network consists of several computers and each of them needs access to the Internet. My provider requires VPN connection, so one computer should be a router and control traffic by means of NAT. The easiest way is to buy a special router which has convenient web interface, but I hadn't enough money, so I decided to configure my Linux server for this purpose.

That's what I had: local home network with subnet number 192.168.1.0/24 (the first server's Ethernet controller is connected to it); DHCP in provider's network (the second server's controller) and VPN connection. I won't describe creation of VPN connection in this post, may be later.

Let's say that my eth0 interface has IP 192.168.1.1 (home network); eth1 interface obtains network setting through DHCP; and ppp0 is the interface which is created on VPN connection.

My routing table is simple and just describes my connections and default route:
$ sudo route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
213.219.200.230 0.0.0.0 255.255.255.255 UH 0 0 0 ppp0
10.251.50.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 ppp0

Now I have to configure my firewall, which will replace IP address for outgoing packets. The POSTROUTING chain of NAT is responsible for this. So I do this by simple command:
# /sbin/iptables -v -t nat -A POSTROUTING -o eth1 -j MASQUERADE
This activates NAT for eth1 interface. Each packet which is routed on this interface will be processed by firewall in order to replace source IP address of the packet with server's one (that's how NAT works).

I also duplicated this command for ppp0, and later wrote a script which is executed on system startup. This script fully configures iptables according to my requirements:
#!/bin/bash

echo "IpTables Loading";

int_if="eth0";
ext_if="eth1";
ppp_if="ppp0";

lo_if="lo";

cmd="/sbin/iptables -v";

$cmd -F;
$cmd -t nat -F;
$cmd -t mangle -F;
$cmd -X;

$cmd -A INPUT -i $lo_if -j ACCEPT;

$cmd -A INPUT -j ACCEPT;
$cmd -A OUTPUT -j ACCEPT;
$cmd -A FORWARD -j ACCEPT;

$cmd -t nat -A POSTROUTING -o $ext_if -j MASQUERADE;
$cmd -t nat -A POSTROUTING -o $ppp_if -j MASQUERADE;

echo 1 > /proc/sys/net/ipv4/ip_forward;
echo 1 > /proc/sys/net/ipv4/ip_dynaddr;

Thursday, June 25, 2009

Syntax highlight for HTML

VIM can create an HTML file with highlighted syntax for browsers.

To do this:
- open you file in Vim
- ensure that syntax is highlighted
- type command ":runtime! syntax/2html.vim"

You'll obtain file with the same name, but with ".html" extension with highlighted syntax.

Thursday, June 4, 2009

HTML: 1-pixel table border without CSS

I had to create a table with 1-pixel black border without CSS because I generated markup for an e-mail and most of Mail clients break all CSS when you push "Reply".

I tried border="1", but it didn't work because the border was in relief. Then I tried style but it didn't work too, because it left only outer border without any lines between cells. Making a border for each cell also didn't work because border between 2 cells was 2-pixel length.

Finally, I found a solution:
<TABLE cellSpacing="1" cellPadding="0" border="0" bgcolor="#000000">
<TR bgcolor="FFFFFF">
...


The idea is 1-pixel spacing between white cells and black background of the table that looks like border. You just set bgcolor of the table to color of your border you want to see and then set bgcolor of all rows to the color of outer background (so they seem to be transparent).

Thursday, April 23, 2009

HTML: 1-pixel horizontal line

<div> element is quite useful for creating 1-pixel width horizontal lines on Web page. For example, we can use empty DIV container and than write the following CSS style:
.separator {
height: 1px;
width: 90%;
display: block;
background-color: #555555;
margin-left: auto;
margin-right: auto;
}

This works fine everywhere except IE6. It creates a horizontal line with minimum width equal to the current font size:

The hack for IE6 is adding empty comment to the div:
<div class="separator"><!----></div>

The result is the same in all browsers:


P.S. "font-size: 1px" doesn't do the trick because, all the same, the resulting line is 2px or something

Thursday, April 9, 2009

HTML: align="center" through CSS

If you need to center one element inside another block element (for example TABLE inside a DIV element) you can use align="center". For example:
<div>
<TABLE width="50" align="center">
...
</TABLE>
</div>

But if you need to use CSS you can do this:
table {
margin-left: auto;
margin-right: auto;
# or just margin: 0 auto 0 auto;
}

Thursday, March 26, 2009

Shell: Reading a file line-by-line as is

I faced this problem when I had to copy one file to another, but change or skip some lines. It's mandatory that all spaces of the copied lines were left unchanged. First of all I've tried this script:
cat "$original_file" | while read str; do
{some analysis of str variable}
echo "$str" >> "$new_file";
done

But it cut all leading and trailing spaces in the new line (it's a feature of "read" tool). Fortunately, it provides $REPLY variable if run without arguments. In this case $REPLY contains the line as is, with all spaces. So the working code was:
cat "$original_file" | while read; do
str="$REPLY";
{some analysis of str variable}
echo "$str" >> "$new_file";
done

Wednesday, March 18, 2009

apt-get MergeList problem

If you have the following error while calling apt-get update:
# apt-get update
{...}
E: Dynamic MMap ran out of room
E: Error occurred while processing util-linux (NewVersion1)
E: Problem with MergeList /var/lib/apt/lists/ftp.debian.org_debian_dists_etch_main_binary-i386_Packages
E: The package lists or status file could not be parsed or opened.

The first solution is to try removing all downloaded lists:
# rm -vf /var/lib/apt/lists/*
Or move to some backup directory and remove from there later if there're no other problems.

If it doesn't help, try to find same of very similar repositories in the /etc/apt/sources.list and remove one of them. They can conflict with each other. Don't forget to repeat the first solution in order to clear cache

Tuesday, March 10, 2009

Experience with Sabayon 4 (Linux)

Yesterday I tried to install Sabayon 4 on my desktop. The reason for this was double-side DVD with 2 Linux distributives which goes with Linux Format magazine. I realized that if I have several different Linux'es why don't I just install them for several weeks and try.

After that I decided to rearrange my primary HDD. Before this I had 3 partitions on my 80Gb HDD: the first for Windows (40Gb) because I used to play some gluttonous games, the second (~30Gb) for Linux and the rest for Linux swap. I made the following changes: left Win partition unchanged, left swap unchanged, reduced Linux partition to about 14Gb. On the freed space I created 8Gb partition for the temporary Linux system and 4Gb partition for home directories. So, everything was ready for the new installation.

Insert Boot DVD and see the initial screen which offers boot from LiveCD (with ability to install OS, like in Ubuntu), try a 3D game (I don't remember it's name), start text-mode installation and start graphical installation.

A few words about LiveCD. It boots quite long (comparing with Ubuntu 8.10 from the previous LXF), but in the end you have quite stylish desktop, with running compiz, Firefox, several 3D games and lots of tools. I could easily surf the Internet a bit, communicate with friends using Pidgin and enjoy the 3D-desktop. A few disadvantages were - resolution of my screen, which was something like 1024x768 or 800x600 (native monitor's resolution is 1680x1050) and keymap switching. Languages were installed, but I couldn't switch them by means of keyboard (ctrl-shift, alt-shift, shift-shift, nothing...) even after some settings. The only way, I could deal with this problem was to click on the language icon by mouse. Hope it won't appear in permanent installation.

After that I tried to reboot and install it through graphical installer. When I was in LiveCD version, I read article about Sabayon and Gentoo (base of Sabayon) in Wikipedia and my expectations were OS like Slackware ot smth. else. Simple, fast and stable. I was wrong.

The first thing that I didn't like was packages selection. I was provided by several general options like "Install KDE", "Install Gnome", "Install 3D games" and so on. 8 points if I'm not mistaken. Of course, I could look through it's contents, but I couldn't select. Ok, I will install or remove required or unnecessary soft after the installation.

The next screen asked me about default services (Samba, NFS, CUPS...), and then I should choose the partitions. Without any doubt I pointed on 8Gb partition as root and the 4Gb as "/home". Click on "Forward"... What's this??.. I see a small window, which says that I'm trying to install the system on 8Gb partition, which is much smaller than 12Gb. I was shocked. It's Linux, which is famous for the ability of installation on diskette, telling me this. It's not Mandriva or other fat desktop distributive, with big amount of the latest software. It's a Gentoo based "true" Linux. And it said "pish!" on my 8Gb partition and asked for 12Gb. Why not 20Gb? Or may be 60Gb? What's the problem I can pull my HDD and it will enlarge! Easily. Seems like Windows Vista requires less...

But nevertheless, it's just a notification. I proceeded with installation, was asked about root passwords, new users, about GRUB loader and installation started offering me to "relax in my chair", just like Win98. When it was ~55% of installation I got an unhandled exception with some useless details and Installer exited. A bug in the Graphical installer. No matter, sometimes it happens. Reboot and select text installer.

Menu structure is the same, phrases are the same, but... Where is the Package selection window? I was asked about desktop environment and nothing more. I'm puzzled, why I'm limited in the text installer options. But hope, it's default selection is Ok. Answer on the rest known questions and, again, start the installation.

Don't remember how much it installed, but "No space left on device" reported one of the tools, which struggled through installer's interface directly on my console. Finish. So, it really requires so much space, and text-installer can't deselect packages. Graphical installer drops during installation (I tried several times). What should I do with this distributive?

It was midnight by the moment and I decided to think about this next day. Today I had another idea - I noticed "Update installer" button in the graphical installation yesterday. May be this exception will disappear in the new version. But... Somehow I damaged my DVD a little bit :( It's double-side and it's hard to use such a disk. I should have copied it on two DVD's before. It will be a lesson for future for me.

My resume about this distributive is the following. Probably it's quite useful and good-looking distributive, with integrated compiz and lots of useful soft. But the installer is quite buggy and useless. I didn't like that I can't select packages and even categories in the text mode. I think I'll try to install it again if have and an opportunity to get another DVD with this or next version.

Wednesday, March 4, 2009

.tgz vs .tar.gz

Comparing the result, there is no difference between "old-style" archival of directories (by means of pipe: tar -c dir1 | gzip > arch1.tar.gz) and "new-style" (by means of integrated ability of tar: tar -czf arch1.tgz dir1).

But there is a huge difference in speed. I tried to archive a large directory these 2 ways and obtained completely the same archives. I used "time" Linux tool to measure execution time. The first method (with pipe) took about 40 minutes to complete. The second method - 12.

The reason of the difference, I guess, in usage of pipe in the first method. Shell creates a temporary file for each pipe and all stdout of one application is stored into this file. Another application gets this file as stdin. We obtain the next sequence: reading of source directory, writing pipe temporary file, reading the temporary file and writing the archive file. In the second method we have only two of these actions: reading of source directory and writing to the target archive file.

Wednesday, February 25, 2009

Web: Stretch all elements to the width of the longest one

I had such a problem with IE (as usual). I had a column with dropdown lists and all these lists had different width. My layout required that all of these elements had the same width.

First of all I tried to create a table with one column and place all my elements into cells. After that I set style="width: 100%" for all content of the table. It worked ideally in all browsers. Except one... Internet Explorer compressed my table to several pixels. As I understood he realized that width of my table depends on elements and width of elements depends on table. This recursion made him crazy.

I thought that if it knew actual width of table, he would scale right. So I removed "width" from tags attributes and added JavaScript in the end of the page (after load and automatic scaling) and added "width=100%" through this script (when table width is known for a browser). The same result...

My solution was to add two lines to the script before setting elements width. I got the first cell as element by ID and then changed it's width from "auto" to it's actual current width. It worked in IE, Firefox, Opera, Chrome and Safari.

Here is the example and solution:
<form name="myform">
<Table cellspacing="5" cellpadding="0">
<!-- set id for the frist cell -->
<TR><TD id="firstcell">
<select name="list1">
<option>opt1</option>
<option>opt2</option>
</select>
</TD></TR>
<TR><TD>
<select name="list2">
<!-- lengths of the elements will be different by default -->
<option>my_opt1</option>
<option>my_opt2</option>
</select>
</TD></TR>
</Table>
</form>
<!-- some other HTML here... -->
<Script language="JavaScript">
// get the first cell object and fix it's width
var firstcell = document.getElementById('firstcell');
firstcell.style.width = firstcell.offsetWidth;

document.myform.list1.style.width = '100%';
document.myform.list2.style.width = '100%';
</Script>

Tuesday, February 24, 2009

Disable backup in VIM

Most modern Linux distributives set creation of backup files in vim by default. Probably it's useful in some particular cases (imho, it's better to make a backup for very important files yourself), but I lose my mind when I see the files like "textfile.txt~" everywhere I've edited a file.

To turn off the default backup add set nobackup to /etc/vim/vimrc or to your own vim config (in your home directory).

Web: Dynamic dropdown lists

In order to change dropdown lists dynamically (depending on values of other elements) the current options of the list should be deleted and new options should be created. This code probably will be located in a handler function of another element (for example, another list or radio button).

For instance, if have 2 options - the first is type of numbers (1,2,3... or I, II, III...) and the second - actual number (it's just a model, I don't say that it's a very useful example :)). So the HTML code will be:
<form name="numbers">
<select name="num_type" onchange="num_type_change();">
<option value="1">1,2,3...</option>
<option value="I">I,II,III...</option>
<!-- remember option values from the previous post -->
</select>
<select name="num_value">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</form>

Now let's write a JavaScript function num_type_change() which will change the second list according to the selected type of numbers:
<Script language="JavaScript">
function num_type_change() {
var type = document.numbers.num_type.value;
var numbers_list = document.numbers.num_value;

// Reset options list
numbers_list.options.length = 0;

// Add required options
if (type == '1') {
numbers_list.options[0] = new Option('1','1');
numbers_list.options[1] = new Option('2','2');
numbers_list.options[2] = new Option('3','3');
numbers_list.options[3] = new Option('4','4');
}
else {
numbers_list.options[0] = new Option('I','I');
numbers_list.options[1] = new Option('II','II');
numbers_list.options[2] = new Option('III','III');
numbers_list.options[3] = new Option('IV','IV');
}
}
</Script>

Value of a dropdown list through JavaScript

A dropdown list is a usual element of a from on WebPages. And very often it's checked by some JaveScript before sending the data to the host (or for whatever other reasons). The following list:
<form name="myform">
<select name="mylist">
<option>A</option>
<option>B</option>
<option>C</option>
</select>
</form>

could be accessed by means of JavaScript the following way: "document.myform.mylist.value". It will work in most of the browsers except Internet Explorer. Especially for this one, HTML code should be little bit different:
<form name="myform">
<select name="mylist">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</form>

Now the same JavaScript code can be used in IE, too.

Friday, January 23, 2009

Colorless VIM in Debian

After installation of Debian 4.0 I had no colors when using Vim. Command like ":syntax on" gave no results, instead it reported: "E319: Sorry, the command is not available in this version". The solution was found there http://www.cyberciti.biz/faq/howto-install-full-vim-under-ubuntu-linux/.

Debian distributives (my and also Ubuntu, as I could see in the source above) install vim-tiny by default. Just "apt-get" a normal version of Vim:
# apt-get install vim

And also uncomment line "syntax on" in /etc/vim/vimrc if it's still commented.

Monday, January 19, 2009

Searching for a package in Debian

There are useful tool in Debian which manage packages within your system - apt-get and aptitude. However, both of them have one disadvantage - they can't tell which packages contain a file your're looking for. In such a situation one can use a special script called apt-file.

To install:
# apt-get install apt-file

Before the first usage it should create indices:
# apt-file update

And now we can use it:
# apt-file search stdio.h