Summer Nights

Let's make the world a better place using digital technologies!

Useful ‘sed’ commands

Delete all empty lines containing a space in the input.txt and save the result as output.txt:
cat input.txt | sed '/^ $/d' > output.txt
In case when the line is completely empty (i.e., no space either), then:
cat input.txt | sed '/^$/d' > output.txt

Tip:To see what a file really contains including non-printable characters, etc. use:
cat -A input.txt

Delete a line containing a given string in file input.txt:
sed '/string/d' input.txt

Replace all case insensitive occurrences of string1 with string2 in file input.txt and save to file output.txt:
sed 's/string1/string2/Ig' input.txt > output.txt
Note: I is for case-Insensitive and g for global replace (multiple matches per line). string1 and string2 are regular expressions.

Insert a blank line before every line where the regexp matches:
sed '/regexp/{x;p;x;}' < input.txt > output.txt

Continue Reading

Mapping Custom Keyboard/Remote Control Keys in Linux

For keyboard mapping, use KeyTouch which is an excellent program to configure almost any special keyboard including all custom keys on Linux. You can install it by:
apt-get install keytouch

For mapping keys from a remote control (e.g., Pinnacle, in my case), use this excellent tutorial by Christian Weiske. There are some custom scripts towards the end of this article which did not work for me because, apparently, Kaffeine has undergone some changes. However, the following worked for me:

# This maps play/pause to Kaffeine. Just create a shortcut using MenuEditor in KDE.
# It assumes that you have already mapped your custom key using .Xmodmap file. Disable "Enable Launch Feedback" to avoid taskbar windows.
# Place in /usr/bin/ as play_pause (or any other name of your liking)
kaffeine=`dcop | grep kaffeine -c`
if [ $kaffeine = '1' ]
dcop kaffeine KaffeineIface pause

Continue Reading

Working with GnuPG

Almost all of the material below has been gratefully taken from Charles Lockhart’s page here. I am publishing it here mostly for my own convenience should I use this information later on.

  • To create a key:
    gpg --gen-key
  • To export a public key into file public.key:
    gpg --export -a "User Name" > public.key
    where -a stands for ASCII output.
    To export a private key:
    gpg --export-secret-key -a "User Name" > private.key
  • To import a public key:
    gpg --import public.key
    To import a private key:
    gpg --allow-secret-key-import --import private.key
  • To delete a public key (from your public key ring):
    gpg --delete-key UserName
    If you have same UserName for multiple keys, then you can issue the following command:
    gpg --delete-key HexKeyID
    where HexKeyID is the hexadecimal unique ID of any given key. The same hex ID can also be used to export a key.
    To delete an private key (a key on your private key ring):
    gpg –delete-secret-key User Name
  • To list the keys in your public key ring:
    gpg --list-keys
    To list the keys in your secret key ring:
    gpg --list-secret-keys
  • To generate a short list of numbers that you can use via an alternative method to verify a public key, use:
    gpg --fingerprint > fingerprint
    This creates the file fingerprint with your fingerprint info.
  • To encrypt data, use:
    gpg -e -u SenderUserName -r ReceiverUserName somefile
    There are some useful options here, such as -u to specify the secret key to be used, and -r to specify the public key of the recipient.
  • To decrypt data, use:
    gpg -d mydata.tar.gpg
Continue Reading

Installing CVS Server on Ubuntu

The installation of CVS includes the following:

  1. The CVS client
  2. The CVS server (cvsd)
  3. The optional CVS web interface using CVSWeb

Proceed as follows:

  • First install all three of the above
    apt-get install cvs cvsd cvsweb
    When asked about the location of CVS repository, enter /cvsrepo. This is relative to the cvsd path (/var/lib/cvsd, in Ubuntu) You can later change it in the /etc/cvsd/cvsd.conf file.
  • cvsd-buildroot /var/lib/cvsd
  • Create the repository folder:
    mkdir /var/lib/cvsd/cvsrepo
  • Change the owner of the repository:
    chown -R cvsd:cvsd cvsrepo
  • Now initialize the repository. This will be required for all the repository folder that you may later create:
    cvs -d /var/lib/cvsd/cvsrepo init
  • Now we need to add the user names that will be accessing any given repository:
    cvsd-passwd /var/lib/cvsd/cvsrepo +username
    If you do not want this user to access the server through the ssh as well, use AllowUsers command in /etc/ssh/sshd_config to allow ssh access to only the allowed users and not for the cvs users.
  • We need to make sure that ordinary linux users are now able to access the repository. Edit /var/lib/cvsd/cvsrepo/CVSROOT/config file and have SystemAuto=no.
  • To add a project to the CVS repo:
    cd /tmp
    mkdir testproj
    cd testproj
    cat >> readme.txt
    This is a test project readme.
    cvs import -m "Test Project" TestProj TestProj V1
  • Now cvsd can be checked using:
    cvs -d :pserver:username@localhost:/cvsrepo login
    cvs -d :pserver:username@localhost:/cvsrepo checkout TestProj
  • To use the web interface of CVSWeb, you need to access (assuming Apache installation, etc. is in place and running) it through http://AddressOfLinuxHost/cgi-bin/cvsweb.
  • The icons and css style-sheet for the web interface are copied to /usr/share/cvsweb/. You need to copy them to a place where they are accessible by the apache server (e.g. /var/www/) and update these settings in the /etc/cvsweb/cvsweb.conf file.
  • The CVSROOT is visible by default. This can be prevented using “hidecvsroot” => “1” setting in /etc/cvsweb/cvsweb.conf.
  • The /etc/cvsweb/cvsweb.conf file also contains settings to select repositories which are to be shown on the web interface.

In writing this post, I have gratefully used the information on the following pages:

Continue Reading

Using openssl with Apache

  • First you have to make sure that openssl is installed. Otherwise
    apt-get install openssl
  • Now we need to create certificates for the webserver:
    cd /etc/ssl/certs
    openssl genrsa -des3 -out server.key 4096
    openssl req -new -x509 -days 1500 -key server.key -out server.crt

    Fill the values with appropriate information. Remeber the passphrase as we shall need it later in the apache configuration.
  • These keys now need to be copied to the apache ssl folder:
    cp server.crt /etc/apache2/ssl/ssl.crt/
    cp server.key /etc/apache2/ssl/ssl.key/

    Delete or secure the original files after copying.
  • Now we need to setup apache to use these certificates. Edit /etc/apache2/sites-enabled/ssl and add the following (or as appropriate) at the end of the file:
    SSLRandomSeed startup file:/dev/urandom 1024
    SSLRandomSeed connect file:/dev/urandom 1024
    SSLSessionCache shm:/var/log/apache2/ssl_cache_shm
    SSLSessionCacheTimeout 600
    # The following echoes the passphrase for key file on the server startup
    SSLPassPhraseDialog exec:/path/to/bash/script
    SSLCertificateFile /etc/apache2/ssl/ssl.crt/server.crt
    SSLCertificateKeyFile /etc/apache2/ssl/ssl.key/server.key
  • Now restart the apache:
    /etc/init.d/apache2 restart
  • Make sure that ssl is present in mods-enabled folder. The file /etc/apache2/mods-enabled/ssl.conf provides some configuration options for ssl module.
  • There you go with the ssl enabled in the apache. Do not forget to use https:// to access the secure sites. The /etc/apache2/sites-enabled/ssl file contains other important settings for secure sites on your webserver.
Continue Reading

Mounting a linux folder in windows as a network drive using Samba

This procedure is based on this excellent tutorial on Ubuntu forums by stormbringer.

  1. First you need to make sure that samba is installed. If not, use the following to install it.
    apt-get install samba
    This assumes you are logged in as root.
  2. Now we need to edit the samba configuration file:
    emacs /etc/samba/smb.conf
    In the [global] section, make sure you have the following:
    netbios name = HOSTNAME
    Here HOSTNAME should be your linux host name.
    In the following line, you should give the workgroup of your windows computer:
    workgroup = WORKGROUP
    And enable the wins support:
    wins support = yes
    Now create a share called mylinuxshare, e.g., using the following section in smb.conf file:
    path = /path/to/linux/sharefolder
    browseable = yes
    read only = no
    guest ok = no
    create mask = 0644
    directory mask = 0755
    force user = USERNAME

    where USERNAME must fulfill the following conditions:

    • It is registered on both windows and linux machines.
    • It has same password on both windows and linux machines.
    • It has the desired access to the shared linux folder (/path/to/linux/sharefolder).

    Now you should save this file and start samba by:
    /etc/init.d/samba start

  3. The username specified in smb.conf must now be registered and enabled with samba. For this:
    smbpasswd -L -a USERNAME
    smbpasswd -L -e USERNAME

    This sets up the Linux side of the share.
  4. On windows do the following:
    • In the TCP/IP Settings of the active LAN connection, go to advanced tab and in WINS, add the ip address of the linux box.
    • Select “Use NetBIOS over TCP/IP”
    • Reboot Windows
  5. In the “Run” textbox, enter \\\MyLinuxShare, and you should see the files shared by the linux machine. You can map it as a network drive in windows as well.
Continue Reading

Basic User Management in Linux

To add a new group:
groupadd groupname
To add user username to this group:
useradd -g groupname username
To add user username with home directory to a group:
useradd -m -g groupname username
To assign bash shell to this newly created user:
usermod -s /bin/bash username
To delete user username:
deluser username
To delete group groupname:
delgroup groupname
To change password of a user (you must be logged in as root):
passwd username
Another better command adduser also creates the home folder automatically (which has to be specified with -m option using useradd):
adduser username

Continue Reading

Adding a Windows Share in Linux using Samba

Update your /etc/fstab file in a way similar to:

# /etc/fstab: static file system information.
# proc /proc proc defaults 0 0
# /dev/sda1
/dev/sda1 / ext3 defaults,errors=remount-ro,usrquota,grpquota 0 1
# /dev/sda5
/dev/sda5 none swap 0 0
#/dev/scd0 /media/cdrom0 udf,iso9660 user,noauto,exec 0 0
//$ /home/user1/share1 cifs auto,iocharset=utf8,uid=linuxuserid,gid=users,credentials=/somefolder/credentialfile,file_mode=0775,dir_mode=0775 0 0
//$ /home/user1/share2 cifs auto,iocharset=utf8,uid=linuxuserid,gid=users,credentials=/somefolder/credentialfile,file_mode=0775,dir_mode=0775 0 0

where credentialfile should either provide windows username/password through some script or in text, like, e.g.,:

After sharing the given drive/folder on windows, and creating the respective folders in /home/user1, issue the following command to mount these new shares at above-mentioned locations in linux:
mount -a
Now you should have the windows shares mounted in linux as 755 (or other settings of your choice).

Continue Reading

Using newmat11 for Matrix Calculations

If you need a good C++ matrix library for scientific computations, then newmat11 could be your choice. Written by Robert B. Davies, it is a complete matrix library, providing all matrix arithmetic, transpose, inverse, SVD, etc.

You have to compile the library into either a static lib or a dll library. I compiled it with Visual Studio 2003 as a static library and then used some of its functions in a sample project.

To use it as a static library, you need to add the newmat11.lib file to the Visual Studio project and then include the newmat header files in your project.

If you want, you can download this Visual Studio 2003 solution as a starting point. This project links to the static library which can be found in newmat11/Debug folder. There is also a release version, in the Release folder.

Continue Reading
Continue Reading