
a278343272d6570ef99a01224674ed32.ppt
- Количество слайдов: 44
CS 465 – Unix The Korn Shell (ksh)
ksh Shell • The Korn (ksh) shell – Scripting syntax is compatible with the standard Bourne (sh) shell – Included in Unix operating systems from most vendors (Sun Solaris, SGI IRIX, Linux, etc. ) – Provides some extra features: • Command aliasing • Easier user input and math calculations • Command history • Command-line editing
Command Aliases allow you to define your own commands • Format: $ alias [-x] name=definition • Examples: $ $ alias ll="ls -la" dir="ls -F" –x home="cd; ls" –x rm="rm -i" Using the –x option “exports” the alias.
Command Aliases • To remove aliases: $ unalias name • To show all aliases: $ alias • If you put the alias commands in your. profile or. kshrc file, you can use them every time you login.
Displaying Alias Values • To determine which command a specific alias will run, use either alias OR whence: $ alias ll="ls -la" $ alias ll ll='ls –la' $ whence ll ls –la $
The Dash (-) Character • Dash (-) represents the previous working directory. When used to cd, it automatically displays the current directory path. • Example: Switching between one of your subdirectories and the system bin directory: $ pwd /home/user 1/sub 1 $ cd /bin $ pwd /bin $ cd /home/user 1/sub 1 $ cd /bin $
The let command • let provides built-in integer handling that easier to use and 10 -30 times faster than expr • Syntax: $ let math-expression NOTE: Use = to assign values • Examples $ let i=i+1 $ let "prod = 12 * 6" Double quotes allow spaces and no backslash on *
The let command Can use (( instead of let )): x=3 y=4 ((z = x * y)) Result: z = 12 ((z = z + 1)) Result: z = 13 Notes: - No dollar sign ($) needed to access variable values - Double parentheses act as quotes, so you can add spaces and don’t need to backslash metacharacters
let Operators – Integer operators: +, -, *, /, % – Integer comparisons: <, <=, ==, !=, > – Compounds && (and), || (or), ! (not) NOTE: No backslashes needed inside (( ))
Bourne vs Korn Comparison • Bourne Shell result=`expr calculation` [ $num -ge 0 ] [ $num 1 -gt 0 -a $num 2 -lt 100 ] • Korn Shell (( result = calculation )) (( num >= 0 )) (( num 1 > 0 && num 2 < 100 ))
let example i=1 total=0 while (( i <= 100 )) do (( total = total + i )) (( i = i + 1 )) done echo $total
Korn Additional test Operators • The Korn shell also extends the test expression from the Bourne shell. • By using two sets of square brackets, instead of one set, the system uses the Korn shell (instead of Bourne) to test specific conditions. [[ test-condition ]]
Korn Additional test Operators File Operators: -a -L f 1 f 1 file -ef f 2 –nt f 2 –ot f 2 file exists and is a symbolic link file 1 and file 2 are linked file 1 is newer than file 2 file 1 is older than file 2 Logical operators: && || logical AND logical OR
Korn test example $ cat lsdir #! /bin/ksh # lists directory contents if yours if [[ -d $1 && -O $1 ]] then ls -l $1 else echo Error - not a directory or not yours! fi exit $
Command History • The Korn shells supports a history feature that lets you recall previous commands, performing a substitution if necessary. • The history command displays the previous 16 commands: $ history • Common Alias: $ alias h="history"
r (recall/rerun command) alone repeats the last command. repeats command number 11 repeats the command before the last one. r d repeats the last command that started with a “d”. r sort one=two repeats previous sort command using two instead one. r r 11 r -2
History Variables • HISTFILE contains name of your history file HISTFILE=$HOME/. myhist • If you do not provide a name, then the Shell uses: $HOME/. sh_history • HISTSIZE contains how many commands to save – Default (to save) is 128 (but show only last 16) HISTSIZE=50
In-Line Command Editing • You can perform vi in-line editing of the command line • In-line edit mode enables you to edit a previous command on the current command line • Use vi commands to move and edit the previous command line
Turning On/Off Editing $ set [-+]o vi set -o vi turns command-line editing on set +o vi turns it off Once turned on, ESC key activates the in-line editor To use: Press the ESC key to activate in-line editing. Press – (or k) until the desired command appears. Edit using vi commands, then press ENTER.
Using Command Line Editing • Your command line now becomes a single line editor window into the command history file. • The single line you start viewing is the current Shell command line. • You can move to other lines by using the editor move commands (- moves backwards, + moves forwards). • Editor search commands can also be used to select the line being viewed.
Some vi for Command Line Editing
Using Filename Completion • Automated completion of a filename used as an argument to a command. • To initiate, use the
Filename Completion Choices • If the filename is NOT unique, you can get a list of choices by using:
Additional Korn Shell Variables $PPID the shell's parent process' PID $_ last parameter of previous command $RANDOM randomly generated integer (0 -Max) $ENV pathname of Korn shell environment startup file $OLDPWD working directory set before current one $EDITOR pathname of editor to use for line editing $PS 3 prompt for select loops (default is #? ) $TMOUT number of seconds to wait before exiting shell if no command is given
OLDPWD example $ pwd /home/smith 123/cprogs $ cd /etc $ pwd /etc $ cd $OLDPWD $ pwd /home/smith 123/cprogs
New Pattern Matching ~ home directory (equivalent to $HOME) ~username’s home ~+ current working directory (equivalent to $PWD) ~- previous working directory (equivalent to $OLDPWD)
Using Tilde Substitution $ pwd /home/smith 123/progs/cprogs $ cd $ pwd /home/smith 123 $ cd ~$ pwd /home/smith 123/progs/cprogs $ cd ~jones 456 $ pwd /home/jones 456
Reading User Input • Korn shell provides one command that will BOTH “echo” and “read”: • Syntax: $ read 'varname? prompt' • Examples: $ read 'name? Enter your name: ' $ read 'year? Current year? '
read Example $ cat mul read 'num 1? Enter a number: ' read 'num 2? Enter another number: ' (( prod = num 1 * num 2 )) echo $num 1 times $num 2 is $prod $ mul Enter a number: 5 Enter another number: 8 5 times 8 is 40 $
until statement until [ condition ] do command(s) done • Same condition syntax as if statement • Begin and end of command block defined by keywords do…done • Loops UNTIL condition is TRUE
until Example Read in a number from a user, and verify the number is positive. $ cat posnum #! /bin/sh # Read positive number from user num=0 until [ num -gt 0 ] do echo Enter a positive non-zero number: read num done echo You entered $num exit 0 $
until Example Read in a number from a user, and verify the number is positive. $ cat posnum #! /bin/sh # Read positive number from user num=0 until (( num > 0 )) do echo Enter a positive non-zero number: read num done echo You entered $num exit 0 $
until Example Execution $ posnum Enter a positive non-zero number: -50 Enter a positive non-zero number: 12 You entered 12 $
select statement select var in list do command(s) done • Implements a menu from within a loop. Automatically displays a numbered list of choices, and interpret the number that the user enters. • Begin and end of command block defined by keywords do…done
select statement • You must still use the case control structure to evaluate the choice chosen, but the structure will loop automatically unless you have chosen to exit. • If your menu options consist of multiple words, they must be enclosed in double quotes.
select example $ cat junkit #!/bin/ksh # Menu-driven junkit script junk=$HOME/junkdir # # If junkdir directory doesn't exist, create it if [[ ! (-d $junk) ]] then 'mkdir' $junk fi # select choice in "List junk" "Delete junk" "Junk files" "Exit" do case $choice in "List junk") ls -lg. F $junk; ;
select example "Delete junk") rm $junk/*; ; "Junk files") read 'filelist? Enter files to junk: ' mv $filelist $junk; ; "Exit") break; ; *) echo Invalid choice; please try again; ; esac done exit 0 $ NOTE: The break command is used to exit the loop.
select execution $ pwd /export/home/jmsmith $ junkdir 1) List junk 2) Delete junk 3) Junk files 4) Exit #? 1 total 0 #? 3 Enter files to junk: p 2. c #? 1 total 2 -rw------1 jwsmith 122 May #? 2 #? 1 total 0 #? 4 $ 7 13: 19 p 2. c
Other Korn Additions: String Length • Length of string: ${#varname} – Returns length of string stored in variable • Example: $ cat namelen name="Pam Smallwood" echo ${#name} $ namelen 13 $
Other Korn Additions • Forcing Command Execution – In addition to the back quotes (graves) `command` – In Korn you can use: $(command) • Example: $ name=$(whoami) $ echo $name small 000
Sample. kshrc file # Set command aliases alias rm='rm -i ' alias rename='mv ' alias c clear # Set environment variables PATH=$PATH: . PS 1="$PWD[!] $ " EDITOR=vi # Export global variables export PATH EDITOR PS 1 # Set history variables HISTSIZE=40
Default. profile • Review handout
Capturing a Terminal Session • You can capture what your terminal display to a file using the command script • Syntax: $ script filename • Everything that appears on your screen will be captured in the file, until you enter
Terminal Session Example $ script session 1 Script started, file is session 1 $ pwd /export/home/small 000 $ date Thu May 22 19: 34: 25 MDT 2003 $