1 / 23

CSCI 330 The UNIX System

CSCI 330 The UNIX System. Unit VIII: Introduction to Shell Programming. Introduction to Shell Programming. shell programming is one of the most powerful features on any UNIX system large portion on UNIX administration and house keeping is done via shell scripts

nia
Download Presentation

CSCI 330 The UNIX System

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. CSCI 330The UNIX System Unit VIII: Introduction to Shell Programming

  2. Introduction to Shell Programming • shell programming is one of the most powerful features on any UNIX system • large portion on UNIX administration and house keeping is done via shell scripts • if you cannot find an existing utility to accomplish a task, you can build one using a shell script CSCI 330 - The Unix System

  3. Shell Programs • A shell program contains high-level programming language features: • Variables for storing data • Decision-making control (e.g. if and case statements) • Looping abilities (e.g. for and while loops) • Function calls for modularity • A shell program can also contain: • any UNIX command • file manipulation: cp, mv, ls, cd, … • utilities: grep, sed, awk, … CSCI 330 - The Unix System

  4. Shell Programs: tips • Naming of shell programs and their output • Give a meaningful name • example: findfile • Do not use: • script1, script2 • UNIX command names • Repository for shell programs • If you develop numerous shell programs, place them in a directory (e.g. ~/bin or ~/shellprogs) • Update your path to include the directory name where your shell programs are located CSCI 330 - The Unix System

  5. Shell Programs: tips • Comments • comment lines start with a pound sign (#) • Include comments to describe sections of your program: about one comment for every 5 lines • Help you understand your program when you look at it later • Formatting of shell programs • Indent areas (3 or 4 spaces) of programs to indicate that commands are part of a group • To break up long lines, place a \ at the end of one line and continue the command on the next line CSCI 330 - The Unix System

  6. Shell Programs: minimum required • create script that contains shell commands • data structure: variables • control structure: sequence, decision, loop • 1. line (shebang line) for bash shell script: #! /bin/bash #! /bin/sh • to run: • make executable: % chmod +x script • invoke via: % ./script CSCI 330 - The Unix System

  7. bash Shell Programming Features • Variables • Input/output • command line parameters • prompting user • Decision • if-then-else • case • Repetition • do-while, repeat-until • for, select • Functions • Traps CSCI 330 - The Unix System

  8. User-defined shell variables Syntax: varname=value Example: rate=moderate echo “Rate today is: $rate” • use double quotes if the value of a variable contains white spaces Example: name=“Thomas William Flowers” Note: no spaces CSCI 330 - The Unix System

  9. Output via echo command • Simplest form of writing to standard output Syntax: echo [-ne] argument[s] -n suppresses trailing newline -e enables escape sequences: \t horizontal tab \b backspace \a alert \n newline CSCI 330 - The Unix System

  10. Examples: shell scripts with output #! /bin/bash echo "You are running these processes:" ps #! /bin/bash echo -ne "Dear $USER:\nWhat's up this month:" cal CSCI 330 - The Unix System

  11. Command line arguments • Use arguments to modify script behavior • command line arguments become positional parameters to shell script • positional parameters are numbered variables: $1, $2, $3 … CSCI 330 - The Unix System

  12. Command line arguments Meaning $1 first parameter $2 second parameter ${10} 10th parameter { } prevents “$1” misunderstanding $0 name of the script $* all positional parameters $# the number of arguments CSCI 330 - The Unix System

  13. Example: Command Line Arguments #! /bin/bash # Usage: greetings name1 name2 echo $0 to you $1 $2 echo Today is `date` echo Good Bye $1 CSCI 330 - The Unix System

  14. Example: Command Line Arguments % chmod +x greetings % ./greetings Mark Flowers ./greetings to you Mark Flowers Today is Wed Oct 5 14:18:03 CST 2011 Good Bye Mark $0 => ./greetings $1 => Mark $2 => Flowers CSCI 330 - The Unix System

  15. Script example • Use command line argument as input for command #! /bin/bash # counts characters in command argument echo -n "$1" | wc -c CSCI 330 - The Unix System

  16. Numeric variables Syntax: let varname=value declare –ivarname=value • can be used for simple arithmetic: let count=1 let count=$count+20 let count+=1 • alternative syntax: ((count++)) CSCI 330 - The Unix System

  17. Array variables Syntax: varname=(list of words) • accessed via index: ${varname[index]} ${varname[0]} first word in array ${varname[*]} all words in array CSCI 330 - The Unix System

  18. Using array variables Examples: % ml=(maryannbrucelindadara) % echo $ml mary % echo ${ml[*]} maryannbrucelindadara % echo ${ml[2]} bruce % ml[2]=john % echo ${ml[*]} maryann john lindadara CSCI 330 - The Unix System

  19. Exporting Variables • Environment variable is created by exporting shell variable Syntax: export varname declare –x varname CSCI 330 - The Unix System

  20. Variables commands • To delete both local and environment variables unset varname • To prohibit change readonlyvarname • list all shell variables (including exported) set CSCI 330 - The Unix System

  21. variable manipulation • use portion of a variable’s value via: ${name:offset:length} • name – the name of the variable • offset – beginning position of the value • length – the number of positions of the value Example: % SSN="123456789" % password=${SSN:5:4} % echo $password % 6789 CSCI 330 - The Unix System

  22. Special variable uses • ${#variable} number of characters in variable’s value • ${variable:-value} if variable is undefined use “value” instead • ${variable:=value} if variable is undefined use “value” instead, and set variable’s value • ${varname:?message} if variable is undefined display error “message” CSCI 330 - The Unix System

  23. Summary • Shell scripts can do what can be done on command line • Shell scripts simplify recurring tasks • Next: control structures CSCI 330 - The Unix System

More Related