Unformatted text preview:

Lecture 2 More UNIX Bash Shell Scripting Programming Tools And Environments 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 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 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 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 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 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 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 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 a non login interactive shell reads bashrc 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 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 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 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 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 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 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 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 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 PATH jjohnson ws44 software echo PATH usr local FrameMaker bin home jjohnson bin usr local gpl mpich1 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 mpich1 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 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 PS1 and PS2 The PS1 shell variable is your command line prompt The PS2 shell variable is


View Full Document

DREXEL CS 265 - shellScripting

Loading Unlocking...
Login

Join to view shellScripting and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view shellScripting and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?