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.
|
Comments (5)
Dec 28, 2007
WPollock says:
Nice! I have two comments: Your createLockFile routine uses "ln -s" to creat...Nice! I have two comments:
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.
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 doneDec 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 /] #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.
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.
Aug 04, 2008
bnsmb says:
Jim, Nice job, very complete, but a lot of lines before starting the job. Yes, ...Jim,
Yes, that's a little confusing. I'm thinking about a solution for some time now but did not find an optimal solution.
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