lecture 2: more unix, bash shell scripting programming tools and environments

Download Lecture 2: More UNIX, Bash Shell Scripting Programming Tools And Environments

Post on 23-Dec-2015




0 download

Embed Size (px)


  • Slide 1
  • Lecture 2: More UNIX, Bash Shell Scripting Programming Tools And Environments
  • Slide 2
  • Objective Objective: To introduce students to the concept of a shell, effective use of the shell, the shell as programming language, and shell scripts. We will use the bash shell for the examples and details provided. Shell syntax and commands history and command completion job control variables programming constructs scripts
  • Slide 3
  • What does the shell do? In Unix, separate from the OS (change look and feel) reads and executes commands some handled by the shell itself (pwd, echo,) some are programs stored in some directory (look in directories in PATH). Start a subshell to execute these Provides support for better interaction with the computer/OS (command history, editing, configuration) Supports scripting (is a programming language)
  • Slide 4
  • Executing a Command After reading a command, the shell may do some processing (see wildcards, etc in the syntax description that follows), then it must find a program to execute the command. Some commands are executed directly by the shell. Other commands are executed by separate programs. These are found by looking in a list of directories for programs with the appropriate name. The shell searches directories in the PATH variable. A hash table is used to make the search fast. You can add new commands simply by adding new programs (a program can be any executable file including scripts review Unix permissions) to directories in the PATH. You can modify/add directories in the PATH.
  • Slide 5
  • Finding out about Commands type tells you if a command is a built-in, an alias, or a program (external to the shell) which tells in which directory a utility is located help displays information about built-in commands (it is a builtin itself) info bash a good place to read about the BASH shell
  • Slide 6
  • Example [jjohnson@ws44 cs265]$ which echo /bin/echo [jjohnson@ws44 cs265]$ type -a echo echo is a shell builtin echo is /bin/echo [jjohnson@ws44 cs265]$ help echo # help provides information on builtin commands echo: echo [-neE] [arg...] Output the ARGs. If -n is specified, the trailing newline is suppressed. If the -e option is given, interpretation of the following backslash-escaped characters is turned on: \a alert (bell) \b backspace \c suppress trailing newline \E escape character \f form feed \n new line \r carriage return \t horizontal tab \v vertical tab \\ backslash \num the character whose ASCII code is NUM (octal). You can explicitly turn off the interpretation of the above characters with the -E option.
  • Slide 7
  • Initialisation Files commands & variables read when shell is started if variables are placed in system-wide init files, available to every shell customizations must be exported to be available commands & aliases cannot be exported so must be placed in user-specific init
  • Slide 8
  • /etc/profile - System-wide Initialization File Read first by shell Tasks accomplished: Sets and exports variables: PATH, TERM, PS1, etc. Displays contents of /etc/motd (msg of the day) Sets default file creation permissions (umask)
  • Slide 9
  • User-specific initialisation If the shell is a login shell, it looks for one of the following (in order) ~/.bash_profile ~/.bash_login ~/.profile If it's an interactive, non-login shell, it reads ~/.bashrc
  • Slide 10
  • Other customizations PS1 prompt PATH add to the search path Set shell options noclobber ignoreeof command-line editing option (vi or emacs) See ~kschmidt/Public/.bash_profile and.bashrc for examples (Feel free to take them, as you wish)
  • Slide 11
  • Shell Variables The shell keeps track of a set of parameter names and values. Some of these parameters determine the behavior of the shell. We can access these variables: set new values for some to customize the shell. find out the value of some to help accomplish a task.
  • Slide 12
  • Example Shell Variables sh / ksh / bash PWD current working directory PATH list of places to look for commands HOME home directory of user MAIL where your email is stored TERM what kind of terminal you have HISTFILE where your command history is saved
  • Slide 13
  • Displaying Shell Variables Prefix the name of a shell variable with "$" to dereference The echo command will do: echo $HOME echo $PATH You can use these variables on any command line: ls -al $HOME
  • Slide 14
  • Setting Shell Variables You can change the value of a shell variable with an assignment command (this is a shell builtin command): HOME=/etc PATH=/usr/bin:/usr/etc:/sbin NEWVAR="blah blah blah" Note the lack of spaces around the '='
  • Slide 15
  • export export a variable make it available to subshells value passed in changes made to a value in the subshell do not persist in the caller Subshell is created whenever a script or a program is run Inherits parent shell's exported variables
  • Slide 16
  • The PATH Each time you give the shell a command line it does the following: Checks to see if the command is a shell built-in. If not - tries to find a program whose name (the filename) is the same as the command. The PATH variable tells the shell where to look for programs (non built-in commands).
  • Slide 17
  • echo $PATH ======= [tux] - 22:43:17 ======= $ echo $PATH ~/bin:/usr/local/sbin:/usr/local/bin:/usr/sbi n:/usr/bin:/sbin:/bin:/usr/games The PATH is a list of ":" delimited directories. The PATH is a list and a search order. You can add stuff to your PATH by changing the shell startup file.
  • Slide 18
  • Notes about PATH If you do not have. in your PATH, commands in your current directory will not be found. You may or may not want to add. to your PATH. If you do not and want to execute a command in the current directory./command The PATH is searched sequentially, the first matching program is executed. Beware of naming your executables test as there is another program called test and this may be executed when you enter test
  • Slide 19
  • PATH [jjohnson@ws44 software]$ echo $PATH /usr/local/FrameMaker/bin:/home/jjohnson/bin:/usr/local/gpl/mpich- 1.2.4/bin:/usr/local/bin:/usr/sbin:/sbin:/usr/openwin/bin:/opt/SUNWspr o/bin:/usr/ccs/bin:/usr/ucb:/usr/sbin:/usr/bin:/etc:/usr/etc:/usr/UTILS/ publisher/bin:/usr/bin/X11:/bin:/usr/remote/alg_soft/linda2.5.2sol2.3/bi n:. [jjohnson@ws44 software]$ PATH=${PATH}:/home/jjohnson/software [jjohnson@ws44 software]$ echo $PATH /usr/local/FrameMaker/bin:/home/jjohnson/bin:/usr/local/gpl/mpich- 1.2.4/bin:/usr/local/bin:/usr/sbin:/sbin:/usr/openwin/bin:/opt/SUNWspr o/bin:/usr/ccs/bin:/usr/ucb:/usr/sbin:/usr/bin:/etc:/usr/etc:/usr/UTILS/ publisher/bin:/usr/bin/X11:/bin:/usr/remote/alg_soft/linda2.5.2sol2.3/bi n:.:/home/jjohnson/software
  • Slide 20
  • set command (shell builtin) The set command with no parameters will print out a list of all the shell varibles. Some common options: noclobber Keeps mv, cp, redirection from deleting an existing file -o vi (or -o emacs ) sets command-line editing mode ignoreeof ctrl-D won't exit the shell
  • Slide 21
  • $PS1 and $PS2 The PS1 shell variable is your command line prompt. The PS2 shell variable is used as a prompt when the shell needs more input (in the middle of processing a command). By changing PS1 and/or PS2 you can change the prompt.
  • Slide 22
  • Fancy bash prompts Bash supports some fancy stuff in the prompt string: \t is replace by the current time \w is replaced by the current directory \h is replaced by the hostname \u is replaced by the username \n is replaced by a newline
  • Slide 23
  • Example bash prompt ======= [queen] - 22:43:17 ======= ~ echo $PS1 ======= [\h] - \t =======\n\w You can change your prompt by changing PS1: PS1="Yes Master? "
  • Slide 24 >>">
  • Metacharacters Shell metacharacters are characters that are handled specially by the shell. Below is an (incomplete) list of shell metacharacters > >> < file; cal 10 2000 >> file (echo Month ; cal 10 2000 ) > file">
  • Grouped Commands If we apply the same operation to the group, we can group commands Commands are grouped by placing them into parentheses Commands are run in a subshell Example: echo Month > file; cal 10 2000 >> file (echo Month ; cal 10 2000 ) > file
  • Slide 42
  • Conditional Commands We can combine two or more commands using conditional relationships AND (&&) and OR (||). If we AND two commands, the second is executed only if the first is successful. If we OR two commands, the second is executed only of the first fails. cp file1 file2 && echo Copy successful cp file1 file2 || echo Copy failed
  • Slide 43
  • Shell Syntax Comments # This is a comment ls # list the files in the current directory Line continuation echo A long \ > line ; #Command separator you can list more than one command per line separated by ; ls ; who / #Pathname separator cd /home/jjohnson
  • Slide 44
  • Shell Syntax wildcards (globbing) Wildcards, and pathname expansion * # match any string (including empty) ? # match any single character [set] # match characters listed in set (can be range) [!set] # match any character not given in set Examples ls *.c ls *.? ls *.[Hh][Tt][Ll] ls [a-z]
  • Slide


View more >