PAUL SCHERRER INSTITUT
Swiss Light Source
Computing and Controls
SLS
PSI HomeSLS HomeControls HomeAIT Home
Google

web sls.psi.ch
Howto Index
Pikett Home
Machine Status
Archiver

Author: David Maden
Phone: +41 56 310 3616
Updated: 19.6.2007


Printer friendly version
 

Tips n' Tricks

Here are some (maybe) useful tips and tricks about how to do things in various scripting languages, programming languages and operating systems. It is an Aide Memoire of things that I have, at some time, found useful and tend to forget.

Awk

    As a rather contrived example of using awk, which does, however, illustrate some of its power, consider the following challenge.

    One has taken a photograph with a digital camera, downloaded the JPEG image onto an MSwindows system, modified the image using PhotoShop or similar utility and then discovered that the time setting of one's camera was 7min 50secs slow. Having transferred the file to a linux system, one now wants to set the modification time of the file, i.e. the time normally shown with the ls -l command, via the touch command to correspond to the time at which the photograph was originally taken.

    To help understand the example, here is a description of my empirical observations about JPEG images, at least those from a Sony DSC-T?? camera. The command:

          strings  | grep 2007
        
    applied to a file downloaded from a camera will generate 4 identical lines of output of the form:
          2007:mm:dd hh:mm:ss
        
    giving the time at which the photograph was taken. If the file has been modified with PhotoShop, the first of the 4 lines gives the time of the modification, and the other 3 lines still give the time at which the photograph was taken. So, our challenge is to pick out the second line, add 7min 50secs to the time, convert the time to a format accepted by the touch command and finally touch the file with the resulting time. The following bash function will calculate and reformat the date:
         function getDate () {
           if [ $# != 1 ]; then echo Usage: getDate \<jpeg-file\>; return 1; fi
           strings $1 | grep 2007 > /dev/null
           if [ $? != 0 ]; then echo No date found in JPEG file.; return 1; fi
           strings $1 | awk -F: '          # Specify a colon as the field separator
             /2007/{x++;                   # Count the number of records found
                                           # containing "2007"
                  if (x == 2) {            # Process the second such record.
                                           #   $1 is the year
                                           #   $2 is the month
                                           #   $3 is the day and hour: "dd hh"
                                           #   $4 is the minutes
                                           #   $5 is the seconds.
                    split ($3, DDHH, " "); # The Day/Hour field needs to be split. 
                    DD = DDHH[1];
                    HH = DDHH[2]; 
                    MM = $4+7;                   # Add 7 minutes and 
                    SS = $5+50;                  # 50 seconds.
                    if (SS > 59) {MM++; SS-=60}; # Correct for overflow of seconds and
                    if (MM > 59) {HH++; MM-=60}; # minutes, but ignore day overflows.
                    printf "%s-%s-%02d %02d:%02d:%02d\n", $1, $2, DD, HH, MM, SS;
                  }
             }
           '
         }
    and, with this function defined, the following function will change the modification date of its arguments:
         function reTouch () {
           if [ $# == 0 ]; then echo Usage: reTouch \<jpeg-file\> ...; return 1; fi
           for jpg do
             if [ -e $jpg ]; then
               echo Processing $jpg ...
               newDate="$(getDate $jpg)"
               if [ $? = 0 ]; then
                 touch -d "$newDate" $jpg
               else
                 echo "      " No date found in $jpg
               fi
             else
               echo "      " $jpg not found.
             fi
           done
         }
      

bash

  • To find out the height and width of one's screen in pixels:
          height=$(echo $(xwininfo -root | grep Height) | \
                   cut -d" " -f2)
          width=$(echo $(xwininfo -root | grep Width) | \
                   cut -d" " -f2)
        

  • The following functions are defined in /afs/psi.ch/user/m/maden/public/.bashrc:
    • caps <file>
      Uses a2ps to convert an ASCII file to PostScript and, optionally, print it.
    • path-append <path>
      Appends a path to PATH, checking that no duplicates occur.
    • path-list
      Lists the items in PATH in a readable form.
    • path-prefix <path>
      Prepends a directory to PATH, checking that no duplicates occur.
    • xedf
      Starts xemacs as a background process using xemacs' default fonts.

  • How to get a printout of a window:

    Assuming that your favourite desktop does not have a convenient tool for printing a snapshot of a window, the following procedure will do it for you:

    • Ensure that the window to be printed is fully visible.
    • In another terminal window, ensure that the PRINTER environment variable specifies the desired printer and then, if you are running on a RedHat 6.2 system, issue the command:

           xwd | xwdtopnm | pnmtops | lp

      If, however, you are running on a RedHat 7+ system, issue the command:

           xwd | xwdtopnm | pnmtops -nosetpage | lp

      The desired printer may be specified explicitly by appending the -d option to the lp command.
    • Click with the mouse in the window to be printed.
    The command xwinpr.bash -simple effectively embeds these commands.

  • An alias for Single Sided Printing:

         alias   lp1="lp   -o   cpi=14   -o   lpi=8   -o   Duplex=None"

  • An alias for Double Sided Printing:

         alias   lp2="lp   -o   cpi=14   -o   lpi=8   -o   Duplex=DuplexNoTumble"

  • An alias for Colour Printing:

         alias   colour="lp   -d   WSLA_112_2"

  • An alias for printing Colour Transparencies:

         alias   transp="colour   -o   media=A4,Transparency"


C


 

C++


 

Desktops

  • Another Level
    • To Make a Window Sticky Automatically
      Under the Another Level window manager, it is possible to cause a window to be sticky when it is created by means of a resource file. The file used to do this is:

            /etc/X11/AnotherLevel/fvwm2rc.defstyles.m4

      As an example, the SLS launcher window is made sticky automatically via a record of the form:

            Style  "launcher.tcl*"  Sticky

      Maybe it is possible to have a local copy of this file which will override the file in /etc/X11/AnotherLevel. Maybe http://www.poulpetersen.dk/linux/ukfvwm.htm is a useful starting point for more information.

  • Gnome
  • Under the Gnome desktop, several window managers are available. The one most commonly configured at SLS is Enlightenment, to which the following notes apply.

    • To Make a Window Sticky
      To flip a window's sticky property between sticky and not-sticky under the Enlightenment window manager, pop-up the menu under the top-left square of the window's banner, drag the cursor to the Stick/Unstick item and release the mouse.

    • To Change a Window's Stacking Order
      A window's stacking order is changed using the same pop-up menu as is used to change the window's stickiness. In this case, drag the cursor to the item labelled Set Stacking, enter the sub-menu and release the mouse on the desired stacking level, viz. Below, Normal, Above or On Top.

    • To Make a Window Sticky Automatically
      To make a window automatically sticky when it is created under the Enlightenment window manager, create the window, make it sticky (and modify its other attributes, if so desired) as described above and then pop-up the menu under the top-left square of the window's banner. By following the sub-menu Remember State to All Attributes (or one of the other items, if more appropriate), it is possible to cause the window manager to remember the window's attributes. Presumably this information is saved in a resource file somewhere. If anyone discovers which one it is and how, please send me a note about it.

  • KDE
    • To Make a Window Sticky Automatically
      Under the KDE window manager, one can set a so-called Filter using the "KDE Control Center" and following the menus Window Behavior (sic ... why on earth can't Americans learn to spell!) --> Advanced --> Filters. There is then a GUI which enables one to set matching patterns for window names and to specify the attributes of windows so selected.

      This GUI would appear to update the resource file, ~/.kde/share/config/kwmrc.


EPICS


 
  • Here is an example C program which uses EZCA, the Easy-to-use Channel Access Package. All the information required to compile and run the program should be in the comments:
    
            /* ezcaTest.c - a test EZCA program
            **
            ** To compile:
            **      cc -o ezcaTest \
            **         -I/devl/epics/base/include \
            **         -I/devl/epics/extensions/src/ezca/ \
            **         -L/devl/epics/extensions/lib/Linux/ \
            **         -Wl,-rpath,/devl/epics/extensions/lib/Linux/ \
            **         -lezca ezcaTest.c
            ** To run:
            **      excas &              # Get the "fred" record defined
            **      ca_put fred 123.45   # Set "fred" to a value
            **      ./ezcaTest           # Run this test program
            **      0 123.456779         # The result
            */
    
            /* EZCA include files */ 
    
            #include  
            #include  
            #include 
    
              int main () {
    
                float  d;
                int    status;
    
                status = ezcaGet ("fred", ezcaFloat, 1, &d);
                printf ("%d %f\n", status, d);
    
              }
    
        

Fortran


 

Linux

  • Printing out a man Page
    • The standard man pages are stored in gzip formatted files under directory /usr/man/man<n>, where <n> is the manual page section number.

      As an example, the following command will generate a file in PostScript format of the bash manual page, which is in Section 1 of the man pages:

              gunzip -c /usr/man/man1/bash.1.gz   |   \
                    groff -Tps -man   >   bash.ps



mail

  • Sending email from the SLSnet
  • The following command allows one to send email from the SLSnet, for example, and make it look as though it came from you:

      cat <file>   |   mail   -s "<subject>"   <to>   ...   --   -f   <from>
    Example:
      vi   /tmp/mail.txt
      cat /tmp/mail.txt   |   mail   -s "A test email"   sls-controls@psi.ch \
              --   -f   david.maden@psi.ch

medm


 

MSwindows

  • To access (map) the SLS linux /exchange/home directory from an MSwindows system on the SLS beamline network:
    • Start Windows Explorer and select Tools --> Map Network Drive ...
    • Select a free Drive: from the drop-down menu.
    • For the Folder: specify:
        \\slsfs01\exchange
    • Select Connect using a different user name.
    • For the User name: specify:
        PSICH\<name>
      and enter the corresponding password for the MSwindows PSICH domain.
    • Click OK and then Finish.
    The /exchange/home directory should then be accessible with write access to the /exchange/home/<name> sub-directory.

  • To access (map) the SLS linux /exchange/home directory from the cygwin bash shell of an MSwindows system on the SLS beamline network, the command is of the form:
      net   use   m:   \\\\slsfc01\exchange   <password>   /user:PSICH\\<name>
    where M: is a free device name. The /exchange/home directory should then be accessible via /cygdrive/m/ with write access to /cygdrive/m/<name>.

  • To access (map) an <e-account> directory from an MSwindows system connected to either the SLS beamline network or to the PSI network:
    • Start Windows Explorer and select Tools --> Map Network Drive ...
    • Select a free Drive: from the drop-down menu.
    • For the Folder: specify, for example:
        \\x04sa\e10041
    • Select Connect using a different user name.
    • For the User name: specify:
        <e-account>
      and enter the corresponding password.
    • Click OK and then Finish.
    The home directory of the <e-account> should then be accessible with write access.

  • To access (map) an <e-account> directory, e10041 say, from the cygwin bash shell via a given drive name, M: say, the command is of the form:
      net   use   m:   \\\\x04sa\e10041   <password>   /user:e10041
    The home directory of the <e-account> should then be accessible with write access as:
      /cygdrive/m/


  • To unmap the M: drive, say, from the cygwin bash shell, the command is of the form:
      net   use   m:   /delete

Perl


 

PostScript and PDF

  • To concatenate PostScript or PDF files into a single PDF file:

         gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite \
              -sOutputFile=<outfile> <infile> <infile> ...


  • To concatenate PostScript or PDF files into a single PostScript file:

         gs -q -dNOPAUSE -dBATCH -sDEVICE=pswrite \
              -sOutputFile=<outfile> <infile> <infile> ...



Python


 

Tcl/Tk


 

vxWorks


 

Author: David Maden   Phone: +41 56 310 3616   Email: david.maden@psi.ch   Updated: 19.6.2007   Source: tips_n_tricks.html,v 1.30