




























































































Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Prepare-se para as provas
Estude fácil! Tem muito documento disponível na Docsity
Prepare-se para as provas com trabalhos de outros alunos como você, aqui na Docsity
Encontra documentos específicos para os exames da tua universidade
Prepare-se com as videoaulas e exercícios resolvidos criados a partir da grade da sua Universidade
Responda perguntas de provas passadas e avalie sua preparação.
Ganhe pontos para baixar
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
linux - shell scripting tutorial - a beginner's handbook
Tipologia: Notas de estudo
1 / 272
Esta página não é visível na pré-visualização
Não perca as partes importantes!





























































































Written by Vivek G. Gite.
Cyberciti Computers & nixCraft, Pune, INDIA.
Copyright © 1999-2002 by Vivek G. Gite
(Formally know as www.vivek-tech.com )
Chapter 1: Quick Introduction to Linux
What Linux is? Who developed the Linux? How to get Linux? How to Install Linux Where I can use Linux? What Kernel Is? What is Linux Shell? How to use Shell What is Shell Script? Why to Write Shell Script? More on Shell...
Chapter 2: Getting started with Shell Programming
How to write shell script Variables in shell How to define User defined variables (UDV) Rules for Naming variable name (Both UDV and System Variable) How to print or access value of UDV (User defined variables) echo Command
Linux Shell Scripting Tutorial v1.05r3 - A Beginner's handbook
http://www.cyberciti.biz/pdf/lsst/index.html (1 of 5) [7/29/2002 6:50:01 PM]
getopts command
Chapter 5: Essential Utilities for Power User
Preparing for Quick Tour of essential utilities Selecting portion of a file using cut utility Putting lines together using paste utility The join utility Translating range of characters using tr utility Data manipulation using awk utility sed utility - Editing file without using editor Removing duplicate lines from text database file using uniq utility Finding matching pattern using grep utility
Chapter 6: Learning expressions with ex
Getting started with ex Printing text on-screen Deleting lines Copying lines Searching the words Find and Replace (Substituting regular expression) Replacing word with confirmation from user Finding words Using range of characters in regular expressions Using & as Special replacement character Converting lowercase character to uppercase
Chapter 7: awk Revisited
Getting Starting with awk Predefined variables of awk Doing arithmetic with awk User Defined variables in awk Use of printf statement Use of Format Specification Code if condition in awk Loops in awk Real life examples in awk awk miscellaneous sed - Quick Introduction Redirecting the output of sed command How to write sed scripts? More examples of sed
Chapter 8: Examples of Shell Scripts
Linux Shell Scripting Tutorial v1.05r3 - A Beginner's handbook
http://www.cyberciti.biz/pdf/lsst/index.html (3 of 5) [7/29/2002 6:50:01 PM]
Logic Development: Shell script to print given numbers sum of all digit Shell script to print contains of file from given line number to next given number of lines Shell script to say Good morning/Afternoon/Evening as you log in to system Shell script to find whether entered year is Leap or not Sort the given five number in ascending order (use of array) Command line (args) handling: Adding 2 nos. suppiled as command line args Calculating average of given numbers on command line args Finding out biggest number from given three nos suppiled as command line args Shell script to implement getopts statement. Basic math Calculator (case statement) Loops using while & for loop: Print nos. as 5,4,3,2,1 using while loop Printing the patterns using for loop. Arithmetic in shell scripting: Performing real number calculation in shell script Converting decimal number to hexadecimal number Calculating factorial of given number File handling: Shell script to determine whether given file exist or not. Screen handling/echo command with escape sequence code: Shell script to print "Hello World" message, in Bold, Blink effect, and in different colors like red, brown etc. Background process implementation: Digital clock using shell script User interface and Functions in shell script: Shell script to implements menu based system. System Administration: Getting more information about your working environment through shell script Shell script to gathered useful system information such as CPU, disks, Ram and your environment etc. Shell script to add DNS Entery to BIND Database with default Nameservers, Mail Servers (MX) and host Integrating awk script with shell script: Script to convert file names from UPPERCASE to lowercase file names or vice versa.
Chapter 9: Other Resources
Appendix - A : Linux File Server Tutorial (LFST) version b0.1 Rev. 2 Appendix - B : Linux Command Reference (LCR) About the author About this Document
Linux Shell Scripting Tutorial v1.05r3 - A Beginner's handbook
http://www.cyberciti.biz/pdf/lsst/index.html (4 of 5) [7/29/2002 6:50:01 PM]
Linux Shell Scripting Tutorial (LSST) v1.05r Prev (^) Chapter 1: Introduction: Quick Introduction to Linux Next
This tutorial is designed for beginners who wish to learn the basics of shell scripting/programming plus introduction to power tools such as awk, sed, etc. It is 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 this 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. But not expects any programming language experience. If you have access to Linux, this tutorial will provide you an easy-to-follow introduction to shell scripting.
Many other tutorial and books on Linux shell scripting are either too basic, or skips important intermediate steps. But this tutorial, maintained the balance between these two. It covers the many real life modern example of shell scripting which are almost missed by many other tutorials/documents/books. I have used a hands-on approach in this tutorial. The idea is very clear " do it yourself or learn by doing " i.e. trying things yourself is the best way to learn, so examples are presented as complete working shell scripts, which can be typed in and executed
Chapter 1 to 4 shows most of the useful and important shell scripting concepts. Chapter 5 introduction to tools & utilities which can be used while programming the Linux shell smartly. Chapter 6 and 7 is all about expression and expression mostly used by tools such as sed and awk. Chapter 8 is loaded with tons of shell scripting examples divided into different categories. Chapter 9 gives more resources information which can be used while learning the shell scripting like information on Linux file system, common Linux command reference and other resources.
Chapter 1 introduces to basic concepts such as what is Linux, where Linux can used and continue explaning the shell, shell script and kernel etc.
Chapter 2 shows how to write the shell script and execute them. It explains many basic concepts which requires to write shell script.
LSST v1.05r3 > Chapter 1 > Quick Introduction to Linux
http://www.cyberciti.biz/pdf/lsst/ch01.html (1 of 2) [7/29/2002 6:50:59 PM]
Chapter 3 is all about making decision in shell scripting as well as loops in shell. It explains what expression are, how shell understands the condition/decisions. It also shows you nesting concept for if and for loop statement and debugging of shell script.
Chapter 4 introduces the many advanced shell scripting concepts such as function, user interface, File Descriptors, signal handling, Multiple command line arguments etc.
Chapter 5 introduces to powerful utility programs which can be used variety of purpose while programming the shell.
Chapter 6 and 7 gives more information on patterns, filters, expressions, and off course sed and awk is covered in depth.
Chapter 8 contains lot of example of shell scripting divided into various category such as logic development, system administration etc.
Note that indicates advanced shell scripting concepts, you can skip this if you are really new to Linux or Programming, though this is not RECOMMENDED by me.
I hope you get as much pleasure reading this tutorial, as I had writing it. After reading this tutorial if you are able to write your own powerful shell scripts, then I think the purpose of writing this tutorial is served and finally if you do get time after reading this tutorial drop me an e-mail message about your comment/suggestion/questions and off course bugs (errors) you find regarding this tutorial.
Prev Home Next Up (^) What Linux is?
LSST v1.05r3 > Chapter 1 > Quick Introduction to Linux
http://www.cyberciti.biz/pdf/lsst/ch01.html (2 of 2) [7/29/2002 6:50:59 PM]
Linux Shell Scripting Tutorial (LSST) v1.05r Prev (^) Chapter 1: Introduction: Quick Introduction to Linux Next
In 1991, Linus Torvalds studying Unix at the University, where he used special educational experimental purpose operating system called Minix (small version of Unix and used in Academic environment). But Minix had it's own limitations. Linus felt he could do better than the Minix. So he developed his own version of Minix, which is now know as Linux. Linux is Open Source From the start of the day. For more information on Linus Torvalds, please visit his home page.
Prev Home Next What Linux is? Up^ How to get Linux?
LSST v1.05r3 > Chapter 1 > Who developed the Linux?
http://www.cyberciti.biz/pdf/lsst/ch01sec02.html [7/29/2002 6:51:08 PM]
Linux Shell Scripting Tutorial (LSST) v1.05r Prev (^) Chapter 1: Introduction: Quick Introduction to Linux Next
Linux available for download over the net, this is useful if your internet connection is fast. Another way is order the CD-ROMs which saves time, and the installation from CD-ROM is fast/automatic. Various Linux distributions available. Following are important Linux distributions.
Linux distributions. Website/Logo
Red Hat Linux: http://www.redhat.com/
SuSE Linux: http://www.suse.com/
Mandrake Linux: http://www.mandrakesoft.com/
Caldera Linux: http://www.calderasystems.com/
Debian GNU/Linux: http://www.debian.org/
Slackware Linux: http://www.slackware.com/
Note: If you are in India then you can get Linux Distribution from the Leading Computer magazine such as PC Quest (Even PCQuest has got its own Linux flavour) or if you are in Pune, India please visit our web site to obtained the Red Hat Linux or any other official Linux distribution. Note that you can also obtained your Linux distribution with Linux books which you purchase from local book store.
LSST v1.05 > Chapter 1 > How to get Linux?
http://www.cyberciti.biz/pdf/lsst/ch01sec03.html (1 of 2) [7/29/2002 6:51:11 PM]
Linux Shell Scripting Tutorial (LSST) v1.05r Prev (^) Chapter 1: Introduction: Quick Introduction to Linux Next
Please visit the LESSBS Project home page for Quick Visual Installation Guide for Red Hat Linux version 6.2 and 7.2.
Prev Home Next How to get Linux? Up^ Where I can use Linux?
LSST v1.05 > Chapter 1 > How to Install Linux?
http://www.cyberciti.biz/pdf/lsst/ch01sec04.html [7/29/2002 6:51:22 PM]
Linux Shell Scripting Tutorial (LSST) v1.05r Prev (^) Chapter 1: Introduction : Linux Shell basics Next
You can use Linux as Server Os or as stand alone Os on your PC. (But it is best suited for Server.) As a server Os it provides different services/network resources to client. Server Os must be:
l Stable l Robust l Secure l High Performance
Linux offers all of the above characteristics plus its Open Source and Free OS. So Linux can be used as:
(1) On stand alone workstation /PC for word processing, graphics, software development, internet, e-mail, chatting, small personal database management system etc. (2) In network environment as: (A) File and Print or Application Server Share the data, Connect the expensive device like printer and share it, e-mail within the LAN/intranet etc are some of the application.
Linux Server with different Client Os
(B) Linux sever can be connected to Internet, So that PC's on intranet can share the internet/e-mail etc. You can put your web sever that run your web site or transmit the information on the internet.
LSST v1.05 > Chapter 1 > Where I can use Linux?
http://www.cyberciti.biz/pdf/lsst/ch01sec05.html (1 of 2) [7/29/2002 6:51:26 PM]
Linux Shell Scripting Tutorial (LSST) v1.05r Prev (^) Chapter 1: Introduction : Linux Shell basics Next
Kernel is heart of Linux Os.
It manages resource of Linux Os. Resources means facilities available in Linux. For e.g. 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).
The kernel acts as an intermediary between the computer hardware and various programs/application/shell.
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
Prev Home Next Where I can use Linux? Up^ What is Linux Shell?
LSST v1.05 > Chapter 1 > LSST v1.05 > Chapter 1 >
http://www.cyberciti.biz/pdf/lsst/ch01sec06.html [7/29/2002 6:51:28 PM]
Linux Shell Scripting Tutorial (LSST) v1.05r Prev (^) Chapter 1: Introduction : Linux Shell basics Next
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 Os there is special program called Shell. Shell accepts your instruction or commands in English (mostly) and if its a valid command, it is passed to kernel.
Shell is a user program or it's a 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.
Shell is not part of system kernel, but uses the system kernel to execute programs, create files etc.
Several shell available with Linux including:
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 -- TCSH See the man page. Type $ man tcsh
-- TCSH is an enhanced but completely compatible version of the Berkeley UNIX C shell (CSH).
Tip: To find all available shells in your system type following command: $ cat /etc/shells
Note that each shell does the same job, but each understand a different command syntax and provides different built-in functions.
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!
Any of the above shell reads command from user (via Keyboard or Mouse) and tells Linux Os 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 ).
LSST v1.05 > Chapter 1 > What is Linux Shell?
http://www.cyberciti.biz/pdf/lsst/ch01sec07.html (1 of 2) [7/29/2002 6:51:30 PM]
Linux Shell Scripting Tutorial (LSST) v1.05r Prev (^) Chapter 1: Introduction : Linux Shell basics Next
To use shell (You start to use your shell as soon as you log into your system) you have to simply type commands.
See common Linux Command for syntax and example, this can be used as quick reference while programming the shell.
Prev Home Next What is Linux Shell? Up^ What is Shell Script?
LSST v1.05 > Chapter 1 > How to use Shell
http://www.cyberciti.biz/pdf/lsst/ch01sec08.html [7/29/2002 6:51:31 PM]
Linux Shell Scripting Tutorial (LSST) v1.05r Prev (^) Chapter 1: Introduction : Linux Shell basics Next
Normally shells are interactive. It means shell accept command from you (via keyboard) and execute them. But if you use command one by one (sequence of 'n' number of commands) , the you can store this sequence of command to text file and tell the shell to execute this text file instead of entering the commands. This is know as shell script.
Shell script defined as: " Shell Script is series of command written in plain text file. Shell script is just like batch file is MS-DOS but have more power than the MS-DOS batch file. "
Prev Home Next How to use Shell Up^ Why to Write Shell Script?
LSST v1.05 > Chapter 1 > What is Shell Script?
http://www.cyberciti.biz/pdf/lsst/ch01sec09.html [7/29/2002 6:51:33 PM]