1 / 32

03-60-256-01 System Programming UNIX Shells

03-60-256-01 System Programming UNIX Shells. Quazi Rahman [Modified from slides by Dr. B. Boufama ] School of Computer Science University of Windsor Winter, 2013. Outline. Introduction to UNIX Shell Unix Variables Shell Commands Shell Start-up Files Meta-characters. Introduction.

naomi
Download Presentation

03-60-256-01 System Programming UNIX Shells

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. 03-60-256-01System ProgrammingUNIX Shells QuaziRahman [Modified from slides by Dr. B. Boufama] School of Computer Science University of Windsor Winter, 2013

  2. Outline • Introduction to UNIX Shell • Unix Variables • Shell Commands • Shell Start-up Files • Meta-characters

  3. Introduction • What is a shell? • A shell is a program that acts as an interface between a user and the operating system. • A Unix shell is basically a command interpreter. • It interprets commands that are either typed-in (keyboard) or read from a file (script-file). • A shell can run in two modes: • an interactive shell: the shell that waits for user input through a command line. • a login shell: the first shell that runs as you login. • a non-login shell: the shell that the user, a script-file or a program executes after login • a non-interactive shell: the shell that executes shell scripts and does not wait for user input. • A non-interactive shell is, in general, a non-login shell. UNIX Shell

  4. Introduction • An interactive shell • 1. Reads a special start-up files that contain some initialization information. • 2. Displays a prompt and waits for user commands. • 3. Executes the user’s command and returns to step 2 unless it is terminated. • A non-interactive shell • In general, reads a script-file and executes the commands sequentially. • The first line of a script-file indicates the shell that will run the script. • A script-file starting with the line: #!/usr/bin/bash will automatically run under the Bourne Again Shell Interactive vs. Non-interactive Shells

  5. Introduction • Many shells exist. They differ by their syntax and by the functionalities that they offer. • All shells are normally located in /usr/bin/ or /bin/: • Some existing shells are: • Bourne shell: sh is the most basic shell. • C shell: cshricher than sh, syntax similar to C. • Korn shell: ksh derived from sh with more functions. • TC shell: tcsh derived from csh with more functions. • Bourne Again Shell: bash compatible with sh and ksh. Some Existing Shells sh csh ksh tcsh bash

  6. Introduction • The login shell is the first shell that runs as you login to your account on the computer. • This shell is interactive. • The Unix system administrator chooses a shell for each user when the account is created. This is your login or default shell. • The name of your login shell program can be found in the corresponding line in /etc/passwd. • Remember an example of an entry in /etc/passwd: • oconnel:x:1003:10:David O’Connell, M.Sc. Student: /users/oconnel:/bin/bash The Login Shell

  7. Introduction • The non-login shell can be either interactive or non-interactive: • Non-login interactive shell • An example of a non-login interactive shell is one that is started from the command line by typing its name. For example, to run BASH shell, type: $ bash • Non-login non-interactive shell: • An example of a non-login non-interactive shell is one that executes a script-file as discussed in the previous slides. The Non-login Shell

  8. Introduction • You can change your login (or default) shell by: • changing your entry in the passwd file (you need to be a super-user) • using the chsh command (not always available). The passwd command can also be used (with appropriate options). • The new shell must be a valid shell, i.e. defined in the file /etc/shells. This file must be defined by the super-user. Changing Your Login Shell

  9. Introduction • Because the shell accepts commands from the keyboard, it terminates when the end-of-file character CTRL-D is entered. • The exit command also allows to exit a shell. • When the user is logging out from the system, the shell reads and executes commands from a logout file (if exists). • For example: • csh and tcsh execute the file /.logout • bash executes /.bash_logout Exiting a Shell

  10. Unix Variables • A Unix variable allows passing information from the shell to programs. • A variable value is a string or a set of strings. • Variable names are case sensitive. • A variable name may contain up to 20 characters (numbers, letters and the underscore character). • A program looks for a particular variable and uses its value. • A variable (value) can be set either by: • the system, • the user (through a command-line), • the shell, • a program. Unix Variables

  11. Unix Variables • Standard UNIX variables are split into two categories: • Environment variables and • Shell variables • Environment and Shell variables are, in general, used to customize the environment in which your shell runs. • Most of these variables are initialized by a start-up file. • Environment variables are global variables. • They have a far reaching significance, and those set at login are valid for the duration of the session. • By convention, environment variables are named in upper case characters. • Shell variables are local variables. • They apply only to the current instance of the shell and are used to set short-term working conditions. • By convention, shell variables are named in lower case characters. Types of UNIX Variables

  12. Unix Variables • Example of environment (global) variables • OSTYPE: the name of the current operating system • USER: your login name • LOGNAME: the login-name of the user • HOME: the path name of your home directory • MAIL: the path to your mail inbox • HOST: the name of the computer you are using • DISPLAY: the name of the computer screen display • PRINTER: the default printer • EDITOR: the default text editor • PATH: the directories the shell should search to find a command • SHELL: the full pathname of the login shell Environment Variables

  13. Unix Variables • Example of shell (local) variables • argv: an array of command/program arguments • cwd: the full pathname of the current directory • gid: the user’s real group ID • history: indicates the number of history events to save. • home: initialized to the home directory of the invoker. • owd: the old (previous) working directory • path: a list of directories to look for executable commands • prompt: the string printed before reading a command • shell: the full pathname of the running shell • term: the terminal type • tty: the name of the tty (TeleTYpe) terminal • uid: the user’s real user ID • user: the user’s login name Visit this page for list of bash reserved variables; http://cs.uwindsor.ca/~rahmanq/256/Bash_Res_Vars.pdf Shell Variables

  14. Unix Variables • In general, environment and shell variables that have the same name (apart from the case) are distinct and independent, though these variables can have the same initial values. • Exceptions : • HOME, USER and TERM • Every time the shell variables home, userand termare changed, the corresponding environment variables HOME, USER and TERM receive the same values. • However, changing the environment variables has no effect on the corresponding shell variables. • PATH • PATH and path specify directories to search for commands and programs. • Both variables always represent the same directory list, and altering either automatically causes the other to be changed. Relationship between the Variables Types

  15. Unix Variables • The value of an environment or shell variable is either a string or a set of strings. • Regardless of which shell is running, getting the value of a variable is possible by placing the $ sign at the beginning of the variable name. e.g.: • $VARIABLE, • ${VARIABLE}, useful if the variable name is immediately followed by other text. Getting the Value of a Variable

  16. Unix Variables • Determining your login shell • The pathname of the login shell is stored in the shell variable SHELL. You can get your login shell by typing • echo $SHELL • /bin/bash • Example • echo My shell saves $HISTSIZEevents • My shell saves 500events • echo My shell does not save ${HISTSIZE}00 events • My shell does not save 50000 events Getting the Value of a Variable

  17. Unix Variables • Unlike getting the value of a variable, setting a variable differs from one shell to another. • For example, to define a variable COLOR: • using Bourne shell (sh) or BASH shell (bash): • COLOR=green • By default, variables in this shell are local. • To turn a local variable global, use: export COLOR • using C shell and TC shells: • For local (shell) variables, use: set color = green • For global (environment) variables, use: setenv COLOR green • Note the difference in the syntax of setand setenv. Setting the Value of a Variable

  18. Unix Variables • Unset a Variable: • Syntax in csh and tcsh: • unset [var_name_list], for local variables, • unsetenv [var_name_list], for environment variables. • Syntax in bash (local or environment): • unset [var_name_list], or, Var_name= • Listing the values of all variables: • Under bash, csh and tcsh, • set without arguments displays all shell variables and their values • Under bash • envorprintenvdisplays all environment variables and their values • Under csh and tcsh • setenv without arguments displays all environment variables and their values. How to Unset or List Variables

  19. Unix Variables • Example 1: • The following examples shows that an environment variable COLOR when set, is inherited by subsequent shells. This variable is initially undefined in all shells. • Lines starting with # are comments for clarity. • Example on our bravo server with bash as a login shell • bravo:~$ COLOR=yellow #set COLOR as a local variable • bravo:~$ export COLOR #set COLOR as an environment variable • bravo:~$ echo $COLOR #display the value of the variable COLOR yellow • bravo:~$ csh#run an instance of csh • bravo:~% echo $COLOR #display the value of the variable COLOR yellow • bravo:~% tcsh#run an instance of tcsh • bravo:~> echo $COLOR#display the value of the variable COLOR yellow Some Examples with Variables

  20. Unix Variables • Example 2: • The following examples shows that a shell variable color when set, is not inherited by subsequent shells. This variable is initially undefined in all shells. • Lines starting with # are comments for clarity. • Example on our bravo server with bash as a login shell • bravo:~$ color=red #set color as a local variable • bravo:~$ echo $color #display the value of the variable color red • bravo:~$ bash #run a new instance of bash • bravo:~$ echo $color #display the value of the variable color # an empty line • bravo:~% tcsh#run an instance of tcsh • bravo:~> echo $color#display the value of the variable color color: Undefined variable. Some Examples with Variables

  21. Unix Variables LIST=“./256/Test" ls -l $LIST total 13 drwx------ 2 rahmanqacadperm 2 Oct 19 2010 Test1 -rw-r--r-- 1 rahmanqacadperm 6636 Oct 19 2010 a.out -rw-r--r-- 1 rahmanqacadperm 134 Oct 19 2010 test.c -rw-r--r-- 1 rahmanqacadperm 302 Oct 19 2010 test2.c -rw-r--r-- 1 rahmanqacadperm 274 Oct 19 2010 test3.c -rw-r--r-- 1 rahmanqacadperm 268 Oct 19 2010 test_out.txt -rw-r--r-- 1 rahmanqacadperm 0 Oct 19 2010 test_out1.txt Simple Bash Variable Assignment Usage

  22. Shell Commands • There are two types of shell commands: • Internal (Built-in) Commands: • A command that is implemented internally by the shell itself, rather than by an executable program somewhere in the file system. • Running these commands does not require searching their path by the system. • External Commands: • The code to be executed resides in a separate binary file. • Running these commands requires searching their path by the system. Types of Shell Commands

  23. Shell Commands • For an external command, the shell searches for its file in the directories whose names are stored in the variable called PATH. • How to define the shell variable PATH? • Example: PATH=./:/usr/bin:~/bin • To make changes permanent, Use: export PATH • You do it at the same time: export PATH=./:/usr/bin:/bin • The shell looks for a command in order, • in the current directory, called ./, • in the directory /usr/bin/ • in the directory ~/bin/ • Adding more directories to the existing PATH: • PATH=$PATH:./:/usr/bin:/bin External Commands

  24. Shell Commands • Use of PATH • You can view your path: echo $PATH • The command which followed by the command-name allows you to find the file location of the command. • Examples: which echo, which emacs • Remark • Note that PATH is set in a shell startup file, • Example: in the file ~/.cshrc, where ~/ represents the current user login directory. External Commands

  25. Start-up Files • Most environment and shell variables are initialized by the startup files. • Different shells read different startup files. • Some startup files are in the directory /etc/. These are intended for system wide initialization of environment variables. • You can customize your environment and shell variables by assigning different values to these variables in the startup files in your home directory. Shell Start-up Files

  26. Start-up Files • Invoked as a login shell • bash attempts to read several initialization files: • First, it reads and executes commands in /etc/profile • Then, it reads and executes commands from the first one of the files /.bash_profile, /.bash_loginand /.profile • Invoked as an interactive non-login shell • bash reads and executes commands from /.bashrc. • Invoked as a non-interactive shell • When a non-interactive bash is started, it reads and executes commands in the file named by the variable $BASH_ENV,if defined. Bourne Again shell (bash) startup files

  27. Start-up Files • Whenever invoked • When csh is invoked, it first executes the commands in the system-wide startup file, usually located in /etc/csh.cshrc. • Then, csh reads and executes a script named /.cshrc. • Invoked as a login shell • In addition to the above files, csh reads and executes the files /etc/csh.loginand /.login once only at login. C shell (csh) startup files

  28. Start-up Files • Whenever invoked • When tcsh is invoked, it first executes the commands in the system-wide startup file, usually located in /etc/csh.cshrc. • Then, tcsh reads and executes a script named /.tcshrc. If /.tcshrcis not found, then it executes /.cshrc. • Invoked as a login shell • In addition to the above files, tcsh reads and executes the files /etc/csh.loginand /.login once only at login. TC shell (csh) startup files

  29. Start-up Files # @(#)cshrc 1.11 89/11/29 SMI umask 077 set path=(/bin /usr/bin /usr/ucb /etc .) if ( $?prompt ) then set history=32 endif if ( -f /etc/system.cshrc) then source /etc/system.cshrc endif Example of /.cshrcin barvo

  30. Metacharacters • These are special characters with special meanings: • >: Output redirection • E.g., ls > fileName.txt • <: Input redirection • E.g., mailx user@uwindsor.ca < letter.txt • >>: Output redirection, appends to a file • E.g., ls >> fileName.txt • *: filename Wild card, matches 0 or more characters • E.g., rm *ps delete all files ending with ’ps’. • ?: filename Wild card, matches 1 character. • E.g., rm *.?  delete files with one character after ’.’ • ls??  list files/directories made up of 2 characters. Metacharacters

  31. Metacharacters • ``:`command`: command substitution, replaced by the command output. • E.g. 1. echo The date of today is `date` • E.g. 2. echo hello ls hello ls • echo hello `ls` hello followed by the ls outputs. • |: Pipe between two commands. • E.g., ls | wc -w output of ls is piped to wc to get the number of files/directories . • Note the utility wc displays a count of lines (wc -l), words(wc -w) and characters (wc -c) in a file. • ;: Used to sequence commands • E.g., date ; ls; date • ||: Executes a command if the previous one fails. • E.g., cc prog1.c || CC prog1.c || gccprog1.c Metacharacters

  32. Metacharacters • && : Executes a command if the previous one succeeds • E.g., CC prog1.c -o prog1 && ./prog1 • & : Executes a command in the background • E.g., netscape & • # : characters after this are ignored (comment) • $ : Expands the value of a shell variable • E.g., echo $PATH • \ : Prevents special interpretation of next character. • E.g., echo this is \& this is & (& is not a metacharacter in this case.) Metacharacters

More Related