1 / 25

Unix and Software Tools (P51UST) Shell Programming

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

liana
Download Presentation

Unix and Software Tools (P51UST) 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. 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

  2. Introduction • Purposes • Variants • Variables • Making shell script executable • Input and output P51UST: Unix and Software Tools

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  17. Special Variables P51UST: Unix and Software Tools

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

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

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

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

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

  23. Input and Output: An Example #!/bin/sh echo Hello $1 $ ./hello.sh Ruibin Hello Ruibin P51UST: Unix and Software Tools

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

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

More Related