Download presentation
Presentation is loading. Please wait.
Published byPaul Jordan Modified over 9 years ago
1
THE BOURNE SHELL
2
Shell : the agency that sits between the user and the UNIX system. The BOURNE SHELL named after its founder Steve Bourne, is one of the earliest shells that came with the UNIX system, and also one that is most widely used. There are other shells that feature in every UNIX system today. The C shell, a product from the University of California, Berkeley and the Korn shell from David Korn of Bell Laboratories are the most notable.
3
If you list out the / bin directory, you will see an executable program named sh. This is the Bourne shell. The C shell and the Korn shell, if present will have the name csh and ksh, respectively. When you issue an instruction, i.e., a command, the shell is the first agency to acquire the information. Before executing the command, it sees whether the command line is in a form which the system can understand. If it is not, then it processes the request to recreate a simplified command line. It then leaves the job of command execution to the kernel. The shell is really a UNIX command- a program that starts when you log in, and terminates when you log out. But unlike other UNIX commands, it is there all the time, indicating its presence by the familiar $ prompt.
4
Many commands require complex interaction with the hardware, like opening and closing of files, starting up a tape drive unit, or skipping a page on the printer. The shell doesn’t interact with the hardware directly. Instead, it works hand in hand with its agent, the kernel, which communicates directly with all hardware, and takes care of all the complex processes that go on in any system. One of the functions of the shell is to wait for input from the user. Anything keyed in through the keyboard, and at the $ prompt, is actually the input to the sh program. With this input, it performs a series of processing tasks, interacting with the kernel when necessary. After the job is complete, it returns to its waiting role, to start the next “cycle”.
5
The following activities are typically performed by the shell in each cycle : ( a ) it issues the $ prompt, and waits for you to enter a command. ( b ) after a command has been entered, the shell scans the command line for some special characters, and then rebuilds the command line after processing is complete. ( c ) the command is then passed on to the kernel for execution, and the shell waits for its completion. ( d ) the $ prompt appears, and the shell waits for you to enter the next command. When there is no input from the user, the shell is said to be sleeping. This is indicated by the $ prompt. It wakes up whenever a user enters some characters through the keyboard, and presses the key. The productive work of the shell begins after it has accepted the characters that are keyed in.
6
What does the shell do with the input? The shell first scans the command line, and processes it in a number of steps. During the scanning operation, it looks for certain characters which have special meaning for it. Because it permits abbreviated command lines ( like the use of * to indicate all files, as in rm * ), the shell has to make sure the abbreviations are expanded before the command can act upon them. It rebuilds the command line at every step, and when all processing is complete, it passes on the processed command line to the kernel for execution. The shell normally can’t do any work while the command it has sent the kernel is in the process of execution. It has to wait, and when execution is complete, it returns the $ prompt for resuming the next cycle.
7
Preceding a command by it path A command runs in unix by executing the file containing the command. When you specify a date command, the shell has to locate the date program (or the file name date) from a “predetermined list”, and then execute it. But if you know the location of a particular command, you can precede its name with the complete path. Since the date command resides in the /bin directory, you can also use the complete ( or absolute ) pathname: $ /bin/date You normally don’t need to do that for files residing in the /bin and the /usr/bin directories but if you are to execute programs residing in some other directory which is not the current directory, then the pathname must be specified.
8
Combining commands UNIX allows you to specify more than one command in the same command line. Each command has to be separated from the other by a ; (semicolon). Ex: $ chmod +rw note ; ls –l note When a command line contains a’;’ character, the shell understands that the commands on either side of it need to be processed separately. This special character is known as a metacharacter.
9
Pattern matching – the wild-cards The shell enables specification of a general pattern of characters in the command line to match a group of filenames. Ex: $ ls –l chap chap01 chap02 chap03 chap04 chapx chapy chapz Can be shortened with the sequence: $ ls –x chap* There are other characters in set relating to filenames, and understood by the shell. These characters are also known as wild- cards.
10
The shell’s wild-cards Wild-cardSignificance *Matches any number of characters including none ?Matches a single character [ijk]Matches a single character- either an i, j or k [! ijk]Matches a single character which is not an i, j or k [x-z] Matches a single character which is within the ASCII range of the characters x and z [!x-z] Matches a single character which is not within the ASCII range of the characters x and z
11
Examples $ ls –x chap? Chapx chapy chapz $ ls –x chap?? Chap01 chap02 chap03 chap04 $ ls –x chap0[1-4] Chap01 chap02 chap03 chap04 $ ls –x chap0[124] Chap01 chap02 chap04
12
QUOTING AND THE echo COMMAND The echo is a built-in command of the shell, and displays its argument ( excluding the options). Ex: $ echo Good Morning Good Morning When you enter a shell metacharacter as an element of the expression, for example, the *? $ echo * You simply see a list of all files in the current directory.
13
But if you intend to echo a * without permitting the shell to interfere, escaping with a \, solves the problem: $ echo \* * When the argument to a command is enclosed in single quotes the meanings of all special characters are turned off: $ echo ‘*’ * To echo a \, you should give: $ echo \\ \ $ echo ‘\’ \
14
REDIRECTION In the context of redirection, the terminal is a generic name that represents the screen, display or keyboard. We see command output and error messages on the terminal (display), and we sometimes provide the command input through the terminal (keyboard). The shell associates three files with the terminal- two for the display and one for the keyboard. These special files are actually streams of characters which many commands see as input and output. A stream is simply a sequence of bytes.
15
When a user logs in, the shell makes available three files representing three streams. Each stream is associated with a default device, and – generically, this device is the terminal: Standard Input- the file (or stream) representing input, which is connected to the keyboard. Standard output- the file (or stream) representing output, which is connected to the display. Standard Error - the file (or stream) representing error messages that emanate from the command or shell. This is also connected to the display. A group of UNIX commands reads from and writes to these files. A command is usually not designed to send output to the terminal but to this file. Every command that uses streams will always find these files open and available.
16
Even though the shell associates each of these files with a default physical device, this association is not permanent. The shell can easily unhook a stream from its default device and connect it to a disk file (or to any command) the moment it sees some special characters in the command line. Ex:
17
Standard Input When cat and wc commands are used without arguments, they read the file representing the standard input. This file can represent three input sources : The keyboard, the default source. A file using redirection with the < symbol ( a metacharacter). Another program using a pipeline. When you use wc without an arguments and have no special symbols like the < and | in the command line, wc obtains its input from the default source. You have to provide this input from ;the keyboard and mark the end of input with [Ctrl-d]. $ wc standard input can be redirected it can come from a file or a pipeline [Ctrl-d] 3 14 71
18
Use wc with a filename as argument: $ wc e1.lst 5 43 231 e1.lst Here wc prints the filename because it opened the file itself. Wc can reassign or redirect the standard input to originate from a file on disk. This redirection requires the < symbol: $ wc < e1.lst 5 43 231 The file name is missing, which means that wc didn’t open e1.lst. It read the standard input file as a stream but only after the shell made a reassignment of this stream to a disk file.
19
When the standard input is redirected to come from a file (with <), it is the shell that opens the file. The command here is totally ignorant of what the shell is doing to provide it with input. However, when you invoke a command with a filename as argument, it’s the command that opens the file and not the shell. Taking input both from the file and Standard input: Cat – foo First from the standard input then from the foo Cat foo – bar First from foo, then standard input, and then bar
20
Standard Output All commands displaying output on the terminal actually write to the standard output file as a stream of characters, and not directly to the terminal. There are three possible destinations of this stream: The terminal, the default destination. A file, using the redirection symbols > and >>. As input to another program using a pipeline.
21
The File Descriptor Each of the three standard files is represented by a number, called a file descriptor. A file is opened by referring to its pathname, but subsequent read and write operations identify the file by this file descriptor. The kernel maintains a table of file descriptors for every process running in the system. The first three slots are generally allocated to the three standard streams in this manner: 0- Standard input 1- Standard output 2- Standard error
22
Standard Error When a command runs unsuccessfully, diagnostic messages often show up on the screen. This is the standard error stream whose default destination is the terminal. Trying to cat a nonexistent file produces the error stream: $ cat foo cat: foo: No such file or directory Cat fails to open the file and writes to the standard error. If you are not using the C Shell, you can redirect this steam to a file. [itlaxmi@snist ~]$ cat foo > errfile cat: foo: No such file or directory [itlaxmi@snist ~]$ cat errfile [itlaxmi@snist ~]$ cat foo 2> errfile [itlaxmi@snist ~]$ cat errfile cat: foo: No such file or directory Error stream cannot be captured with >.
23
COMMAND SUBSTITUTION Another way of connecting two commands. While a pipe enables a command to obtain its standard input from the standard output of another command, the shell enables one or more command arguments to be obtained from the standard output of another command. This feature is called command substitution.
24
Ex: Suppose you need to display today’s date with a statement like this: The date today is Tue Aug 25 10:29:25 IST 2009 Then you should use: $ echo The date today is `date` The date today is Tue Aug 25 10:29:25 IST 2009 When scanning the command line, the ` (backquote or backtick) is another metacharacter that the shell looks for. The shell executes the enclosed command and replaces the enclosed command line with the output of the command. For command substitution to work, the command so “backquoted” must use standard output.
25
Ex: you can also use two commands in a pipeline and use the output as the argument to a third : $ echo "there are `ls |wc -l ` files in the current directory" there are 41 files in the current directory The above command when used with single quotes : $ echo 'there are `ls |wc -l ` files in the current directory' there are `ls |wc -l ` files in the current directory The ` is one of the few characters interpreted by the shell when placed within double quotes. If you want to echo a literal `, you have to use single quotes. Command substitution speeds up work by letting you combine a number of instruction in one.
26
SHELL VARIABLES The shell supports variables that are useful both in the command line and shell scripts. These variables are called shell variables. No type declarations or initialisations are necessary before you can use them. Shell variables are assigned with the = operator, but evaluated in a different way. They are evaluated by prefixing the variable name with a $. Variable usage in the Bourne family differs from that in the C shell. A variable assignment is of the form variable=value (no spaces around =), but its evaluation requires the $ as prefix to the variable name: $ count=5 $ echo $count 5
27
Variable names begin with a letter but can contain numerals and the _ as other characters. Names are case-sensitive. All shell variables are of the string type, which means even a number like 123 is stored as a string rather than in binary. All shell variables are initialized to null strings by default. While explicit assignment of null strings are possible with : X=“ “ X=‘ ‘ X= A variable can be removed with unset and protected from reassignment by readonly. Both are the shell internal commands.
28
Examples: $ x=12 [itlaxmi@snist ~]$ echo $x 12 [itlaxmi@snist ~]$ unset x [itlaxmi@snist ~]$ echo $x [itlaxmi@snist ~]$ y=35 [itlaxmi@snist ~]$ echo $y 35 [itlaxmi@snist ~]$ readonly y [itlaxmi@snist ~]$ unset y -bash: unset: y: cannot unset: readonly variable
29
Effects of Quoting and Escaping: To assign a multiword string to a variable, you can escape each space character, but quoting (single or double) is the preferred solution : Message=you\ didn’t\ enter\ the\ filename Message=“you didn’t enter the filename” If you want $ to interpret it literally without it being evaluated, you can use the options: $ echo The average pay is \$1500 The average pay is $1500 $ echo 'The average pay is $1500' The average pay is $1500 Like the backquote, the $ is also evaluated by the shell when it is double-quoted.
30
$ echo "the path is $PATH and the current directory is `pwd`" the path is /usr/lib64/qt- 3.3/bin:/usr/kerberos/bin:/usr/lib64/ccache:/usr/local/bin:/bin:/usr/bin: /home/itlaxmi/bin and the current directory is /home/itlaxmi $ echo "the average pay is $1000" the average pay is 000 The ex1 shows both command substitution and variable evaluation. The ex2, the shell evaluated a “variable” $1. it is not defined, so a null string was output. $1 belongs to a set of parameters that are called positional parameters, signifying the arguments that you pass to a script.
31
Where to use Shell Variables: Setting pathnames: If a pathname is used several times in a script, you should assign it to a variable. You can then use it as an argument to any command. Ex: Let’s use this command with cd: $ prgs='/home/itlaxmi/d1' $ cd $prgs;pwd /home/itlaxmi/d1 Using Command Substitution: you can also use the feature of command substitution to set variables: [itlaxmi@snist d1]$ mydir=`pwd`;echo $mydir /home/itlaxmi/d1
32
ENVIRONMENT VARIABLES Shell variables are of two types – local and environment. Environment variables control the behavior of the system. They are so called because they are available in the user’s total environment– the sub-shells that run shell scripts, and mail commands and editors. If they are not set properly, you may not be able to use some commands without a pathname. The set statement displays all variables available in the current shell, but the env command displays only environment variables.
33
$ env HOME=/home/itlaxmi HOSTNAME=snist IFS=$' \t\n‘ LOGNAME=itlaxmi MAIL=/var/spool/mail/itlaxmi MAILCHECK=60 PS1='[\u@\h \W]\$ ' PS2='> ' PS4='+ ' PWD=/home/itlaxmi TERM=xterm
34
By convention, environment variable names are defined in uppercase although nothing prevents you from using a different scheme. Applications often obtain information on the process environment through these environment variables.
35
Common Environment Variables Variable Significance HOMEHome directory- the directory a user is placed on logging in PATHList of directories searched by shell to locate a command LOGNAMELogin name of user USERAs above TERMType of terminal IFSInternal field separator CDPATHList of directories searched by cd when used with a nonabsolute pathname PS1Primary prompt string PS2Secondary prompt string SHELLUser’s login shell and one invoked by programs having shell escapes
36
SHELL SCRIPTS The shell offers the facility of storing a group of commands in a file and then executing the file. All such files are called shell scripts or shell programs or shell procedures. The instructions stored in these files are executed in the interpretive mode. Ex:: $ cat p1.sh directory=`pwd` echo the date today is `date` echo the current directory is $directory Execute the file by simply invokjing the filename : $ p1.sh P1.sh: execute permission denied
37
Executable permission is usually necessary for any shell procedure to run, and by default, a file doesn’t have this permission on creation. So use: $ chmod u+x p1.sh $ p1.sh The date today is Tue Aug 25 10:29:25 IST 2009 The current directory is /home/itlaxmi The script executes the three statements in sequence. Even though we used the shell as an interpreter, it is also a programming language.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.