250 likes | 463 Views
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
E N D
CSCI 330The 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 • if you cannot find an existing utility to accomplish a task, you can build one using a shell script CSCI 330 - The Unix System
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Exporting Variables • Environment variable is created by exporting shell variable Syntax: export varname declare –x varname CSCI 330 - The Unix System
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
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
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
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