960 likes | 1.26k Views
Korn Shell Programming. by Chris Seddon. Korn Shell Programming. 1. Simple Scripts 2. Data Types 3. Patterns 4. Conditionals 5. Loops 6. Select 7. Parameters 8. Further Examples 9. Functions. 1. Simple Scripts. 1. echo. #! /bin/ksh # clear screen clear
E N D
Korn Shell Programming byChris Seddon
Korn ShellProgramming • 1. Simple Scripts • 2. Data Types • 3. Patterns • 4. Conditionals • 5. Loops • 6. Select • 7. Parameters • 8. Further Examples • 9. Functions
echo #! /bin/ksh # clear screen clear # print titles echo "===============================" echo "" echo "This is your first Korn shell script" echo "" echo "===============================" echo "" # print information echo "Current directory is: " pwd echo "Today's date and time: " date echo "" echo ""
print #! /bin/ksh # clear screen clear # print titles print "===============================" print "" print "This is your second Korn shell script" print "" print "===============================" print "" # print information print ‑n "Current directory is: " pwd print ‑n "Today's date and time: " date print "" print ""
Shell Variables • Shell variables are local • not exported to sub-shell variable = value variable=$(command) myVar=42 myVar=$(ls -1) echo $myVar
Shell Variables #! /bin/ksh # clear screen clear # print titles print "================================" print "" print "This is your third Korn shell script" print "" print "================================" print "" # gather information currentDirectory=$(pwd) dateAndTime=$(date) # print information print "Current directory is: $currentDirectory" print "Today's date and time: $dateAndTime" print "" print ""
read ... #! /bin/ksh # prompt user print ‑n "Please enter your first name: \a" read firstName print ‑n "Please enter your last name: \a" read lastName # display message print "\n\n" print "Your name is: $firstName $lastName" print "\n\n"
... read #! /bin/ksh # prompt user print ‑n "Please enter your first and last names: \a" read firstName lastName # display message print "\n\n" print "Your name is: $lastName, $firstName" print "\n\n"
... read #! /bin/ksh # prompt user read fullName?"Please enter your first and last names: " # display message print "\n\n" print "Your name is: $fullName" print "\n\n"
Data Types Integers Strings Arrays typeset -i typeset -u typeset -l typeset -L typeset -R set -A integer upper case string lower case string left justified string right justified string array
Integers #! /bin/ksh # declare integers typeset ‑i x y typeset ‑i sum product difference # read data print ‑n "Enter two integers: " read x y # perform calculations (( sum = x + y )) (( product = x * y )) (( difference = x ‑ y )) # print results print "$x + $y = $sum" print "$x * $y = $product" print "$x ‑ $y = $difference"
Strings #! /bin/ksh # declare integers typeset ‑u upperCaseString typeset ‑l lowerCaseString typeset ‑L10 leftJustifiedString typeset ‑R10 rightJustifiedString typeset ‑i lengthOfString # read data print ‑n "Enter a string: " read theString ... ... # perform calculations upperCaseString=$theString lowerCaseString=$theString leftJustifiedString=$theString rightJustifiedString=$theString (( lengthOfString = ${#theString} )) # print results print "upper case string = $upperCaseString" print "lower case string = $lowerCaseString" print "left justified string = $leftJustifiedString" print "right justified string = $rightJustifiedString" print "length of string = $lengthOfString"
Arrays #! /bin/ksh # declare array of strings set ‑A weekDay Monday Tuesday Wednesday Thursday Friday print "3rd weekday is ${weekDay[2]}" # declare array of integers typeset ‑i number set ‑A number 5 10 15 20 25 30 print "3rd number is ${number[2]}"
Reading an Array #! /bin/ksh # set up an array using keyboard input read aLineFromTheKeyboard?"Please enter a line of text: " set ‑A word $aLineFromTheKeyboard print "word 3 is ${word[2]}" print "word 1 is ${word[0]}"
Constants #! /bin/ksh # declare integers typeset ‑ir interestRate=15 typeset ‑i balance interest # read in data etc read balance?"What is the balance in your account? " (( interest = balance * interestRate / 100 )) print "interest on $balance at $interestRate% is $interest"
Patterns • Objective • Get an overview of C++ • Contents • History • What is C++? • Compilers • Development environments • Third party software 3
pattern meaning * ? [abc] [a-z] any string any character a or b or c any of a thru z Patterns
| OR Pattern Lists a* | b* | c* | d* a??? | b??? | c??? a* | b???
Composite Patterns pattern meaning ?(pattern-list) *(pattern-list) +(pattern-list) @(pattern-list) |(pattern-list) 0 or 1 0+ 1+ 1 NOT
ls with Patterns #! /bin/ksh # change directory to sub directory called patterns clear cd patterns # use wildcards to select specific filenames ls f* ls f????? ls [c‑e]* ls !([c‑e]*) ls b*(ck*|sh*) ls b**(ck*|sh*)
String Patterns pattern meaning # % delete BEGINNING of string delete END of string # or % ## or %% delete SMALLEST match delete LARGEST match ${variable#pattern} ${variable##pattern} ${variable%pattern}
Editing Shell Variables #! /bin/ksh # change directory and record new directory in shell variable clear cd /usr/openwin/bin directory=$(pwd) # use wildcards to select substrings of directory name print $directory print ${directory%/*} print ${directory##*/}
if then if then else if <condition> then <command> <command> fi if <condition> then <command> <command> else <command> <command> fi If Statements ...
if then elif else if <condition> then <command> <command> elif <command> <command> elif <command> <command> else <command> <command> fi ... If Statements
pattern meaning [[ -d file ]] [[ -d file ]] [[ -d file ]] [[ -d file ]] [[ -n file ]] [[ -z file ]] [[ string = pattern ]] [[ string != pattern ]] [[ number1 -eq number2 ]] [[ number1 -ne number2 ]] [[ number1 -lt number2 ]] [[ number1 -gt number2 ]] [[ number1 -le number2 ]] [[ number1 -ge number2 ]] directory normal file non empty file writable file string is not empty string is empty string matches pattern string doesn't match pattern equal not equal less than greater than less than or equal greater than or equal Conditions
pattern meaning ! condition condition1 && condition2 condition1 || condition2 NOT AND OR Grouping Conditions (cond-1 || cond-2) && cond-3
Backup a File ... #! /bin/ksh # get name of source file read sourceFile?"Enter name of file to be copied: " # generate name of duplicate file duplicateFile=$sourceFile.copy # use the cp command to make the copy cp $sourceFile $duplicateFile
... Backup a File #! /bin/ksh # get name of source file read sourceFile?"Enter name of file to be copied: " # make sure source file exists if [[ ! ‑f $sourceFile ]] then print "Error: $sourceFile does not exist" print "exiting ..." exit 1 fi # generate name of duplicate file duplicateFile=$sourceFile.copy # use the cp command to make the copy cp $sourceFile $duplicateFile
Case Statements ... • Multiway if statements case $variable in pattern-1) cmd; cmd ;; pattern-2) cmd; cmd ;; pattern-3) cmd; cmd ;; esac
... Case Statement case $name in [Jj]ohn) echo "Hi John" ;; [Pp]eter) echo "Hi Peter" ;; [Mm]ary) echo "Hi Mary ;; *) echo "who are you" ;; esac
... Case Statements #! /bin/ksh read theString?"Type one word and hit return: " case $theString in +([0‑9])) print "$theString is an integer" ;; +([a‑zA‑Z])) print "$theString is a pure string" ;; *) print "$theString is not an integer or a pure string" ;; esac
Loops 5
Loop Statements for name in john mary peter alice do echo $name done while [ $# -ne 0 ] do echo $1 shift done until [ $# -eq 0 ] do echo $1 shift done
Loop 10 Times #! /bin/ksh # declare integers typeset ‑i number square cube # initialise clear (( number = 0 )) # loop 10 times while (( number <= 10 )) do (( number = number + 1 )) # increment number (( square = number * number )) (( cube = number * number * number )) print "$number $square $cube" done