Shell Scripting
In 8 Hours
For Beginners
Learn Coding Fast
Ray Yao
Copyright 2015 by Ray Yao
All Rights Reserved
Neither part of this book nor whole of this book may be reproduced or transmitted in any form or by any means electronic, photographic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without prior written permission from the author . All rights reserved !
Ray Yao
About the Author: Ray Yao
Certified PHP engineer by Zend, USA
Certified JAVA programmer by Sun, USA
Certified SCWCD developer by Oracle, USA
Certified A+ professional by CompTIA, USA
Certified ASP . NET expert by Microsoft, USA
Certified MCP professional by Microsoft, USA
Certified TECHNOLOGY specialist by Microsoft, USA
Certified NETWORK+ professional by CompTIA, USA
www . amazon . com/author/ray-yao
Recommended Books on Amazon
Advanced C++ in 8 Hours
Advanced Java in 8 Hours
AngularJs in 8 Hours
Asp . net Programming
Awk in 8 Hours
BootStrap in 8 Hours
C# Interview Q&A
C# Programming
C++ Interview Q&A
C++ Programming
Dart in 8 Hours
Django in 8 Hours
Erlang in 8 Hours
Go in 8 Hours
Html Css Interview Q&A
Html Css Programming
Java Interview Q&A
Java Programming
JavaScript Interview Q&A
JavaScript Programming
JQuery Interview Q&A
JQuery Programming
Jsp Servlets Programming
Kotlin in 8 Hours
Linux Command Line
Linux Interview Q&A
Lua in 8 Hours
Matlab in 8 Hours
MySql in 8 Hours
Node . Js in 8 Hours
Numpy in 8 Hours
Perl in 8 Hours
Php Interview Q&A
Php MySql Programming
PowerShell in 8 Hours
Python Interview Q&A
Python Programming
R Programming
React . Js in 8 Hours
Ruby Programming
Rust in 8 Hours
Scala in 8 Hours
Shell Scripting in 8 Hours
Swift in 8 Hours
Tcl in 8 Hours
TypeScript in 8 Hours
Visual Basic Interview Q&A
Visual Basic Programming
Vue . Js in 8 Hours
Xml Json in 8 Hours
Preface
Linux Shell Scripting in 8 Hours & Exercises covers all essential Linux Shell language knowledge . You can learn complete primary skills of Linux Shell Scripting fast and easily .
The book includes more than 60 practical examples for beginners and includes exercises for the college exam, the engineer certification exam, and the job interview exam .
Note:
This book is only for beginners, it is not suitable for experienced programmers .
Table of Content
Hour 1
What is Linux Shell?
The shell is the interface between the user and Linux; every command you type at the prompt is interpreted by the shell and passed to the Linux kernel .
The shell is a command-language interpreter with its own built-in shell command collection . In addition, the shell can be called by any other valid Linux utilities and application programs in the system .
Whenever you type a command, it is interpreted by the Linux shell .
For example :
The command of printing current working directory (PWD) is contained within Linux bash .
The copy commands (cp) and the move commands (rm) are two single programs that exist in a directory on the file system .
The shell first checks to see if the command is an internal command, and then checks to see if it is an application, either of Linux's own utilities, such as ls and rm, or of a purchased commercial program, such as xv and ghostview .
Another important feature of the shell is that it is an interpreted programming language that supports most data structures, such as loops, functions, variables, and arrays .
Shell Scripting
A Shell script is a scripting program written for the Shell .
Shell Working Environment
Shell scripting, like Java, C++ programming, requires only a text editor to write code and a script interpreter to interpret execution
Various Linux Shell
Linux has different types of shells, such as :
- Bourne Shel l /bin/s h
- Bourne Again Shel l /bin/bas h
- C Shel l /usr/bin/cs h
- K Shel l /usr/bin/ks h
- Shell for Roo t /sbin/s h
This book talks about Bash (Bourne Again Sell) . Bash is the default Shell on most Linux systems .
The First Shell Scripting
Example 1.1
- Open the Vi text editor with a vi command .
- Press i key, enter the Insert Mode .
- Type the following code in the Vi editor :
# ! /bin/bash echo "Hello World ! " |
- Press the Esc key to quit the Insert Mode .
- Type :wq study.sh to save the file . The file name is study . sh, the extension name is . sh .
- Run this Shell program with the following commands :
bash study.sh
Output:
Hello World !
Explanation:
vi is a command to open the Vi editor .
i key is used to enter the Insert Mode in Vi editor .
Insert Mode can be used for writing and editing Shell Scripts .
# ! /bin/bash let Shell system know that using Bash to interpret the scripting .
echo "Hello World ! " shows the content Hello World !
Esc key is used to quit the Insert Mode in Vi editor .
: wq file_name saves the Shell file, and quit from Vi .
bash study . sh executes the Shell file study . sh .
bash myFile.sh executes the Shell file myFile.sh.
Shell Comment
# symbol is used for the Shell comments .
Shell interpreter always ignores the Shell comments
Example 1.2
# ! /bin/bash echo "Hello World ! " # output Hello World! echo "Hi, Friends ! " # output Hi, Friends! |
Output:
Hello World !
Hi, Friends !
Explanation:
# output Hello World ! is a comment of Shell scripting .
# output Hi, Friends ! is a comment of Shell scripting .
Multi-Line Comments
: < comment 1 comment 2 comment 3 ... EOF |
: < .
Example 1.3
# ! /bin/bash echo "Hello World ! " :< This is a "Hello World" program . This is a Shell scripting . This is a sample of multi-line comment . ...... EOF echo "This is a sample of multi-line comment . " |
Output:
Hello World !
This is a sample of multi-line comment .
Explanation:
:< is used for multi-line comments .