The Shell
University of Babylon
Department of Software
Dr.Safaa O. Al-Mamory
2011
Why Shell? Why Shell?
.Several major reasons for learning how to usethe shell are:
1-You will know how to get around any Linux orother UNIX-like system. For example, I can log in
to my Red Hat Linux MySQL server, my bootablefloppy router/ firewall and explore and use anyof those computer systems from a shell.
2-Special shell features enable you to gather datainput and direct data output between commandsand the Linux file system. To save on typing, youcan find, edit, and repeat commands from yourshell history.
3-You can gather commands into a file usingprogramming constructs such as loops and casestatements to quickly do complex operations thatwould be difficult to retype over and over.
Programs consisting of commands that are storedand run from a file are referred to as shell scripts.
Most Linux system administrators use shell scripts
to automate tasks such as backing up data,
monitoring log files, or checking system health.04/03/11 3
2-Special shell features enable you to gather datainput and direct data output between commandsand the Linux file system. To save on typing, youcan find, edit, and repeat commands from yourshell history.
3-You can gather commands into a file usingprogramming constructs such as loops and casestatements to quickly do complex operations thatwould be difficult to retype over and over.
Programs consisting of commands that are storedand run from a file are referred to as shell scripts.
Most Linux system administrators use shell scripts
to automate tasks such as backing up data,
monitoring log files, or checking system health.04/03/11 3
Shells Shells
.
An interface between the Linux system andthe user
.
Used to call commands and programs
.
An interpreter
.
Powerful programming language
.
“Shell scripts” = .bat .cmd EXEC REXX
.
Many available (bsh; ksh; csh; bash; tcsh)
Another definition of a
Shell
Another definition of a
Shell
.
A shell is any program that takes input fromthe user, translates it into instructions that
the operating system can understand, andconveys the operating system s output back
to the user.
· i.e. Any User Interface
· Character Based v Graphics Based
Shell Prompt Shell Prompt
.The default prompt for a regular user is simplya dollar sign:
$
.The default prompt for the root user is apound sign (also called a hash mark):
#
Which Shell I Have? Which Shell I Have?
.Type “ echo $SHELL” in the terminal window
and you will get
/bin/bash
If you use Fedora 10.
The Linux System
User commands includes executable
programs and scripts
The shell interprets user commands. It is
responsible for finding the commands
and starting their execution. Several
different shells are available. Bash is
popular,
The kernel manages the hardware resources
for the rest of the system.
User commands
Shell
File Systems Kernel
Device Drivers
Hardware
Types of Shell
.Sh – simple shell
.BASH – Bourne Again Shell
.KSH – Korne Shell
.CSH – C Shell
.SSH – Secure Shell
.To use a particular shell type the shell name at the command
prompt.
.Eg $csh – will switch the current shell to c shell
.To view the available shells in the system, type cat /etc/shells atthe command prompt
.To view the current shell that is being used, type echo $SHELL atthe command prompt
04/03/11 9
Types of Shell
.Sh – simple shell
.BASH – Bourne Again Shell
.KSH – Korne Shell
.CSH – C Shell
.SSH – Secure Shell
.To use a particular shell type the shell name at the command
prompt.
.Eg $csh – will switch the current shell to c shell
.To view the available shells in the system, type cat /etc/shells atthe command prompt
.To view the current shell that is being used, type echo $SHELL atthe command prompt
04/03/11 9
Command Structure
Command <Options> <Arguments>
Multiple commands separated by ; can be executed one
after the other
04/03/11 10
Linux Command Basics Linux Command Basics
.
To execute a command, type its name and
arguments at the command line
ls -l /etc
Command name Arguments
Options
(flags)
04/03/11 11
Redirecting Output
.
The output of a command may be sent
(piped) to a file:
ls -l >output
“>” is used to specify
the output file
04/03/11 12
Redirecting Input
.
The input of a command may come (bepiped) from a file:
wc <input
“<” is used to specify
the input file
04/03/11 13
Connecting commands
with Pipes
Connecting commands
with Pipes
.
Not as powerful as CMS Pipes but the sameprinciple
.
The output of one command can become theinput of another:
Like CMS Pipes, “|” is used
ps aux | grep netscape | wc -l
The output of the ps wcto separate stages
takes this input and
counts the lines its output
command is sent to
going to the console
grep
grep takes input and searches for
“netscape” passing these lines to wc
04/03/11 14
Pipes
An important early development in Unix was the invention
of "pipes," a way to pass the output of one tool to the input
of another.
eg. $who| wc-l
By combining these two tools, giving the wc command the
output of who, you can build a new command to list the
number of users currently on the system
04/03/11 15
Redirection and Pipes
.Redirection
.Input redirection
- wc < file1 – Content of file 1 is given as input for wccommand that counts the no of lines, words and
characters in a file
.Output redirection
- cat file > newfile – Copies file’s content to newfile. Overwrites the existing content
- cat file >> newfile – Appends the new content to the
existing content
.Pipes
.Output of first command is input for the second and so on
.who | wc –l – Number of lines in the output of who command
will be displayed
04/03/11 16
Redirection and Pipes
.Redirection
.Input redirection
- wc < file1 – Content of file 1 is given as input for wccommand that counts the no of lines, words and
characters in a file
.Output redirection
- cat file > newfile – Copies file’s content to newfile. Overwrites the existing content
- cat file >> newfile – Appends the new content to the
existing content
.Pipes
.Output of first command is input for the second and so on
.who | wc –l – Number of lines in the output of who command
will be displayed
04/03/11 16
Saving command output in
a file
.To save the output of a command in a file,
redirect the standard output to a file. Forexample,
$ ls -l >ss.out
.Then we can display file s content by:
vi ss.out
04/03/11 17
Saving command output in
a file
.To save the output of a command in a file,
redirect the standard output to a file. Forexample,
$ ls -l >ss.out
.Then we can display file s content by:
vi ss.out
04/03/11 17
Command Options
.
Command options allow you to control acommand to a certain degree
.
Conventions:
.
Usually being with a single dash and are asingle letter (“-l”)
.
Sometimes have double dashes followed by akeyword (“--help”)
.
Sometimes follow no pattern at all
04/03/11 18
Common Commands Common Commands
.
pwd - print (display) the working directory
.
cd <dir> - change the current working
directory to dir
.
ls - list the files in the current working
directory
.
ls -l - list the files in the current workingdirectory in long format
04/03/11 19
File Commands File Commands
.
cp <fromfile> <tofile>
.
Copy from the <fromfile> to the <tofile>
.
mv <fromfile> <tofile>
.
Move/rename the <fromfile> to the <tofile>
.
rm <file>
.
Remove the file named <file>
.
mkdir <newdir>
.
Make a new directory called <newdir>
.
rmdir <dir>
.
Remove an (empty) directory
04/03/11 20
More Commands More Commands
.
who
.
List who is currently logged on to the system
.
whoami
.
Report what user you are logged on as
.
ps
.
List your processes on the system
.
echo “A string to be echoed”
.
Echo a string (or list of arguments) to the terminal
04/03/11 21
Help Facilities forHelp Facilities for
Commands
To understand the working of the command and possible
options use (man command)
Using the GNU Info System (info, info command)
Listing a Description of a Program (whatis command)
Many tools have a long-style option, `--help , that outputs
usage information about the tool, including the options
and arguments the tool takes. Ex: whoami --help
04/03/11 22
Shell Programming
You can write shell programs by creating scripts
containing a series of shell commands.
Shell Programming
You can write shell programs by creating scripts
containing a series of shell commands.
The first line of the script should start with #! whichindicates to the kernel that the script is directly
executable.
You immediately follow this with the name of the shell,
or program (spaces are allowed), to execute, using thefull path name. So to set up a Bourne shell script thefirst line would be: #! /bin/sh
04/03/11 23
Shell Programming
The first line is followed by commands
Within the scripts # indicates a comment from that pointuntil the end of the line, with #! being a special case iffound as the first characters of the file.
#!/bin/bash
cd /tmp
mkdir t
You also need to specify that the script is executable bysetting the proper bits on the file with chmod, e.g.:
$ chmod +x shell_script
04/03/11 24
Shell Scripting
.Shell scripting is the most useful and
powerful feature in Linux
.Minimizes typing of repetitive command
.Can schedule jobs to run in the system
.Can initiate back up activities for systemadministration
.Similar to batch files in DOS, but more powerful
than Batch files
04/03/11 25
Shell Scripting
.Shell scripting is the most useful and
powerful feature in Linux
.Minimizes typing of repetitive command
.Can schedule jobs to run in the system
.Can initiate back up activities for systemadministration
.Similar to batch files in DOS, but more powerful
than Batch files
04/03/11 25
Working with shell script
.Open a file with extension .sh using vi editor
.We can type any number of commands that
we use to type at command prompt
.Save the file
.Execute the file
.sh file.sh
../file.sh (if the file has execution permission)
04/03/11 26
Working with shell script
.Open a file with extension .sh using vi editor
.We can type any number of commands that
we use to type at command prompt
.Save the file
.Execute the file
.sh file.sh
../file.sh (if the file has execution permission)
04/03/11 26
Shell Scripts
.To Print a line
.echo “Hello World” (Prints Hello World in the screen)
.To read a line
.read n (Stores the content entered by user in
variable n
.To Comment a line
.# This is a comment
.Only single line comment is available. For multi linecomment, we need to use # symbol in lines which
we want to comment.
04/03/11 27
Shell Scripts
.To Print a line
.echo “Hello World” (Prints Hello World in the screen)
.To read a line
.read n (Stores the content entered by user in
variable n
.To Comment a line
.# This is a comment
.Only single line comment is available. For multi linecomment, we need to use # symbol in lines which
we want to comment.
04/03/11 27
#!/bin/bash
while
true
do
cat somefile > /dev/null
echo .
done
/* */
do forever
‘PIPE < SOME FILE | hole’
say ‘.’
end
04/03/11 28
Text editors
.Vi
.Emacs
.gEdit
.kWrite
.TextPad
.And more…
04/03/11 29
Text editors
.Vi
.Emacs
.gEdit
.kWrite
.TextPad
.And more…
04/03/11 29
VI Editor
.Popular text editor
.Just type vi <<filename>> at the prompt
and hit the enter key.
.A new file will be opened
.Type the contents needed and save
.To save, press the Esc Key and then press :
(colon) w q and then enter
.To quit with out saving Esc + : + q and then
enter
04/03/11 30
VI Editor
.Popular text editor
.Just type vi <<filename>> at the prompt
and hit the enter key.
.A new file will be opened
.Type the contents needed and save
.To save, press the Esc Key and then press :
(colon) w q and then enter
.To quit with out saving Esc + : + q and then
enter
04/03/11 30
Vi editor
.Navigation
Left alt+- h
Down alt+ j
Up alt+ k
Right alt+ l
.Top of the screen – H (shift + h) //caps lock will not work
.Middle of the screen – M (shift + m)
.Bottom of the screen – L (shift + l)
.$ - End Key, 0 – Home Key
.Edit Commands
.Cut – X, x
.Copy – yy, yw
.Paste – P, p
04/03/11 31
Vi editor
.Navigation
Left alt+- h
Down alt+ j
Up alt+ k
Right alt+ l
.Top of the screen – H (shift + h) //caps lock will not work
.Middle of the screen – M (shift + m)
.Bottom of the screen – L (shift + l)
.$ - End Key, 0 – Home Key
.Edit Commands
.Cut – X, x
.Copy – yy, yw
.Paste – P, p
04/03/11 31
Wild Characters Wild Characters
You can use three types of wildcards in bash:
.
The asterisk (*) character matches zero or
more characters in a file name. That means *
denotes all files in a directory.
.
The question mark (?) matches any singlecharacter. If you type test?, that matches anyfive-character text that begins with test.
.
A set of characters in brackets matches anysingle character from that set. The string [aB]
*, for example, matches any filename that
starts with a or B.
04/03/11 32
Example 1:
The file name starts with s.
The filename ends with .h.
.
The wildcard specification s*.h denotes all file
names that meet these criteria.
Example 2:
cp /media/cdrom/* .
bash replaces the wildcard character * with the names ofall the files in the /media/cdrom directory. The period
at the end of the command represents the current
directory.
04/03/11 33
Example 3:
Suppose that you have four files — image1.pcx,
image2.pcx, image3.pcx, and image4.pcx —in the
current directory. To copy these files to the /mnt/floppy
directory, use the following command:
cp image?.pcx /media/floppy
bash replaces the single question mark with any single
character and copies the four files to /mnt.
04/03/11 34
Example 4:
To see a list of all filenames in the /etc/X11/xdm
directory that start with x or X, type the following
command:
ls /etc/X11/xdm/[xX]*
04/03/11 35
Example 5:
$ ls ????e
apple grape
$ ls g???e*
grape grapefruit
The first example matches any five-character file that
ends in e (apple, grape). The second matches any filethat begins with g and has e as its fifth character
(grape, grapefruit).
04/03/11 36
Example 6:
Here are a couple of examples using braces to do
pattern matching:
$ ls [abw]*
apple banana watermelon
$ ls [agw]*[ne]
apple grape watermelon
In the first example, any file beginning with a, b, or w ismatched. In the second, any file that begins with a, g,
or w and also ends with either n or e is matched.
04/03/11 37
More About Commands
Exiting the Shell:To exit the shell when you are done, type
exit or press Ctrl+D.
Several Commands can be found at several directories:
1- /bin
2- /usr/bin
3-/sbin
Then use the man command (for example, man hostname) to
see what each command does.
To view your history list, use the history command.
04/03/11 38