(ebook) Linux Shell Scripting Tutorial.pdf

(169 KB) Pobierz
Linux Shell Scripting Tutorial
Linux Shell Scripting Tutorial
Linux Shell Scripting Tutorial Ver. 1.0
Written by Vivek G Gite
I N D E X
m
m
Shell
m
How to use Shell
m
Common Linux Command Introduction
l
Why Process required
m
m
Linux commands related with process
l
m
m
m
Filters
l
Variables in Linux
m
m
How to define User defined variables
m
m
How to print or access value of UDV (User defined variables)
m
How to write shell script
m
How to Run Shell Scripts
m
Quotes in Shell Scripts
http://www.freeos.com/guides/lsst/index.htm (1 of 2) [17/08/2001 17.41.52]
l
56112887.003.png 56112887.004.png
Linux Shell Scripting Tutorial
m
Shell Arithmetic
m
Command Line Processing (Command Line Arguments)
m
Why Command Line arguments required
m
Exit Status
m
Programming Commands
echo command
Filename Shorthand or meta Characters (i.e. wild cards)
l
m
m
Decision making in shell script ( i.e. if command)
m
test command or [ expr ]
m
Loop in shell scripts
m
The case Statement
m
The read Statement
l
/dev/null - Use to send unwanted output of program
m
m
Local and Global Shell variable (export command)
m
Conditional execution i.e. && and ||
m
I/O Redirection and file descriptors
m
Functions
m
User Interface and dialog utility
m
trap command
m
getopts command
m
More examples of Shell Script (Exercise for You :-)
© 1998-2000 FreeOS.com (I) Pvt. Ltd. All rights reserved.
http://www.freeos.com/guides/lsst/index.htm (2 of 2) [17/08/2001 17.41.52]
Linux Shell Script Tutorial
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
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 :-
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
http://www.freeos.com/guides/lsst/kershell.htm (1 of 5) [17/08/2001 17.42.02]
l
l
56112887.005.png
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 )
http://www.freeos.com/guides/lsst/kershell.htm (2 of 5) [17/08/2001 17.42.02]
56112887.006.png 56112887.001.png
Linux Shell Script Tutorial
$ 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
http://www.freeos.com/guides/lsst/kershell.htm (3 of 5) [17/08/2001 17.42.02]
56112887.002.png
Zgłoś jeśli naruszono regulamin