Random Wisdom

Software

MATLAB – Interrupting function execution

by on Mar.02, 2007, under How To ..., Software

MATLAB itself does not provide any way of arbitrarily interrupting/pausing a function at mid-execution to examine the internal function stack/variables. It is a pity since such a mechanism would be an extremely valuable debugging tool.

Finally, I realized that this is not as impossible as it seems. In fact, it is EXTREMELY simple! All that is required are four lines of code in any script/function:

[statcode, result] = system('ls sometoken');
if statcode == 0
  keyboard
end

The system command is used to run system console commands. Here, a simple check is made for the existence of the file ‘sometoken’ in the current directory. If the file exists, statcode is set to zero and the if condition is satisfied. The keyboard command is then executed which causes MATLAB to pause execution and return control to the command line! In order to continue execution, ‘sometoken’ must be deleted/removed from the current directory and the command return must be issued.

The best place for this code is perhaps in any loop that might exist in the MATLAB script/function. Then, to cause execution to pause, one simply needs to create a file ‘sometoken’ in the current directory.

14 Comments :, more...

Linux NFSv4 Howto

by on Feb.27, 2007, under How To ..., Linux, Software

NFS is commonly used to share files on Linux. NFSv4 is the latest protocol and circumvents firewall related complications experienced with NFSv3 by requiring only ONE fixed tcp port open on the server side. It is surprisingly easy to set up:

Server side:

  1. Edit /etc/exports and add directories to be exported (fsid=0 is a mandatory option for nfs4) and authorized clients (check the exports manpage)
  2. Open up tcp port 2049 on the firewall
  3. # /etc/init.d/nfs restart
  4. # chkconfig –level 345 nfs on

Client side:

  1. # mount -t nfs4 -o rw,intr,hard server:/ /mount/point

It is not necessary to specify the exact path on the “server:/” with NFSv4.

Useful sites:

Learning NFSv4 with Fedora Core 2
RHEL4 NFS manual
Linux NFS-HOWTO

Leave a Comment :, more...

Linux authentication using LDAP

by on Feb.27, 2007, under How To ..., Linux, Software

There’s a lot of information out there but none really provide a step-by-step guide that would be useful to a novice:

This one however, sheds some light on the bigger picture:

LDAP Authentication In Linux

Once the initial configuration of the server is complete, LDAP Browser/Editor serves as a very useful client/admin tool.

Leave a Comment :, , more...

Cross-platform filesystem access

by on Nov.21, 2006, under How To ..., Linux, Software

Looks like the ntfs-3g driver has already gained a lot of popularity and is quite easy to use:

NTFS-3G Read/Write Driver

The driver itself is supposedly available from the Fedora Extras repository for FC6 onwards. Completely safe read/write access to NTFS drives from Linux — now that’s a dream come true!

In the meanwhile, it looks like there’s also a windows Ext2 file-system driver that allows full read/write access to Ext2/3 volumes:

Ext2 Installable File System For Windows

Of course, one of the drawbacks here is that the Ext3 volume is mounted as Ext2 — so there is no journaling support. In case of a ‘dirty’ unmount e2fsck will have to be run. The other drawback is the fact that is won’t work with LVM.

So, these drivers really open up a lot of choices. But I guess the best option is to have the shared drive as NTFS since the ntfs-3g driver takes care of journaling.

Leave a Comment :, , , , , more...

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...

Cropping a PDF Document

by on Jul.31, 2006, under How To ..., LaTeX, Software

Easily accomplished using pdftops:

$ pdftops -paperw WIDTH \
             -paperh HEIGHT \
             -noshrink -expand document.pdf && ps2pdf document.ps

WIDTH and HEIGHT are in points — they basically specify the dimensions of the image to be cropped.

Content is extracted from the center of the page. This technique is specially useful as a bypass for using psfrag with pdfLatex:

  • Save EPS figure with TAGS
  • Create a very simple tex document that simply includes the figure (centered) with psfrag replacements and run latex -> dvips -> ps2pdf
  • Follow the step above to crop out the figure.

The cropped out figure will have the TAGS replaced and be in PDF format — ready to be used with pdfLatex!

UPDATE [16 July 2009] It looks like pdfcrop might actually be a better option:

$ pdfcrop --help
PDFCROP 1.5, 2004/06/24 - Copyright (c) 2002, 2004 by Heiko Oberdiek.
Syntax:   pdfcrop [options] <input[.pdf]> [output file]
Function: Margins are calculated and removed for each page in the file.
Options:                                                    (defaults:)
  --help              print usage
  --(no)verbose       verbose printing                      (false)
  --(no)debug         debug informations                    (false)
  --gscmd <name>      call of ghostscript                   (gs)
  --pdftexcmd <name>  call of pdfTeX                        (pdftex)
  --margins "<left> <top> <right> <bottom>"                 (0 0 0 0)
                      add extra margins, unit is bp. If only one number is
                      given, then it is used for all margins, in the case
                      of two numbers they are also used for right and bottom.
  --(no)clip          clipping support, if margins are set  (false)
  --(no)hires         using `%%HiResBoundingBox'            (false)
                      instead of `%%BoundingBox'
  --papersize <foo>   parameter for gs's -sPAPERSIZE=<foo>,
                      use only with older gs versions <7.32 ()
Examples:
  pdfcrop --margins 10 input.pdf output.pdf
  pdfcrop --margins '5 10 5 20' --clip input.pdf output.pdf

The tool comes as a part of the ‘tetex’ package.

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...