Random Wisdom

Linux

BASH as a simple calculator

by on Sep.25, 2006, under How To ..., Linux, Software

As long as you are satisfied with integer results:

[darkknight@darkworld ~]$ echo $[32 * 98]
3136
[darkknight@darkworld ~]$ echo $[332 / 98]
3
[darkknight@darkworld ~]$ echo $[29 + 56]
85
[darkknight@darkworld ~]$ echo $[29 - 156]
-127

For full precision, one can use bc:

$ bc -l
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
332/98
3.38775510204081632653
7 Comments :, , more...

Finally, NTFS write support!

by on Jul.16, 2006, under Linux

Slashdot ran a story yesterday about the new fully open source NTFS driver that has full read and write support for NTFS volumes. Although still in BETA phase it looks really promising. Too bad it doesn’t work on 64-bit systems yet. I’m definitely keeping my eyes peeled for this one ..

More details and download links here:

[announcement] ntfs-3g: open source read-write driver

Leave a Comment :, , more...

Creating directory hierarchies in bash

by on Jul.16, 2006, under How To ..., Linux, Software

The BASH ‘brace expansion‘ feature can be used to create whole directory trees using a single command. From the man page for BASH:

Brace Expansion
       Brace expansion is a mechanism by which arbitrary strings may be gener?
       ated.   This  mechanism is similar to pathname expansion, but the file?
       names generated need not exist.  Patterns to be brace expanded take the
       form of an optional preamble, followed by either a series of comma-sep?
       arated strings or a sequence expression between a pair of braces,  fol?
       lowed  by  an  optional  postscript.   The preamble is prefixed to each
       string contained within the braces, and the postscript is then appended
       to each resulting string, expanding left to right.

       Brace  expansions  may  be nested.  The results of each expanded string
       are not sorted;  left  to  right  order  is  preserved.   For  example,
       a{d,c,b}e expands into `ade ace abe'.

       A  sequence  expression takes the form {x..y}, where x and y are either
       integers or single characters.  When integers are supplied, the expres?
       sion  expands  to each number between x and y, inclusive.  When charac?
       ters are supplied, the expression expands  to  each  character  lexico?
       graphically between x and y, inclusive.  Note that both x and y must be
       of the same type.

       Brace expansion is performed before any other expansions, and any char?
       acters  special to other expansions are preserved in the result.  It is
       strictly textual.  Bash does not apply any syntactic interpretation  to
       the context of the expansion or the text between the braces.

       A  correctly-formed  brace  expansion must contain unquoted opening and
       closing braces, and at least one unquoted comma  or  a  valid  sequence
       expression.   Any incorrectly formed brace expansion is left unchanged.
       A { or , may be quoted with a backslash to prevent its being considered
       part  of  a brace expression.  To avoid conflicts with parameter expan?
       sion, the string ${ is not considered eligible for brace expansion.

       This construct is typically used as shorthand when the common prefix of
       the strings to be generated is longer than in the above example:

              mkdir /usr/local/src/bash/{old,new,dist,bugs}
       or
              chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}

       Brace  expansion  introduces  a  slight incompatibility with historical
       versions of sh.  sh does not treat opening or closing braces  specially
       when  they  appear as part of a word, and preserves them in the output.
       Bash removes braces from words as a  consequence  of  brace  expansion.
       For  example,  a word entered to sh as file{1,2} appears identically in
       the output.  The same word is output as file1 file2 after expansion  by
       bash.   If strict compatibility with sh is desired, start bash with the
       +B option or disable brace expansion with the +B option to the set com?
       mand (see SHELL BUILTIN COMMANDS below).

So, for example:

$ mkdir -p root/{1/{1.1,1.2,1.3},2,3/{3.1,3.2/{3.2.1,3.2.2}}}

$ tree
.
`-- root
    |-- 1
    |   |-- 1.1
    |   |-- 1.2
    |   `-- 1.3
    |-- 2
    `-- 3
        |-- 3.1
        `-- 3.2
            |-- 3.2.1
            `-- 3.2.2

11 directories, 0 files

The ‘-p’ option to mkdir makes it create the parent directories if they do not exist.

Leave a Comment :, more...

Working with LaTeX & KPDF

by on Jul.11, 2006, under LaTeX, Linux, Software

KPDF is particularly handy in “watch file” mode. To activate, go to:

Settings -> Configure KPDF…

and check “Watch file” in the General section.

This causes KPDF to watch the file for changes and reload it automatically. Really handy when you don’t want to keep navigating to the old page after an edit and reload ..

2 Comments :, more...

Installing MS TrueType core fonts on Linux

by on May.31, 2006, under How To ..., Linux, Software

Instructions obtained from:
http://corefonts.sourceforge.net/

  1. Install cabextract and fedora-rpmdevtools:
    $ sudo yum install cabextract fedora-rpmdevtools
  2. Create the build tree:
     $ fedora-buildrpmtree 
  3. Get the SPEC file:
    $ cd ~/rpmbuild/SPEC && \
      wget http://corefonts.sourceforge.net/msttcorefonts-2.0-1.spec
  4. Get the fonts and create the RPM package:
     $ rpmbuild -bb msttcorefonts-2.0-1.spec 
  5. Install the fonts package:
     $ sudo rpm -ivh ../RPMS/noarch/msttcorefonts-2.0-1.noarch.rpm 

Restart any running application to use the new fonts.

Leave a Comment :, , more...

Installing RPMs as a regular user

by on Apr.20, 2006, under How To ..., Linux, Software

A while back I needed some packages on a machine that I don’t have admin rights to. Grabbing the source and recompiling would have been a pain the a** so I decided to read the rpm man pages and look for a way to install packages in the user home directory. Since it’s such a nice package manager, it comes with options that allows me to do just that. The command needed is:

$ rpm -ivh --relocate OLDPATH1=NEWPATH1 [--relocate OLDPATH2=NEWPATH2 ...] \
      --badreloc package.rpm

where OLDPATH is the path in the package; and NEWPATH is something like /home/user/userroot/usr, etc.

It’s best to run

$ rpm -qpl package.rpm

to see exactly which paths are going to be used by the package. For example, if the package foo.rpm produces:

$ rpm -qpl foo.rpm
/usr/bin/foo
/usr/lib/foo.so.0.0
/usr/lib/foo.so.0
/usr/share/doc/foo/README

then the steps needed are:

$ mkdir -p ~/myroot/usr/bin ~/myroot/usr/lib ~/myroot/usr/share/doc
$ rpm -ivh --relocate /usr=/home/$USER/myroot/usr --badreloc foo.rpm

That’ll install the package under the hierarchy ~/myroot. There will some errors from rpmdb but this is fine since the rpmdb is in a filesystem that we do not have write access to. The only repercussion is that rpm will have no record of the package foo being installed (so packages will have to be removed by hand); but that’s okay since we cannot possibly mess up the system while installing stuff under our own home dirs.

The only steps remaining are to add the new paths to the binary and library search paths. To so this, add the following lines to ~/.bash_profile

        PATH=$PATH:$HOME/myroot/usr/bin
        LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/myroot/usr/lib

        export PATH LD_LIBRARY_PATH

This is only efficient as long as the package does not have too many unmet dependencies — since you will need to grab and install all missing dependencies along with the package itself. And even if you had installed some of the dependencies earlier using this method, rpm will not know about it since there will be no entry in the system rpmdb.

Leave a Comment :, , more...

FC kernel rebuild HOWTO

by on Apr.20, 2006, under How To ..., Linux

Paul Howarth has a very nice article here:

http://www.city-fan.org/tips/TweakKernelPackage

The main benefit is that you can use rpmbuild to generate kernel RPMs using the same SPEC file as the distribution itself — resulting in packages that are as easy to handle as those from FC itself.

Leave a Comment :, , , more...

SSH automatic reconnect on timeout

by on Mar.15, 2006, under How To ..., Linux, Software

There may already be some builtin option that allows a client to reconnect when a timeout occurs but I was too lazy to look through the man pages. So I came up with line of bash commands that will do just that:


$ while [ 1 ]; do ssh user@host.domain; sleep 120; done

This will keep reconnecting to the host 120 seconds after a connection drops out for whatever reason. This is specially handy to make sure that a remote tunnel stays open. Right now I use it to reach a single host (A,http) on a remote private network from home (B) via another machine in the private network (C) and an intermediate SSH server (D):

  1. [user@C ~]$ while [ 1 ]; do ssh -R someport:A:80 user@D; sleep 120; done
  2. [user@B ~]$ ssh -L 80:localhost:someport user@D
  3. [root@B ~]# echo "127.0.0.1    A" >> /etc/hosts

Now it is possible to type “http://A” and visit the site from B as easily as from within the remote private network.

4 Comments :, more...


coLinux

by on Jan.19, 2006, under How To ..., Linux, Software

Recently, I needed to use Umbrello but didn’t have access to a workstation that had it installed. And since I’m stuck with a Windows PC, I thought I’d take a look at the KDE-Cygwin project. It seems that they’ve stopped working on the project since there is a better alternative: Cooperative Linux. It’s basically a modified Linux 2.6.10 kernel that runs on Windows — kind of a like a virtual machine but it’s different — it’s definitely a lot faster. Here’s a snippet from the coLinux homepage:

Cooperative Linux is the first working free and open source method for optimally running Linux on Microsoft Windows natively. More generally, Cooperative Linux (short-named coLinux) is a port of the Linux kernel that allows it to run cooperatively alongside another operating system on a single machine. For instance, it allows one to freely run Linux on Windows 2000/XP, without using a commercial PC virtualization software such as VMware, in a way which is much more optimal than using any general purpose PC virtualization software.

The project page had a preconfigured Debian image and so that’s what I’m using at the moment. The native X server does not work though — so the Cygwin based X server is needed to diplay all the GUI stuff.

Getting it installed was a piece of cake. The Wiki site for coLinux has a lot of helpful info.

Once the basic text-mode linux is up and running (must be able to reach the host via the network), we need to run some commands from the cygwin shell. First run


$ cygserver-config

to configure the cygwin server and then start the server with:


$ cygrunserver -S cygserver

Then, we need to set


$ export CYGWIN=server

And then start the X server in rootless mode. The easiest way is to just make a copy of the startxwin.bat file and then edit it to change the options to XWin.

Once the X server is running, we need to run “xhost +” from the xterm that had popped up. Then, we switch over to the coLinux console and run:


$ export DISPLAY=<ip of windows host>:0

And that’s it! Now you have a fully functional linux distro! I would have gone for a Fedora install but couldn’t find a proper image.

Leave a Comment :, , , more...