A Script Template and Useful Techniques for ksh Scripts

A Script Template and Useful Techniques for ksh Scripts

November, 2007

This article discusses a template for Korn shell (ksh) scripts and explains the techniques used in the template.

Read Full Article

Community-Submitted Article


How to Leave Comments or Tag Pages

1. Register.
2. Log in. Use the link at top right, just below the Search box.
3. To leave comments on this page, select "Add Comment" on the lower left hand corner of this page. Additional instructions here.
4. To tag pages using labels, read these instructions.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Dec 28, 2007

    WPollock says:

    Nice! I have two comments: Your createLockFile routine uses "ln -s" to creat...

    Nice! I have two comments:

    1. Your createLockFile routine uses "ln -s" to create the lockfile. While it is common practice to do that, as far as I can tell from checking the POSIX docs at OpenGroup.org this is not guaranteed to create a file atomically.
      Dave Korn taught me the correct way to do this:
      set -C  # or: set -o noclobber
      : > ${__LOCKFILE} 2>/dev/null
      LN_RC=$?
      

      With noclobber set, creating a file via the shell's I/O redirect mechanism is guaranteed to atomically create the file if it doesn't already exist.

    2. This same idiom can be used to create temporary files. It turns out that using "mktemp" is the best way; however that isn't (yet!) part of POSIX and you didn't use it. None the less you might wish to "improve" your temp file creation routine:
      • To make the name highly unpredictable, append a random number to the file name. Use the ${RANDOM} shell variable as part of the name. (Unfortunately, while ${RANDOM} is available in KSH and most modern shells, it isn't part of POSIX.) This is a security feature to prevent a DoS attack by guessing your temp file names.
      • Use ${TMPDIR} if set as the location for temporary files.
      • A POSIX-ly correct replacement for mktemp is possible. I wrote this awhile back, feel free to adapt it for your template:
        set -C  # turn on noclobber shell option
        # AWK is the only standard utility that provides random numbers:
        rand()
        {
            awk 'BEGIN { srand(); printf "%d\n", (rand() * 10^8); }'
        }
        umask 177
        NAME="$1"
        NAME="${NAME:=tmp}"
        while :
        do
           TMP=${TMPDIR:-/tmp}/${NAME}-$$.$(rand)
           : > ${TMP} && break
        done
        
  2. Dec 29, 2007

    bnsmb says:

    Thanks for the info! I'll add the code the script template. One thing I foun...

    Thanks for the info! I'll add the code the script template. One thing I found is that the awk in Solaris (snv_78) does not handle the awk code from your example but nawk does

    [Sat Dec 29 13:13:40 root@sol9 /]
    # uname -a
    SunOS sol9 5.11 snv_78 sun4u sparc sun4u
    
    [Sat Dec 29 13:13:43 root@sol9 /]
    # awk 'BEGIN { srand(); printf "%d\n", (rand() * 10^8); }'
    awk: syntax error near line 1
    awk: illegal statement near line 1
    awk: syntax error near line 1
    awk: illegal statement near line 1
    
    [Sat Dec 29 13:14:00 root@sol9 /]
    # nawk 'BEGIN { srand(); printf "%d\n", (rand() * 10^8); }'
    75591296
    
    [Sat Dec 29 13:14:02 root@sol9 /]
    #
    
    
    1. Dec 31, 2007

      WPollock says:

      It should work, it only uses standard POSIX awk features. If not on Solaris, yo...

      It should work, it only uses standard POSIX awk features. If not on Solaris, you can't have your template pure POSIX anyway, and you might as well use mktemp instead.

  3. Aug 04, 2008

    Jim.L says:

    Nice job, very complete, but a lot of lines before starting the job. Maybe, yo...

    Nice job, very complete, but a lot of lines before starting the job.

    Maybe, you should use the FPATH environment variable in order to create and use repositories of common functions. That variable is avalaible on AIX-ksh and Solaris-ksh. Use it as PATH variable but only put directories filled with files containing one and only one function of the same name as the file. In that manner you can create "libraries" of scripted functions that you can use as if they were native to the shell within the script you are editing.

    1. Aug 04, 2008

      bnsmb says:

      Jim, Nice job, very complete, but a lot of lines before starting the job. Yes, ...

      Jim,

      Nice job, very complete, but a lot of lines before starting the job.

      Yes, that's a little confusing. I'm thinking about a solution for some time now but did not find an optimal solution.

      Maybe, you should use the FPATH environment variable in order to create and use repositories of common functions. That variable is avalaible on AIX-ksh and Solaris-ksh. Use it as PATH variable but only put directories filled with files containing one and only one function of the same name as the file. In that manner you can create "libraries" of scripted functions that you can use as if they were native to the shell within the script you are editing.

      The goal of the template is to create scripts which run out of the box on any Solaris box without additional tools or configurations. If I move functions to an FPATH environment that's not the case anymore - the script will only work on machines where the FPATH environment is setup.

      Bernd

Sign up or Log in to add a comment or watch this page.


The individuals who post here are part of the extended Sun Microsystems community and they might not be employed or in any way formally affiliated with Sun Microsystems. The opinions expressed here are their own, are not necessarily reviewed in advance by anyone but the individual authors, and neither Sun nor any other party necessarily agrees with them.

© 2010, Oracle Corporation and/or its affiliates
Powered by Atlassian Confluence
Oracle Social Media Participation Policy Privacy Policy Terms of Use Trademarks Site Map Employment Investor Relations Contact