Commit ef8d0d97 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

\ No newline at end of file
## Example files for the title:
# Learning the Korn Shell 2nd Edition, by Arnold Robbins
[![Learning the Korn Shell 2nd Edition, by Arnold Robbins](](
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <>.
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.
# Tksh Demo
# Jeff Korn
# This script keeps track of visited directories and shows the files
# in the current directory. You can double-click on files and
# directories. The script should be used interactively, so to run:
# $ tksh
# $ . scripts/watchdir
function winsetup
pack $(frame .f)
frame .f.dirname -relief raised -bd 1
pack .f.dirname -side top -fill x
pack $(frame $(frame .f.dirs) -side left
label .f.dirname.label -text "Current directory: "
label .f.dirname.pwd -textvariable PWD
pack .f.dirname.label .f.dirname.pwd -side left
scrollbar -command " yview"
listbox -yscroll " set" -width 20 -setgrid 1
pack $(label -text "Directory Contents") -side top
pack -side left -fill y -expand 1
scrollbar .f.dirs.scroll -command ".f.dirs.list yview"
listbox .f.dirs.list -yscroll ".f.dirs.scroll set" -width 20 -setgrid 1
pack $(label .f.dirs.label -text "Visited Directories") -side top
pack .f.dirs.list .f.dirs.scroll -side left -fill y -expand 1
bind .f.dirs.list "<Double-1>" 'cd $(selection get)'
bind "<Double-1>" 'tkfileselect $(selection get)'
function tkfileselect
[[ -d "$1" ]] && tkcd "$1"
[[ -f "$1" ]] && ${EDITOR-${VISUAL-emacs}} "$1"
function tkcd
cd $1 > /dev/null || return delete 0 end
set -o markdirs insert end .. *
[[ ${VisitedDir["$PWD"]} == "" ]] && .f.dirs.list insert end "$PWD"
typeset -A VisitedDir
winsetup > /dev/null
alias cd=tkcd
tkcd .
#! /bin/ksh
# usage: phaser4 [ options ] files
# -k, --kill use kill setting (default)
# -l n, --level n set phaser level (default = 2)
# -s, --stun use stun-only setting
# -t [lf], --tricorder [lf] tricorder mode, opt. scan for life form lf
USAGE=$'[-?\n@(#)$Id: phaser4 (Starfleet Research and Development)'
USAGE+=$' Stardate 57234.22 $\n]'
USAGE+="[-author?J. Programmer <>]"
USAGE+="[-copyright?Copyright (c) Stardate 57000 Starfleet.]"
USAGE+="[+NAME?phaser4 --- combined phaser and tricorder]"
USAGE+="[+DESCRIPTION?The \aphaser4\a program combines the operation "
USAGE+="of the \aphaser3\a and \atricorder\a programs in one handy tool.]"
USAGE+="[k:kill?Use kill setting (default).]"
USAGE+="[l:lev*el]#[level:=2?Set the phaser level.]{
[0-2?non-lethal settings]
[3-10?lethal, use with caution]
USAGE+="[t:tricorder?Tricorder mode.]:?[life_form]"
USAGE+=$'\n\nfile ...\n\n'
USAGE+=$'[+SEE ALSO?\aphaser3\a(1), \atricorder\a(1)]'
kill=1 stun=0 level=2 # defaults
tricorder=0 phaser=1
while getopts "$USAGE" optchar ; do
case $optchar in
k) kill=1 stun=0 ;;
s) kill=0 stun=1 ;;
l) level=$OPTARG
if ((level < 0)) ; then level=0 ; fi
if ((level > 10)) ; then level=10 ; fi
t) phaser=0 tricorder=1
print kill=$kill
print stun=$stun
print level=$level
print phaser=$phaser
print tricorder=$tricorder
print life_form=$life_form
cut -d: -f1 < /etc/passwd | sort | lp
cut -d: -f1 < /etc/passwd | sort
function testopt {
if [[ -o $1 ]] ; then
print Option $1 is on.
print Option $1 is off.
Coltrane, John|Giant Steps|Atlantic|1960|Ja
Coltrane, John|Coltrane Jazz|Atlantic|1960|Ja
Coltrane, John|My Favorite Things|Atlantic|1961|Ja
Coltrane, John|Coltrane Plays the Blues|Atlantic|1961|Ja
function testme {
countargs "$@" # Show the original number of parameters
set a b c # Now change them
countargs "$@" # Print the new count
testme 1 2 3 4 5 6 # Run the function
countargs "$@" # No change to invoking shell's parameters
person="John Q. Public"
# alternate way:
# person=(firstname=John initial=Q. lastname=Public)
print $person # Simple print
# ( lastname=Public initial=Q. firstname=John )
print -r "$person" # Print in full glory
# (
# lastname=Public
# initial=Q.
# firstname=John
# )
print ${person.initial} # Print just the middle initial
function countargs {
print "$# args."
# highest filename [howmany]
# Print howmany highest-numbered lines in file filename.
# The input file is assumed to have lines that start with
# numbers. Default for howmany is 10.
sort -nr "$filename" | head -$howmany
function lsd {
ls -l | grep -i "^.\{41\}$date" | cut -c55-
name="bill" # Set initial value
nameref firstname=name # Set up the nameref
print $firstname # Actually references variable name
# bill
firstname="arnold" # Now change the indirect reference
print $name # Shazzam! Original variable is changed
# arnold
# To find out the name of the real variable being referenced by the nameref,
# use ${!variable}:
print ${!firstname}
date # Current day and time
# Wed May 23 17:49:44 IDT 2001
function getday { # Define a function
typeset -n day=$1 # Set up the nameref
day=$(date | awk '{ print $1 }') # Actually change it
today=now # Set initial value
getday today # Run the function
print $today # Display new value
function demo { # Define a Korn shell function
typeset myvar=3 # Set a local variable myvar
print "demo: myvar is $myvar"
myvar=4 # Set the global myvar
demo ; print "global: myvar is $myvar" # Run the function
# demo: myvar is 3
# global: myvar is 4
. demo # Run with POSIX semantics
# demo: myvar is 3
print "global: myvar is $myvar" # See the results
# global: myvar is 3
function dirs { # print directory stack (easy)
function pushd { # push current directory onto stack
cd ${dirname:?"missing directory name."}
print "$DIRSTACK"
function popd { # cd to top, pop it off stack
top=${DIRSTACK%% *}
cd $top
print "$PWD"
function afunc {
print in function $0: $1 $2
var1="in function"
var1="outside of function"
print var1: $var1
print $0: $1 $2
afunc funcarg1 funcarg2
print var1: $var1
print $0: $1 $2
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment