next up previous contents
Next: tcsh shell Up: Korn shell Previous: Korn shell scripts

Korn shell exec, read and miscellaneous commands

The exec command is used for redirection of file descriptors 0:9. Example:

> ksh
$ exec 1>std.out
$ dir ~/test
$ ....
$ exit
In the above example, all output directed to stdout from the command exec to the command exit is written on file std.out. To use file descriptor 5:

> cat kexec
# open file x.x by descriptor 5 with exec 5>x.x
exec 5>x.x
print -u5 "1. redirect print output to fd 5 opened by exec"
print -u5 "2. prinf on fd 5"
print -u5 "3. print on fd 5 again"
exec 5<&-
# close file x.x with exec 5<&-
echo "\n verify x.x by cat\n"
The kexec script listed above creates file x.x containing the script lines 1.-2.-3..
If the file descriptor is closed, any attempt to access closed file produces the following error message:
> kexec
kexec[13]: print: bad file unit number
exec may be used to execute a script but user is logged out exec is invoked when in parent shell.

The read command reads input from the terminal as single variables, piped names or file redirected with exec as summarized in the following example.

> cat kread
#!/bin/ksh
#-----------kread: read data with Korn shell---------------------
#
echo Proc $0: read command in Korn shell
echo
print "type a name> \c"                              # read var at promp
read name
print "typed name is: " $name
print "\npiped read example:"
print "apr may jun" | read a b c                     # pipe args
print arg1 from pipe is $a
print arg2 from pipe is $b
print arg3 from pipe is $c
print "\nread/write lines with exec redirection\n"
exec 0<$1                                            # redirect i/o
while read LINE
do
   print $LINE
done
#
#----------end script------------------

> kread
Proc kread: read command in Korn shell

type a name> any
typed name is:  any

piped read example:
arg1 from pipe is apr
arg2 from pipe is may
arg3 from pipe is jun

read/write lines with exec redirection

line 1
line 1
<ctrl>C
>


Korn Shell Read Options
-p read line form co-process
-r do not treat \ as continuation
-s save input in history file
-un read from file descriptor n



The prompt can be specified in the read statement:

$ read var?prompt
If var is not defined, input is assigned to variable REPLY. Field separator can be assigned with the IFS ( Internal Field Separator) variable.
Example:

> cat kpwd
#!/bin/ksh
#-----------kpwd: read example in Korn shell
echo Proc $0: type pwd info with Korn shell
echo
read ok?"Type pwd info? (y/n)"                  #read with prompt
[[ $ok = @([Nn])* ]] && exit 1                  #test read variable
echo pwd data are:
echo ""
IFS=:                                           #set IFS to :
exec 0</etc/passwd                              #redirect stdin to /etc/passwd
# list users
#
while read -r NAME PAS UID GID COM HOME SHELL
do
   print "acct= $NAME - home= $HOME - shell= $SHELL:"
done
#----------end script------------------
> kpwd

Type pwd info? (y/n)y
pwd data are:

acct= john - home= /home/john - shell= /bin/tcsh:
acct= mary - home= /home/mary - shell= /bin/tcsh:
acct= tester - home= /d4/check - shell= /bin/sh:
>
If you need to run a script in the current shell either use the dot [.] or the source command:

$ . .profile
$ source .profile
$ rehash
In this way any variable, alias or function setting stay in effect. Note the rehash command that recreates the in-memory shell tables and grants that the system aknowledges new .profile definitions.

Korn shell function statement enables the creation of new commands that can take arguments. Functions are defined as follows:
function f_name { commands } or f_name() { command } # Bourne
Functions are exported with typeset -fx f_name.


Korn Shell set options
-flag sets flag on
+flag sets flag off
flags can be used at shell invocation
-A unset and reassign array
-a export subsequent parameters
-b notify background job completion
-C set noclobber
-e execute error trap
-f disable file name generation
-h create tracked alias
-k put all parameters in environment
-m background job message
-n check syntax error of commands without execution
-o set/type on/off options (see man)
-p disable user profile and use /etc/suid_profile
-s sort positional parameters
-t exit at command end
-u cause error if unset parameter
-v print script lines (debug)
-x print commands and arguments (debug)
- unset x and v
- - do not change flags (see man)



Resource limit setting are nearly the same as Bourne shell with the following differences:
options
h, H, S are missing
Limits on virtual memory and swap space are settable if the system file
/usr/include/sys/resource.h defines:
RLIMIT_VMEM, option
-v limits Kbytes for virtual memory
RLIMT_SWAP , option
-w limits Kbytes for swap area.
An example of RLIMIT_VMEM definition is:

#define RLIMIT_AS       7               /* address space */
#define RLIMIT_VMEM     RLIMIT_AS       /* V.4 alias for AS */


Korn Shell Built-in Commands
:[arg ...]1 only expand arguments
. file [arg ...]1 read file and execute commands
alias2 print/set alias, -xt for export/tracked
bg put in background
break1 exit from loop
continue1 skip loop step
cd change directory (see man)
echo write to stdout
eval1 evaluate args and execute resulting command
exec1 execute command in this shell
exit1 exit from shell
export2 export vars or type if -p option
fc edit command (see man)
fg bring in foregroud
getopts check arguments for legal options
hash rehash variable or empty list if -r option
jobs list jobs
kill kill jobs (see man)
let assign arithmetic vars
newgrp change primary group
print print command (see man)
pwd print current directory
read get input
readonly2 set var as readonly
return1 return status
set assign vars depending on option
shift1 shift parameters (see man)
times1 print used time
trap1 execute on signal (see man)
typeset1 set shell parameters
ulimit set resource limits
umask set default protection (see man)
unalias delete named alias or all if -a
unset deassign vars
wait wait job termination
whence explain command, -pv for more specifications
inlib obsolete (see man)
rmlib obsolete (see man)
1, 2 commands 1, 2 are treated specially (see man)


In ksh commands marked 1 and 2 are treated as follows:


next up previous contents
Next: tcsh shell Up: Korn shell Previous: Korn shell scripts
Marisa Luvisetto
2001-02-05