250 likes | 462 Views
Unix and Software Tools (P51UST) Shell Programming. Ruibin Bai (Room AB326) Division of Computer Science The University of Nottingham Ningbo, China. Introduction. Purposes Variants Variables Making shell script executable Input and output. What is a Shell Script?.
E N D
Unix and Software Tools (P51UST) Shell Programming Ruibin Bai (Room AB326) Division of Computer Science The University of Nottingham Ningbo, China P51UST: Unix and Software Tools
Introduction • Purposes • Variants • Variables • Making shell script executable • Input and output P51UST: Unix and Software Tools
What is a Shell Script? • The shell is a command interpreter • It reads commands and then executes them • It can work interactively or from a text file • A shell program is simply a text file which contains commands you would normally type at the prompt • It is normally executable P51UST: Unix and Software Tools
Why Shell Programming? • Unix runs Bourne shell scripts when it boots • Customised your boot-time behaviour • You can put commands in a file and execute them all at once. • Easy to make modifications (compared with command line inputs) P51UST: Unix and Software Tools
A Shell Script #!/bin/sh # A script to rename user folders cd ~/marks/ mv usr1 unnc-usr1 mv usr2 unnc-usr2 mv usr3 unnc-usr3 mv usr4 unnc-usr4 mv usr5 unnc-usr5 mv usr6 unnc-usr6 ... ... P51UST: Unix and Software Tools
Shell Variants • Bourne Shell (sh) • Traditional Unix shell from the every early days • C-Shell (csh) • Better interactive functionality than sh, more like Clanguage • Korn shell (ksh) • written by David Korn from Bell Labs • Bash (Bourne-Again Shell) • Additional functionality • We’ll use bash but in this lecturer you will learn about Bourne shell scripting • zsh and others • Find out your current shell $ echo $SHELL P51UST: Unix and Software Tools
Common Shell Script Components • The first line (for bourne shell) is usually #!/bin/sh • “#!” (shebang) forces the script to run in a particular shell. • NB. # is also used for comments P51UST: Unix and Software Tools
Invoking Shell • sh [option] arguments • ./arguments • /bin/sh ./arguments • You need to grant “execute” permission for the last two invoking methods P51UST: Unix and Software Tools
Writing and Running a Shell Script • Create the text file (using Emacs) • Make it executable (optional): chmod a+x filename P51UST: Unix and Software Tools
A Simple Shell Script #!/bin/sh # Name: myls.sh # Purpose: A very first shell example # Author: Ruibin Bai # Date: 2008-3-26 # Modified: ls –l echo "done " Each command appears on a separate line P51UST: Unix and Software Tools
Variables • Like java, sh allows you to have variables • But variables DO NOT need to be declared!!! • To set a variable, use var=value • To use the value of the variable, use $var or $(var) P51UST: Unix and Software Tools
Local vs Environment Variables • A sh variable can be either a local variable or an environment variable • Environment variables • Variables are passed to subprocesses • Used to store information used by the shell and by other programs • Conventionally written in all capitals • Local variables • Variables are valid within the current function. P51UST: Unix and Software Tools
Environment Variables • To list all the environment variables, use $ env • To find the value of a particular environment variable, use $ echo $var • Some of the environment variables • PATH - the directories the system searches to execute commands • TERM - The type of terminal (most commonly xterm and vt100) • HOME - Your home directory P51UST: Unix and Software Tools
Assigning Environment Variables at the Prompt • Varies from shell to shell • In bash: export VAR=value • To add something to your PATH: export PATH=$PATH:new stuff • To set your terminal type: export TERM=vt100 • NB. These changes are effective for the current windows only. P51UST: Unix and Software Tools
Your PATH Variable • Your PATH tells Unix where to look for commands and programs. Only programs in your path will be executed when you type their names • Each directory on your PATH should be separated by a colon “:” • If you change your PATH in one window it is changed only in that window • Be careful you don’t empty your PATH!!! • To check your path, use echo $PATH P51UST: Unix and Software Tools
Your PATH Variable • The order of the directories matters – Unix searches the directories one at a time, starting with the first one. As soon as the command is found the search stops and the command is executed. • The current directory “.” should always be near or at the end of your path. - why? • Suppose you type in a command name which happens to be the name of a program or a file in your current directory, you would run the wrong program. P51UST: Unix and Software Tools
Special Variables P51UST: Unix and Software Tools
Quoting String • Sometimes we need to quote strings so that they can passed through the shell. Three ways doing this: • Backslash “\” escapes the next character • Single quotes – strong quoting • Quotes everything except the single quote itself. • Double quotes – weak quoting • Does not expands most meta-characters like “*” or “?”, but does expand variables and does command substitution. P51UST: Unix and Software Tools
Quoting String - Backslash • Suppose the following commands $ ls files1 files2 $ echo Sure to remove these files? Sure to remove these files1 files2 • Why? • “?” is a shell meta-character, the shell look for all files that match the pattern “files?” • Use “\” to escape $ echo Sure to remove these files\? • Use “\\” for backslash itself. P51UST: Unix and Software Tools
Quoting String – Single Quotes • How would we pass the following string to the shell? What the is a $ and* doing here??? • Using single quotes echo ‘What the is a $ and* doing here???’ • How about single quote itself? P51UST: Unix and Software Tools
Quoting String – Double Quotes • Double quotes expand all variables and command substitutions and escape most meta-characters. E.g. $ echo "Is your home directory $HOME?“ Is your home directory /home/rzb? • Or $ echo "Your current directory is `pwd`" Your current directory is /home/rzb/ust P51UST: Unix and Software Tools
Input and Output • The first argument to a shell script is called $1 • The second argument to a shell script is called $2 • ….etc…until $9. • $0 stores script name • To print a string, Shell uses echo • like Java’s println • Option “-n” suppress carriage return. P51UST: Unix and Software Tools
Input and Output: An Example #!/bin/sh echo Hello $1 $ ./hello.sh Ruibin Hello Ruibin P51UST: Unix and Software Tools
read command takes a text string from standard input and assigns it to one or more variables. Syntax read var1 [var2…] The number of variables to which the string is assigned depends upon two things The number of words in the input string The number of variables associated with the read command. Input and Output: read P51UST: Unix and Software Tools
Read: example Example: #!/bin/sh echo -n "Please type in your first and last name: " read firstname lastname echo "Hello $firstname $lastname“ $ ./read.sh Please type in your first and last name: Ruibin Bai Hello Ruibin Bai P51UST: Unix and Software Tools