60-256 system programming: unix shellskobti.myweb.cs.uwindsor.ca/60-256/c2.pdf · shell commands...
TRANSCRIPT
Content
60-256 System Programming:Unix shells
by Dr. A. Habed
School of Computer ScienceUniversity of Windsor
[email protected]://cs.uwindsor.ca/∼adlane/60-256
Unix Shells 60-256 System Programming
Content
Content1 Introduction2 Running a shell
Interactive and non-interactive shellslogin and non-login shellsExiting a shell
3 Unix variablesDefinitionEnvironment and Shell variablesThe global/local variables relationshipGetting and setting variables
4 Shell commands5 Shell startup files
csh startup filestcsh startup filesbash startup files
6 MetacharactersUnix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
Introduction
What is a shell?A shell is a program that acts as an interface between auser and the operating system.A Unix shell is basically a command interpreter.A Unix shell interprets commands that are either typed-in(keyboard) or in a script-file.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
Running shells
Running shellsA shell can run as:
a login shell: it is the first shell that runs as you login.a non-login shell: it is a shell that the user, a script-file or aprogram executes after loginan interactive shell: it is a shell that waits for user inputthrough a command line.a non-interactive shell: it is responsible of executing a shellscript and does not wait for user input.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
Some existing shells
Many shells exist. They differ by their syntax and by thefunctionalities that they offer.
Some existing shellsAll shells are normally located in /usr/bin/ or /bin/:
Bourne shell: sh is the most basic shell.C shell: csh richer than sh, syntax ∼ 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.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
Interactive and non-interactive shellslogin and non-login shellsExiting a shell
Interactive vs. non-interactive shells
An interactive shellAn interactive shell
1 reads a special startup files that contain some initializationinformation.
2 displays a prompt and waits for user commands then,3 the shell executes the user’s command and returns to step
2 unless it is terminated.
A non-interactive shellIn general, a non-interactive shell is present to run ascript-file.The first line of a script-file indicates the shell that will runthe script.A script-file starting with the line: #!/usr/bin/cshwill automatically run under the C shell
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
Interactive and non-interactive shellslogin and non-login shellsExiting a shell
The login shell
The login shellYour login shell it is the first shell that runs as you login to youraccount on the computer.
This shell is interactive.The Unix system administrator chooses a shell for eachuser when the account is created. This is your login ordefault shell.The name of your login shell program can be found in thecorresponding line in /etc/passwd.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
Interactive and non-interactive shellslogin and non-login shellsExiting a shell
A non-login shell
A non-login shell can be either interactive or non-interactive:
non-login non-interactive shellAn example of a non-login non-interactive shell is one thatexecutes a script-file as discussed in the previous slides.
non-login interactive shellAn example of a non-login interactive shell is one that is startedfrom the command line by typing its name. For example, to runC shell, type:csh
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
Interactive and non-interactive shellslogin and non-login shellsExiting a shell
Changing your login shell
Changing your login (or default) shellYou can change your login shell by:
changing your entry in the passwd file (you need to be asuper-user)using the chsh command (not always available). Thepasswd command can also be used (with appropriateoptions).The new shell must be a valid shell, i.e. defined in the file/etc/shells. This file must be defined by thesuper-user.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
Interactive and non-interactive shellslogin and non-login shellsExiting a shell
Exiting a shell
Because the shell accepts commands from the keyboard, itterminates when the end-of-file character CTRL-D isentered.The exit command allows also to exit a shell.
When the user logs out from the systemWhen the user is logging out from the system, the shell readsand executes commands from a file (if the file exists). Forexample:
csh and tcsh execute the file ∼/.logoutbash executes ∼/.bash logout
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
DefinitionEnvironment and Shell variablesThe global/local variables relationshipGetting and setting variables
Unix variables: definition
A Unix variable allows passing information from the shell toprograms.Its 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 can be set either by
the system,the user (through a command-line),the shell,a program.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
DefinitionEnvironment and Shell variablesThe global/local variables relationshipGetting and setting variables
Environment and Shell variables
Environment and Shell variables are, in general, used tocustomize the environment in which your shell runs. Most ofthese variables are initialized by a start-up file.
Standard Unix variables are split into two categories,1 Shell variables are local variables. They apply only to the
current instance of the shell and are used to set short-termworking conditions. By convention, they are named inlower case characters.
2 Environment variables are global variables. They have afarther reaching significance, and those set at login arevalid for the duration of the session. By convention, theyare named in upper case characters.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
DefinitionEnvironment and Shell variablesThe global/local variables relationshipGetting and setting variables
Example of environment (global) variables
OSTYPE: the name of the current operating systemUSER: your login nameLOGNAME: the login-name of the userHOME: the path name of your home directoryMAIL: the path to your mail inboxHOST: the name of the computer you are usingDISPLAY: the name of the computer screen displayPRINTER: the default printerEDITOR: the default text editorPATH: the directories the shell should search to find acommandSHELL: the full pathname of the login shell
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
DefinitionEnvironment and Shell variablesThe global/local variables relationshipGetting and setting variables
Example of shell (local) variables
argv: an array of command/program argumentscwd: the full pathname of the current directorygid: the user’s real group IDhistory: indicates the number of history events to save.home: initialized to the home directory of the invoker.owd: the old (pr previous) working directorypath: a list of directories to look for executable commandsprompt: the string printed before reading a commandshell: the full pathname of the running shellterm: the terminal typetty: the name of the tty (TeleTYpe) terminaluid: the user’s real user IDuser: the user’s login name
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
DefinitionEnvironment and Shell variablesThe global/local variables relationshipGetting and setting variables
The global/local variables relationship (in general)
In generalIn general, environment and shell variables that have thesame name (apart from the case) are distinct andindependent.Possibly, these variables can have the same initial values.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
DefinitionEnvironment and Shell variablesThe global/local variables relationshipGetting and setting variables
The global/local variables relationship (special cases)There are, however, some exceptions to the general case:
HOME, USER and TERM
Every time the shell variables home, user and term arechanged, the corresponding environment variables HOME,USER and TERM receive the same values.However, changing the environment variables has no effecton the corresponding shell variables.
PATH
PATH and path specify directories to search forcommands and programs.Both variables always represent the same directory list,and altering either automatically causes the other to bechanged.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
DefinitionEnvironment and Shell variablesThe global/local variables relationshipGetting and setting variables
Getting the value of a variable
The value of an environment or shell variable is either astring or a set of strings.Regardless of which shell is running, getting the value of avariable is possible by placing the $ sign at the beginningof the variable name. e.g.:$VARIABLERegardless of the running shell, you can also get the valueof the variable by using. e.g.:${VARIABLE}which is particularly useful if the variable name isimmediately followed by other text.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
DefinitionEnvironment and Shell variablesThe global/local variables relationshipGetting and setting variables
Examples
ExampleDetermining your login shellThe pathname of the login shell is stored in the shell variableSHELL. You get your login shell by typingecho $SHELL/bin/tcsh
Exampleecho My shell saves $history eventsMy shell saves 32 events
echo My shell does not save ${history}00 eventsMy shell does not save 3200 events
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
DefinitionEnvironment and Shell variablesThe global/local variables relationshipGetting and setting variables
Setting a variable
Unlike getting the value of a variable, setting a variable differsfrom one shell to another. For example, to define a variableCOLOR:
using Bourne shell (sh):COLOR=greenBy default, variables in this shell are local. To turn a localvariable global, use: export COLORusing C shell and TC shells:setenv COLOR greensetenv is used for global (environment) variables. Forlocal (shell) variables, use: set color=greenNote the difference in the syntax of setenv and set.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
DefinitionEnvironment and Shell variablesThe global/local variables relationshipGetting and setting variables
How to unset a variable
Syntax in csh and tcsh:unset [var-name-list], for local variablesOr,unsetenv [var-name-list], for environment variables
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
DefinitionEnvironment and Shell variablesThe global/local variables relationshipGetting and setting variables
Listing the values of all variables
RemarkUnder csh and tcsh,
set without arguments displays all shell variables and theirvalues,setenv without arguments displays all environmentvariables and their values.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
DefinitionEnvironment and Shell variablesThe global/local variables relationshipGetting and setting variables
Example 1The following examples shows that an environmentvariable COLOR when set, is inherited by subsequentshells. This variable is initially undefined in all shells.Lines starting with # are comments for clarity.
Example on our luna server with tcsh as a login shell
luna:˜>setenv COLOR yellow #set COLOR as an environment variable
luna:˜>echo $COLOR #display the value of the variable COLOR
yellow
luna:˜>tcsh #run a new instance of tcsh
luna:˜>echo $COLOR #display the value of the variable COLOR
yellow
luna:˜>bash #run an instance of bash
bash-3.00$ echo $COLOR #display the value of the variable COLOR
yellow
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
DefinitionEnvironment and Shell variablesThe global/local variables relationshipGetting and setting variables
Example 2The following examples shows that a shell variable colorwhen set, is not inherited by subsequent shells. Thisvariable is initially undefined in all shells.Lines starting with # are comments for clarity.
Example on our luna server with tcsh as a login shell
luna:˜>set color=red #set color as a shell variable
luna:˜>echo $color #display the value of the variable color
red
luna:˜>tcsh #run a new instance of tcsh
luna:˜>echo $color #display the value of the variable color
color: Undefined variable.
luna:˜>bash #run an instance of bash
bash-3.00$ echo $color #display the value of the variable COLOR
#an empty line is displayed
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
External vs. internal commands
A shell command can beinternal (built-in): the code to be executed is part of theshell. Running these commands does not requiresearching their path by the system.external: the code to be executed resides in a speratebinary file. Running these commands requires searchingtheir path by the system.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
External Commands
PATH
For an external command, the shell searches for its file in thedirectories whose names are stored in the variable PATH.How to define the shell variable PATH?Example: set path = ./:/usr/bin:∼/bin→ the shell looks for the command in order in
the current directory, called ./,/usr/bin/your-login-directory/bin/ (which is also ∼/bin)
RemarkNote that after the above setting, both path and PATH containthe same string ./:/usr/bin:∼/bin.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
Modifying the PATH variable
Modifying the PATH variableTo add more directories to your current path by:set path = /usr/local/bin:$PATHYou can view your path: echo $PATHThe command which followed by the command-name allowsyou to find the file location of the command.Examples: which echo, which emacs
RemarkNote that PATH is set in a shell startup file,Example: in the file ∼/.cshrc,where ∼/ represents the current user login directory.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
csh startup filestcsh startup filesbash startup files
Startup files
Most environment and shell variables are initialized by thestartup files.Different shells read different startup files.Some startup files are in the directory /etc/. These areintended for system wide initialization of environmentvariables.You can customize your environment and shell variables byassigning different values to these variables in the startupfiles in your home directory (which we refer to as ∼).
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
csh startup filestcsh startup filesbash startup files
C shell (csh) startup files
Whenever invokedWhen csh is invoked, it first executes the commands in thesystem-wide startup file, usually located in/etc/csh.cshrc.Then, csh reads and executes a script named ∼/.cshrc.
Invoked as a login shellIn addition to the above files, tcsh reads and executes the files/etc/csh.login and ∼/.login once only at login.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
csh startup filestcsh startup filesbash startup files
TC shell (csh) startup files
Whenever invokedWhen tcsh is invoked, it first executes the commands inthe system-wide startup file, usually located in/etc/csh.cshrc.Then, tcsh reads and executes a script named∼/.tcshrc. If ∼/.tcshrc is not found, then it executes∼/.cshrc.
Invoked as a login shellIn addition to the above files, tcsh reads and executes the files/etc/csh.login and ∼/.login once only at login.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
csh startup filestcsh startup filesbash startup files
Bourne Again shell (bash) startup files
Invoked as a login shellbash attempts to read several initialization files:
First, it reads and executes commands in /etc/profileThen, it reads and executes commands from the first oneof the files ∼/.bash profile, ∼/.bash login and∼/.profile
Invoked as an interactive non-login shellbash reads and executes commands from ∼/.bashrc.
Invoked as a non-interactive shellWhen a non-interactive bash is started, bash reads andexecutes commands in the file named by the variable$BASH ENV if defined.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
csh startup filestcsh startup filesbash startup files
Example of ∼/.cshrc in luna
Example# @(#)cshrc 1.11 89/11/29 SMI
umask 077
set path=(/bin /usr/bin /usr/ucb /etc .)
if ( $?prompt ) thenset history=32
endif if ( -f /etc/system.cshrc) thensource /etc/system.cshrc
endif
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
Metacharacters...
These are special characters with special meanings:
>: Output redirectionE.g., ls > fileName.txt<: Input redirectionE.g., mailx [email protected] < letter.txt>>: Output redirection, appends to a fileE.g., ls >> fileName.txt
*: filename Wild card, matches 0 or more charactersE.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.
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
Metacharacters (cont’d)
‘command ‘: command substitution, replaced by thecommand output.E.g. 1. echo The date of today is ‘date‘E.g. 2. echo hello ls→ hello lsecho 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 thenumber of files/directories.Note the utility wc displays a count of lines, words andcharacters in a file.;: Used to sequence commandsE.g., date ; ls; date||: Executes a command if the previous one fails.E.g., cc prog1.c || CC prog1.c || gcc prog1.c
Unix Shells 60-256 System Programming
IntroductionRunning a shell
Unix variablesShell commands
Shell startup filesMetacharacters
Metacharacters (cont’d)
&& : Executes a command if the previous one succeedsE.g., CC prog1.c -o prog1 && ./prog1&: Executes a command in the backgroundE.g., netscape &#: characters after this are ignored (comment)$: Expands the value of a shell variableE.g., echo $PATH\: Prevents special interpretation of next character.E.g., echo this is \&→ this is && is not a metacharacter in this case.
Unix Shells 60-256 System Programming