Introduction
This tutorial is designed for beginners only and This tutorial explains the basics of
shell programming by showing some examples of shell programs. Its not help or
manual for the shell. While reading this tutorial you can find manual quite useful (
type man bash at $ prompt to see manual pages). Manual contains all necessary
information you need, but it won't have that much examples, which makes idea
more clear. For that reason, this tutorial contains examples rather than all the
features of shell. I assumes you have at least working knowledge of Linux i.e. basic
commands like how to create, copy, remove files/directories etc or how to use
editor like vi or mcedit and login to your system. Before Starting Linux Shell Script
Programming you must know
l Kernel
l Shell
l Process
l Redirectors, Pipes, Filters etc.
What's Kernel
Kernel is hart of Linux O/S. It manages resource of Linux O/S. Resources means
facilities available in Linux. For eg. Facility to store data, print data on printer,
memory, file management etc . Kernel decides who will use this resource, for how
long and when. It runs your programs (or set up to execute binary files) It's
Memory resident portion of Linux. It performance following task :-
l I/O management
l Process management
l Device management
l File management
l Memory management
What's Linux Shell
Computer understand the language of 0's and 1's called binary language, In early
days of computing, instruction are provided using binary language, which is difficult
for all of us, to read and write. So in O/s there is special program called Shell. Shell
accepts your instruction or commands in English and translate it into computers
native binary language.
This is what Shell Does for US
Linux Shell Script Tutorial
You type Your command and shell convert it as
It's environment provided for user interaction. Shell is an command language
interpreter that executes commands read from the standard input device
(keyboard) or from a file. Linux may use one of the following most popular shells
(In MS-DOS, Shell name is COMMAND.COM which is also used for same purpose,
but it's not as powerful as our Linux Shells are!)
Shell Name Developed by Where Remark
BASH ( Bourne-Again
SHell )
Brian Fox and Chet
Ramey
Free Software
Foundation
Most common shell in
Linux. It's Freeware
shell.
CSH (C SHell) Bill Joy
University of California
(For BSD)
The C shell's syntax and
usage are very similar to
the C programming
language.
KSH (Korn SHell) David Korn AT & T Bell Labs
Any of the above shell reads command from user (via Keyboard or Mouse) and tells
Linux O/s what users want. If we are giving commands from keyboard it is called
command line interface ( Usually in-front of $ prompt, This prompt is depend upon
your shell and Environment that you set or by your System Administrator,
therefore you may get different prompt ).
NOTE: To find your shell type following command
$ echo $SHELL
How to use Shell
To use shell (You start to use your shell as soon as you log into your system) you
have to simply type commands. Following is the list of common commands.
Linux Common Commands
NOTE that following commands are for New users or for Beginners only. The
purpose is if you use this command you will be more familiar with your shell and
secondly, you need some of these command in your Shell script. If you want to get
more information or help for this command try following commands For e.g. To see
help or options related with date command try
$ date --help
or To see help or options related with ls command (Here you will screen by screen
help, since help of ls command is quite big that can't fit on single screen )
$ ls --help | more
Syntax: command-name --help
Syntax: man command-name
Syntax: info command-name
See what happened when you type following
$ man ls
$ info bash
NOTE: In MS-DOS, you get help by using /? clue or by typing help command as
C:\> dir /?
C:\> date /?
C:\> help time
C:\> help date
C:\> help
Linux Command
For this Purpose Use this Command Syntax
Example ( In front of $
Prompt)
To see date date $ date
To see who's using
system. who $ who
Print working directory pwd $ pwd
List name of files in
current directory ls or dirs $ ls
To create text file
NOTE: Press and hold
CTRL key and press D to
stop or to end file
(CTRL+D)
cat > { file name }
$ cat > myfile
type your text
when done press
^D
To text see files cat {file name } $ cat myfile
To display file one full
screen at a time more {file name } $ more myfile
To move or rename
file/directory mv {file1} {file2} $ mv sales
sales.99
To create multiple file
copies with various link.
After this both oldfile
newfile refers to same
name
ln {oldfile} {newfile} $ ln Page1 Book1
To remove file rm file1 $ rm myfile
Linux Shell Script Tutorial
Remove all files in given
directory/subdirectory.
Use it very carefully.
rm -rf {dirname} $ rm -rf oldfiles
To change file access
permissions
u - User who owns the
file
g - Group file owner
o - User classified as
other
a - All other system user
+ Set permission
- Remove permission
r - Read permission
w - Write permission
x - Execute permission
chmod {u|g|o|a} {+|-} {r|w|x} {filename}
$ chmod
u+x,g+wx,o+x myscript
NOTE: This command set
permission for file called
'myscript' as User (Person
who creates that file or
directory) has execute
permission (u+x) Group of
file owner can write to this
file as well as execute this
file (g+wx) Others can
only execute file but can
not modify it, Since we
have not given w (write
permission) to them. (o+x).
Read your mail. mail $ mail
To See more about
currently login person
(i..e. yourself)
who am i $ who am i
To login out logout (OR press CTRL+D)
$ logout
(Note: It may ask
you password type
your login password,
In some case this
feature is disabled by
System
Administrator)
Send mail to other person mail {user-name} $ mail ashish
To count lines, words and
characters of given file
wc {file-name} $wc myfile
To searches file for line
that match a pattern.
grep {word-to-lookup} {filename} $ grep fox
myfile
To sort file in following
order
-r Reverse normal order
-n Sort in numeric order
-nr Sort in reverse
numeric order
sort -r -n -nr {filename} $sort myfile
Linux Shell Script Tutorial
To print last | first line of
given file
tail - | + { linenumber } {filename} $tail +5 myfile
To Use to compare files
cmp {file1} {file2}
OR
diff {file1} {file2}
$cmp myfile
myfile.old
To print file pr {file-name} $pr myfile
What is Processes
Process is any kind of program or task carried out by your PC. For e.g. $ ls -lR , is
command or a request to list files in a directory and all subdirectory in your current
directory. It is a process. A process is program (command given by user) to
perform some Job. In Linux when you start process, it gives a number (called PID
or process-id), PID starts from 0 to 65535.
Why Process required
Linux is multi-user, multitasking o/s. It means you can run more than two process
simultaneously if you wish. For e.g.. To find how many files do you have on your
system you may give command like
$ ls / -R | wc -l
This command will take lot of time to search all files on your system. So you can
run such command in Background or simultaneously by giving command like
$ ls / -R | wc -l &
The ampersand (&) at the end of command tells shells start command (ls / -R | wc
-l) and run it in background takes next command immediately. An instance of
running command is called process and the number printed by shell is called
process-id (PID), this PID can be use to refer specific running process.
Linux Command Related with Process
For this purpose Use this Command Example
To see currently running
process ps $ ps
To stop any process i.e. to
kill process kill {PID} $ kill 1012
To get information about all
running process ps -ag $ ps -ag
To stop all process except
your shell kill 0 $ kill 0
For background processing
(With &, use to put particular
command and program in
background)
linux-command & $ ls / -R | wc -l &
NOTE that you can only kill process which are created by yourself. A Administrator
can almost kill 95-98% process. But some process can not be killed, such as VDU
Process.
.
Linux Shell Script Tutorial
Redirection of Standard output/input or Input - Output
redirection
Mostly all command gives output on screen or take input from keyboard, but in
Linux it's possible to send output to file or to read input from file. For e.g. $ ls
command gives output to screen; to send output to file of ls give command , $ ls
> filename. It means put output of ls command to filename. There are three main
redirection symbols >,>>,<
(1) > Redirector Symbol
Syntax: Linux-command > filename
To output Linux-commands result to file. Note that If file already exist, it will be
overwritten else new file is created. For e.g. To send output of ls command give
$ ls > myfiles
Now if 'myfiles' file exist in your current directory it will be overwritten without any
type of warning. (What if I want to send output to file, which is already exist and
want to keep information of that file without loosing previous information/data?,
For this Read next redirector)
(2) >> Redirector Symbol
Syntax: Linux-command >> filename
To output Linux-commands result to END of file. Note that If file exist , it will be
opened and new information / data will be written to END of file, without losing
previous information/data, And if file is not exist, then new file is created. For e.g.
To send output of date command to already exist file give
$ date >> myfiles
(3) < Redirector Symbol
Syntax: Linux-command < filename
To take input to Linux-command from file instead of key-board. For e.g. To take
input for cat command give
$ cat < myfiles
Pips
A pipe is a way to connect the output of one program to the input of another
program without any temporary file.
Linux Shell Script Tutorial
A pipe is nothing but a temporary storage place where the output of one command
is stored and then passed as the input for second command. Pipes are used to run
more than two commands ( Multiple commands) from same command line.
Syntax: command1 | command2
Command using Pips Meaning or Use of Pipes
$ ls | more
Here the output of ls command is given as input to
more command So that output is printed one screen
full page at a time
$ who | sort
Here output of who command is given as input to
sort command So that it will print sorted list of
users
$ who | wc -l
Here output of who command is given as input to
wc command So that it will number of user who
logon to system
$ ls -l | wc -l
Here output of ls command is given as input to wc
command So that it will print number of files in
current directory.
$ who | grep raju
Here output of who command is given as input to
grep command So that it will print if particular user
name if he is logon or nothing is printed ( To see
for particular user logon)
Filter
If a Linux command accepts its input from the standard input and produces its
output on standard output is know as a filter. A filter performs some kind of
process on the input and gives output. For e.g.. Suppose we have file called
'hotel.txt' with 100 lines data, And from 'hotel.txt' we would like to print contains
from line number 20 to line number 30 and store this result to file called 'hlist' then
give command
$ tail +20 < hotel.txt | head -n30 >hlist
This tutorial is designed for beginners only and This tutorial explains the basics of
shell programming by showing some examples of shell programs. Its not help or
manual for the shell. While reading this tutorial you can find manual quite useful (
type man bash at $ prompt to see manual pages). Manual contains all necessary
information you need, but it won't have that much examples, which makes idea
more clear. For that reason, this tutorial contains examples rather than all the
features of shell. I assumes you have at least working knowledge of Linux i.e. basic
commands like how to create, copy, remove files/directories etc or how to use
editor like vi or mcedit and login to your system. Before Starting Linux Shell Script
Programming you must know
l Kernel
l Shell
l Process
l Redirectors, Pipes, Filters etc.
What's Kernel
Kernel is hart of Linux O/S. It manages resource of Linux O/S. Resources means
facilities available in Linux. For eg. Facility to store data, print data on printer,
memory, file management etc . Kernel decides who will use this resource, for how
long and when. It runs your programs (or set up to execute binary files) It's
Memory resident portion of Linux. It performance following task :-
l I/O management
l Process management
l Device management
l File management
l Memory management
What's Linux Shell
Computer understand the language of 0's and 1's called binary language, In early
days of computing, instruction are provided using binary language, which is difficult
for all of us, to read and write. So in O/s there is special program called Shell. Shell
accepts your instruction or commands in English and translate it into computers
native binary language.
This is what Shell Does for US
Linux Shell Script Tutorial
You type Your command and shell convert it as
It's environment provided for user interaction. Shell is an command language
interpreter that executes commands read from the standard input device
(keyboard) or from a file. Linux may use one of the following most popular shells
(In MS-DOS, Shell name is COMMAND.COM which is also used for same purpose,
but it's not as powerful as our Linux Shells are!)
Shell Name Developed by Where Remark
BASH ( Bourne-Again
SHell )
Brian Fox and Chet
Ramey
Free Software
Foundation
Most common shell in
Linux. It's Freeware
shell.
CSH (C SHell) Bill Joy
University of California
(For BSD)
The C shell's syntax and
usage are very similar to
the C programming
language.
KSH (Korn SHell) David Korn AT & T Bell Labs
Any of the above shell reads command from user (via Keyboard or Mouse) and tells
Linux O/s what users want. If we are giving commands from keyboard it is called
command line interface ( Usually in-front of $ prompt, This prompt is depend upon
your shell and Environment that you set or by your System Administrator,
therefore you may get different prompt ).
NOTE: To find your shell type following command
$ echo $SHELL
How to use Shell
To use shell (You start to use your shell as soon as you log into your system) you
have to simply type commands. Following is the list of common commands.
Linux Common Commands
NOTE that following commands are for New users or for Beginners only. The
purpose is if you use this command you will be more familiar with your shell and
secondly, you need some of these command in your Shell script. If you want to get
more information or help for this command try following commands For e.g. To see
help or options related with date command try
$ date --help
or To see help or options related with ls command (Here you will screen by screen
help, since help of ls command is quite big that can't fit on single screen )
$ ls --help | more
Syntax: command-name --help
Syntax: man command-name
Syntax: info command-name
See what happened when you type following
$ man ls
$ info bash
NOTE: In MS-DOS, you get help by using /? clue or by typing help command as
C:\> dir /?
C:\> date /?
C:\> help time
C:\> help date
C:\> help
Linux Command
For this Purpose Use this Command Syntax
Example ( In front of $
Prompt)
To see date date $ date
To see who's using
system. who $ who
Print working directory pwd $ pwd
List name of files in
current directory ls or dirs $ ls
To create text file
NOTE: Press and hold
CTRL key and press D to
stop or to end file
(CTRL+D)
cat > { file name }
$ cat > myfile
type your text
when done press
^D
To text see files cat {file name } $ cat myfile
To display file one full
screen at a time more {file name } $ more myfile
To move or rename
file/directory mv {file1} {file2} $ mv sales
sales.99
To create multiple file
copies with various link.
After this both oldfile
newfile refers to same
name
ln {oldfile} {newfile} $ ln Page1 Book1
To remove file rm file1 $ rm myfile
Linux Shell Script Tutorial
Remove all files in given
directory/subdirectory.
Use it very carefully.
rm -rf {dirname} $ rm -rf oldfiles
To change file access
permissions
u - User who owns the
file
g - Group file owner
o - User classified as
other
a - All other system user
+ Set permission
- Remove permission
r - Read permission
w - Write permission
x - Execute permission
chmod {u|g|o|a} {+|-} {r|w|x} {filename}
$ chmod
u+x,g+wx,o+x myscript
NOTE: This command set
permission for file called
'myscript' as User (Person
who creates that file or
directory) has execute
permission (u+x) Group of
file owner can write to this
file as well as execute this
file (g+wx) Others can
only execute file but can
not modify it, Since we
have not given w (write
permission) to them. (o+x).
Read your mail. mail $ mail
To See more about
currently login person
(i..e. yourself)
who am i $ who am i
To login out logout (OR press CTRL+D)
$ logout
(Note: It may ask
you password type
your login password,
In some case this
feature is disabled by
System
Administrator)
Send mail to other person mail {user-name} $ mail ashish
To count lines, words and
characters of given file
wc {file-name} $wc myfile
To searches file for line
that match a pattern.
grep {word-to-lookup} {filename} $ grep fox
myfile
To sort file in following
order
-r Reverse normal order
-n Sort in numeric order
-nr Sort in reverse
numeric order
sort -r -n -nr {filename} $sort myfile
Linux Shell Script Tutorial
To print last | first line of
given file
tail - | + { linenumber } {filename} $tail +5 myfile
To Use to compare files
cmp {file1} {file2}
OR
diff {file1} {file2}
$cmp myfile
myfile.old
To print file pr {file-name} $pr myfile
What is Processes
Process is any kind of program or task carried out by your PC. For e.g. $ ls -lR , is
command or a request to list files in a directory and all subdirectory in your current
directory. It is a process. A process is program (command given by user) to
perform some Job. In Linux when you start process, it gives a number (called PID
or process-id), PID starts from 0 to 65535.
Why Process required
Linux is multi-user, multitasking o/s. It means you can run more than two process
simultaneously if you wish. For e.g.. To find how many files do you have on your
system you may give command like
$ ls / -R | wc -l
This command will take lot of time to search all files on your system. So you can
run such command in Background or simultaneously by giving command like
$ ls / -R | wc -l &
The ampersand (&) at the end of command tells shells start command (ls / -R | wc
-l) and run it in background takes next command immediately. An instance of
running command is called process and the number printed by shell is called
process-id (PID), this PID can be use to refer specific running process.
Linux Command Related with Process
For this purpose Use this Command Example
To see currently running
process ps $ ps
To stop any process i.e. to
kill process kill {PID} $ kill 1012
To get information about all
running process ps -ag $ ps -ag
To stop all process except
your shell kill 0 $ kill 0
For background processing
(With &, use to put particular
command and program in
background)
linux-command & $ ls / -R | wc -l &
NOTE that you can only kill process which are created by yourself. A Administrator
can almost kill 95-98% process. But some process can not be killed, such as VDU
Process.
.
Linux Shell Script Tutorial
Redirection of Standard output/input or Input - Output
redirection
Mostly all command gives output on screen or take input from keyboard, but in
Linux it's possible to send output to file or to read input from file. For e.g. $ ls
command gives output to screen; to send output to file of ls give command , $ ls
> filename. It means put output of ls command to filename. There are three main
redirection symbols >,>>,<
(1) > Redirector Symbol
Syntax: Linux-command > filename
To output Linux-commands result to file. Note that If file already exist, it will be
overwritten else new file is created. For e.g. To send output of ls command give
$ ls > myfiles
Now if 'myfiles' file exist in your current directory it will be overwritten without any
type of warning. (What if I want to send output to file, which is already exist and
want to keep information of that file without loosing previous information/data?,
For this Read next redirector)
(2) >> Redirector Symbol
Syntax: Linux-command >> filename
To output Linux-commands result to END of file. Note that If file exist , it will be
opened and new information / data will be written to END of file, without losing
previous information/data, And if file is not exist, then new file is created. For e.g.
To send output of date command to already exist file give
$ date >> myfiles
(3) < Redirector Symbol
Syntax: Linux-command < filename
To take input to Linux-command from file instead of key-board. For e.g. To take
input for cat command give
$ cat < myfiles
Pips
A pipe is a way to connect the output of one program to the input of another
program without any temporary file.
Linux Shell Script Tutorial
A pipe is nothing but a temporary storage place where the output of one command
is stored and then passed as the input for second command. Pipes are used to run
more than two commands ( Multiple commands) from same command line.
Syntax: command1 | command2
Command using Pips Meaning or Use of Pipes
$ ls | more
Here the output of ls command is given as input to
more command So that output is printed one screen
full page at a time
$ who | sort
Here output of who command is given as input to
sort command So that it will print sorted list of
users
$ who | wc -l
Here output of who command is given as input to
wc command So that it will number of user who
logon to system
$ ls -l | wc -l
Here output of ls command is given as input to wc
command So that it will print number of files in
current directory.
$ who | grep raju
Here output of who command is given as input to
grep command So that it will print if particular user
name if he is logon or nothing is printed ( To see
for particular user logon)
Filter
If a Linux command accepts its input from the standard input and produces its
output on standard output is know as a filter. A filter performs some kind of
process on the input and gives output. For e.g.. Suppose we have file called
'hotel.txt' with 100 lines data, And from 'hotel.txt' we would like to print contains
from line number 20 to line number 30 and store this result to file called 'hlist' then
give command
$ tail +20 < hotel.txt | head -n30 >hlist
https://www.facebook.com/Hand-made-genuine-leatherh-and-made-handicrafts-and-old-coin-on-amazon20-395982754135737/?ref=bookmarks