1 / 92

Korn Shell Programming

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

corina
Download Presentation

Korn Shell Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Korn Shell Programming byChris Seddon

  2. Korn ShellProgramming • 1. Simple Scripts • 2. Data Types • 3. Patterns • 4. Conditionals • 5. Loops • 6. Select • 7. Parameters • 8. Further Examples • 9. Functions

  3. 1

  4. Simple Scripts 1

  5. 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 ""

  6. 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 ""

  7. Shell Variables • Shell variables are local • not exported to sub-shell variable = value variable=$(command) myVar=42 myVar=$(ls -1) echo $myVar

  8. 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 ""

  9. 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"

  10. ... 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"

  11. ... 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"

  12. 2

  13. Data Types 2

  14. 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

  15. 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"

  16. 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"

  17. 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]}"

  18. 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]}"

  19. 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"

  20. 3

  21. Patterns • Objective • Get an overview of C++ • Contents • History • What is C++? • Compilers • Development environments • Third party software 3

  22. pattern meaning * ? [abc] [a-z] any string any character a or b or c any of a thru z Patterns

  23. | OR Pattern Lists a* | b* | c* | d* a??? | b??? | c??? a* | b???

  24. Composite Patterns pattern meaning ?(pattern-list) *(pattern-list) +(pattern-list) @(pattern-list) |(pattern-list) 0 or 1 0+ 1+ 1 NOT

  25. 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*)

  26. 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}

  27. 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##*/}

  28. 4

  29. Conditionals 4

  30. if then if then else if <condition> then <command> <command> fi if <condition> then <command> <command> else <command> <command> fi If Statements ...

  31. if then elif else if <condition> then <command> <command> elif <command> <command> elif <command> <command> else <command> <command> fi ... If Statements

  32. 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

  33. pattern meaning ! condition condition1 && condition2 condition1 || condition2 NOT AND OR Grouping Conditions (cond-1 || cond-2) && cond-3

  34. 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

  35. ... 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

  36. Case Statements ... • Multiway if statements case $variable in pattern-1) cmd; cmd ;; pattern-2) cmd; cmd ;; pattern-3) cmd; cmd ;; esac

  37. ... 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

  38. ... 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

  39. 5

  40. Loops 5

  41. 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

  42. 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

More Related