Difference between revisions of "BASH"

From Script | Spoken-Tutorial
Jump to: navigation, search
(BASH Shell Scripting)
 
(94 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Linux is one of the most popular Operating Systems used in today's world. Linux refers to the family of Unix-like computer operating systems using the Linux kernel. Linux can be installed on a wide variety of computer hardware, ranging from mobile phones, tablet computers and video game consoles to mainframes and supercomputers.
+
'''Bash: '''
 +
Bash is the shell, or command language interpreter, that will appear in the '''GNU operating system'''.  
 +
Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh).
 +
It offers functional improvements over sh for both programming and interactive use.  
  
It is an opensource software and the Linux kernel is released under the GNU General Public License and hence can be freely created, modified and distributed.
+
In addition, most sh scripts can be run by Bash without modification.
 +
The improvements offered by Bash include: Command line editing, Unlimited size command history, Job Control, Shell Functions and Aliases, Indexed arrays of unlimited size, Integer arithmetic.
  
Linux is actually just a kernel. Many people have put together distributions (often called flavors), that contain not just the kernel but also many other programming tools and utilities. Some well-known distributions include Red Hat Linux, Ubuntu, SuSE Linux, and Debian GNU/Linux.
+
'''Bash Scripting:'''
 +
In addition to the interactive mode, where the user types one command at a time, with immediate execution and feedback, Bash also has the ability to run an entire script of commands, known as a "Bash shell script". A script might contain just a very simple list of commands or even just a single command or it might contain functions, loops, conditional constructs. In effect, a Bash shell script is a computer program written in the Bash programming language.
  
The real power of Linux can be tapped by using its wide and powerful storehouse of commands which need to be typed in on the terminal. The reason behind this is the fact that Linux can trace its intellectual heritage, if not its source code, to the Unix OS. Unix was developed much before GUI environments were dreamt of. Thus, Unix (and hence Linux) provides a wide array of flexible text-mode commands.
+
Shell scripts are commonly used for many system administration tasks, such as performing disk backups, evaluating system logs, and so on. They are also commonly used as installation scripts for complex programs. They are particularly suited to all of these because they allow complexity without requiring it.
  
In this tutorial we would mainly concentrate on how to use the wide variety of commands of Linux to handle files,directories,processes etc. These tutorials are created using Ubuntu version 9.04 and above.  Please see the associated text box of individual spoken tutorials on the website to decide the versions of Linux OS to which it is applicable.
 
 
The Spoken Tutorial Effort for Linux is being contributed by Ms. Ashwini Patil and Lavitha  from IIT Bombay and Mr. Sachin Patil.
 
  
 +
In this tutorial we would mainly concentrate on how to use the wide variety of commands of Linux commands to handle files, directories, processes etc. These tutorials are created using '''Ubuntu version 12.04''' and above.  Please see the associated text box of individual spoken tutorials on the website to decide the versions of Linux OS to which it is applicable.
 
<blockquote style="background-color: lemonchiffon; border: solid thin grey;">
 
<blockquote style="background-color: lemonchiffon; border: solid thin grey;">
'''Linux Slide Template''' [[Media:linux_template.zip | (TEX Format)]]  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
+
The contributors who helped create the outline, transcribe, create the tutorials are - Sachin Patil, Lavitha Monisha Pereira, Ashwini Patil, Nancy Varkey - all from IIT Bombay. The effort has been moderated and coordinated by the''' Spoken Tutorial''' team and '''FOSSEE team''', IIT Bombay.
[[Glossary_for_Linux | Glossary]]
+
<br>
+
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; [[Media:linux_template_ppt.zip | (PPT Format)]] 
+
<br>
+
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; [[Media:linux_template_odp.zip | (ODP Format)]]
+
 
</blockquote>
 
</blockquote>
 +
 +
 +
'''Learners''': Polytechnic/UG/PG computer hardware students and system administrators will greatly benefit by learning to automate common tasks using BASH.
 +
 +
 
__TOC__
 
__TOC__
  
  
==BASH Shell Scripting==
+
==BASH Shell Scripting: Basic==
 
# Introduction to BASH Shell Scripting   
 
# Introduction to BASH Shell Scripting   
#*The bash shell
+
#*About different types of Shells
#**Bash Shell is a Command language interpreter that executes commands.
+
#*The Bash shell 
#**commands are read from input device.
+
#**input can be your keyboard or from a external file. 
+
 
#*Bash Shell Script
 
#*Bash Shell Script
#**Series of BASH commands written seqentially in plain text file
+
#*Write a Bash Shell script and execute it
#*Hello, World! Tutorial
+
#**creating a simple 'hello_world.sh' file using vim editor.
+
#*Shebang [#!/bin/bash]
+
#**The first line of every script is called a shebang.
+
#**It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash.
+
#*Shell Comments
+
#**adding comments in your BASH programs, to make it more readable using '#'
+
#*echo Statement
+
#**printing a message on the screen using ECHO statement.
+
#*exit status
+
#**terminating your script using EXIT command and returning a numerical value
+
#*Execute a script
+
#**making the script executable using CHMOD command.
+
#*Debug a script
+
#**turning on the debug mode using 'set -x'
+
#*Shell Commands
+
#**Already explained in 'Introduction to linux' section
+
 
# Basics of Shell Scripting  
 
# Basics of Shell Scripting  
#*Variable in a shell
+
#*System variables  
#**System Variable      -These are created and maintained by linux bash shell itself.Commonly used system variables HOSTNAME, HOME, USER.
+
#*User defined variables
#**User Defined Variables-These variables are created and maintained by users.
+
#*Accepting user input via keyboard
#*Variable Declaration
+
#Command Line arguments and quoting
#**Global variable -By default all the variables are global, i.e their values remains the same in and outside the function. 
+
#**Local variable  -To declare variables locally use local.The syntax is local variable=value
+
#*Getting user input via Keyboard
+
#**We can accept input from the keyboard and assing an input value to a user defined variable using 'read' command.
+
 
#*Command Line arguments  
 
#*Command Line arguments  
#**A Command line argument is an arguments passed to a program which is been called.
+
#*Single quote
#*Quoting
+
#*Double quote
#**They are three types of quotes
+
#*Backslash
#***1)Double quote
+
#Globbing and export statements
#***2)Single quote
+
#***3)Backslash
+
 
#*Globbing
 
#*Globbing
#**Filename expansion carried by BASH is known as Globbing.
 
 
#*The export statement
 
#*The export statement
#**The export command makes available variables to all child processes of the running script or shell
+
# Array Operations in BASH
# Arrays
+
#*Declaring an Array and Assigning values
#*Declaring an Array and Assigning values.
+
 
#*Initializing an Array during declaration
 
#*Initializing an Array during declaration
 
#*To find length of Bash Array and length of nth element
 
#*To find length of Bash Array and length of nth element
 
#*To print whole Bash Array
 
#*To print whole Bash Array
 +
#*Shell script to illustrate the concepts
 
# More on Arrays
 
# More on Arrays
 
#*Extraction of Array elements
 
#*Extraction of Array elements
Line 77: Line 54:
 
#*To Add an element to an Array
 
#*To Add an element to an Array
 
#*To remove an Element from an Array
 
#*To remove an Element from an Array
# Special operations on Arrays
+
#*Shell script to illustrate the concepts
 
# Conditional execution  
 
# Conditional execution  
#*test
+
#*Test
 +
#*Syntax of test
 
#*if...then
 
#*if...then
 +
#*Bash script  example for if...then
 
#*if...then...else...if
 
#*if...then...else...if
# More on If loops
+
#*if-else with a password program
#*nested if
+
#Nested and multilevel if statement in BASH
 +
#*Nested if
 +
#*Nested if with flowgraph
 +
#*Nested If-else with Name and Password verification program
 
#*Multilevel if-then-else
 
#*Multilevel if-then-else
# Logical Operations
+
#*Multilevel if-then-else with flowgraph
#*logical AND
+
#*Multilevel if-else  with String comparison program
#*logical OR
+
# Logical Operators in Bash
#*logical NOT
+
#*Logical AND syntax
#*Conditional expression using [
+
#*Logical AND workflow
#*Conditional expression using [[
+
#*Logical OR  
# Bash comparison  
+
#*Logical OR workflow
#*Arithmetic comparison                              
+
#*Logical OR and AND with string comparison program
#*String comparison                            
+
#*Logical NOT syntax
 +
#*Demonstration of logical NOT with an example(Example used: Checks whether a file exists)
 +
# Arithmetic comparison in BASH
 +
#*Arithmetic comparison
 +
#*-eq(equal to)
 +
#*-ne(not equal to)
 +
#*Demonstration of -eq and -ne with a word count program(Used: `wc -w`)
 +
#*-lt(lesser than)
 +
#*-le(lesser than or equal to)
 +
#*-gt(greater than)
 +
#*-ge(greater than or equal to)
 +
#*Demonstration of -lt,-gt, -ge and -le with a word count program(Used: `wc -c`)
 +
# String and File Attributes comparison in BASH
 +
#*String comparison
 +
#*String comparison using == operator 
 +
#*String comparison using != operator
 +
#*Simple program of user id to demnostrate string comparison                     
 
#*File attributes comparisons
 
#*File attributes comparisons
# Loops  
+
#*File attributes discussed are -f, -s, -w, -nt, -ot
#*The for loop statement            
+
# Conditional Loops  
#*Nested for loop statement         
+
#*The for loop statement  
#*The while loop statement            
+
#*for loop with three expression syntax
#*Use of : to set infinite while loop  
+
#*for loop with sequence/range syntax            
#*The until loop statement          
+
#*flow of for loop program
#*The select loop statement         
+
#*The while loop statement
#*Exit the select loop statement     
+
#*flow of for loop program
#*Using the break statement         
+
#More on Loops 
#*Using the continue statement      
+
#*A loop within a loop
# The case statement    
+
#*Outer for loop       
#*using case
+
#*Nested for loop
#*creating menus using case
+
#*Inner for loop
#*multiple options in case
+
#*Syntax of nested for loop
# Functions
+
#*Finding files in the directories using nested for loop
 +
# Case statement in BASH   
 +
#*Importance of case statement
 +
#*Syntax of case(With both single and multiple options)
 +
#*Work-flow of case statement
 +
#*Creating menus using case
 +
#*Case statement example
 +
#*Illustration of case statement with simple test for checking disk space
 +
 
 +
==BASH Shell Scripting: Intermediate==
 +
#Using File Descriptors
 +
#*Introduction on fd                                 
 +
#*Assign the file descriptor (fd) to file for output(exec fd > filename)
 +
#*Explained using an example of 'date' and 'echo'
 +
#*Closing the output fd(exec fd<&-)
 +
#*Explaination on error for the statements defiened after closing fd
 +
#*Assign the file descriptor (fd) to file for input(exec fd < filename)
 +
#*Closing the input file descriptor(exec fd<&-)
 +
#*Explaination on input fd using 'cat'
 +
# Basics of functions
 
#*Writing your first shell function  
 
#*Writing your first shell function  
#*Displaying functions            
+
#*Displaying functions                        
#*Removing functions              
+
#*Writing functions
#*Defining functions             
+
#*Function defintion           
#*Writing functions              
+
#*Calling functions
#*Calling functions             
+
#*Workflow of function
#*Pass arguments into a function   
+
#*Demonstration of function(Example used: Machine details program)
#*Local variable                
+
#More on Functions  
#*Returning from a function      
+
#*Local variable in a function
#*Source command                 
+
#*Global variable in a function
#*Recursive function             
+
#*Scope of local and global variables
#*Putting functions in background
+
#*Demonstration of local and global variable by an example(Example used: Displaying complete name)
# Redirections (error handling)  
+
#*Passing command line arguments
#*Input and Output                                   
+
#*Demonstration of passing command line arguments by an example(Example used: Displaying simple message)
#*Standard input                                     
+
#Arrays & functions
#*Standard output                                   
+
#*Arrays & functions
#*Standard error                                     
+
#*Usability of array by different function call
#*Redirection of both standard error and output     
+
#*Array example 
#*Appending redirected output                       
+
#*Use of return in a function
#*Empty file creation                               
+
#*Use of exit in a function
#*Here documents                                     
+
#*Illustration of return and exit by an example
#*Here strings                                       
+
#*Workflow of functions having return and exit     
#*Assigns the file descriptor (fd) to file for output
+
#Advance topics in a function     
#*Assigns the file descriptor (fd) to file for input 
+
#*Source command
#*Closes the file descriptor (fd)                             
+
#*Source command usage example
# Pipes and filters
+
#*Brief description on background function               
#*Linking Commands                   
+
#*Illustration of bg function
#*Multiple commands                   
+
#*Use of Jobs command to find job number and PID
#*Putting jobs in background         
+
#Recursive function
#*Pipes                               
+
#*Recursive function meaning and uses
#*How to use pipes to connect programs
+
#*Illustration of recursive function(Example used: Factorial of a number)
#*Input redirection in pipes         
+
#*Debugging of bash script(bash -x filename)
#*Output redirection in pipes         
+
#*Work flow of recursive function with the help of flowchart           
#*Why use pipes                       
+
# Basics of Redirection (error handling)
#*Filters
+
#*Input and Output in bash 
# Signals, process and traps
+
#*Redirection definition
#*Signals                                                 
+
#*file descriptors(FD)                             
#*What is a Process?                                       
+
#*Standard input
#*How to view Processes                                   
+
#*Input redirection explained with an example of sorting                                   
#*Sending signal to Processes                             
+
#*Standard output 
#*Terminating Processes                                      
+
#*Output redirection explained with an example of ls                                 
#*Shell signal values                                     
+
#*Standard error
#*The trap statement                                       
+
#*Error redirection explained with an example of rm
#*How to clear trap                                       
+
#More on Redirection                                      
#*Include trap statements in a script                     
+
#*Redirection of both standard error and output using 'command &>filename' 
#*Use the trap statement to catch signals and handle errors
+
#*Redirection of both standard error and output using 'command >/dev/null 2>&1'
#*What is a Subshell?                                     
+
#*Explanation on /dev/null
#*Compound command                                         
+
#*Appending redirected output using '>>' 
#*Exec command
+
#'Here' document and 'Here' string                                                 
# Making you shell script interactive (using dialog box)  
+
#*Here document   
 +
#*Illustration of Here document with 'wc -w'
 +
#*Here string 
 +
#*Illustration of Here string with 'wc -w'
 +
 
 +
==BASH Shell Scripting: Advance==
 +
# Making shell script interactive (using dialog box)  
 
#*Menu driven scripts                           
 
#*Menu driven scripts                           
#*Getting information about your system       
+
#*Bash display dialog boxes                    
#*Bash display dialog boxes                  
+
#*A yes/no dialog box  
#*Dialog customisation with configuration file
+
# More on interactive shell script(using dialog box)                          
#*A yes/no dialog box                           
+
 
#*An input dialog box                           
 
#*An input dialog box                           
 
#*A password box                               
 
#*A password box                               
#*A menu box                                  
+
#*A menu box
#*A progress bar (gauge box)                 
+
#*The form dialog for input
+

Latest revision as of 14:28, 7 May 2019

Bash: Bash is the shell, or command language interpreter, that will appear in the GNU operating system. Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh). It offers functional improvements over sh for both programming and interactive use.

In addition, most sh scripts can be run by Bash without modification. The improvements offered by Bash include: Command line editing, Unlimited size command history, Job Control, Shell Functions and Aliases, Indexed arrays of unlimited size, Integer arithmetic.

Bash Scripting: In addition to the interactive mode, where the user types one command at a time, with immediate execution and feedback, Bash also has the ability to run an entire script of commands, known as a "Bash shell script". A script might contain just a very simple list of commands or even just a single command or it might contain functions, loops, conditional constructs. In effect, a Bash shell script is a computer program written in the Bash programming language.

Shell scripts are commonly used for many system administration tasks, such as performing disk backups, evaluating system logs, and so on. They are also commonly used as installation scripts for complex programs. They are particularly suited to all of these because they allow complexity without requiring it.


In this tutorial we would mainly concentrate on how to use the wide variety of commands of Linux commands to handle files, directories, processes etc. These tutorials are created using Ubuntu version 12.04 and above. Please see the associated text box of individual spoken tutorials on the website to decide the versions of Linux OS to which it is applicable.

The contributors who helped create the outline, transcribe, create the tutorials are - Sachin Patil, Lavitha Monisha Pereira, Ashwini Patil, Nancy Varkey - all from IIT Bombay. The effort has been moderated and coordinated by the Spoken Tutorial team and FOSSEE team, IIT Bombay.


Learners: Polytechnic/UG/PG computer hardware students and system administrators will greatly benefit by learning to automate common tasks using BASH.



BASH Shell Scripting: Basic

  1. Introduction to BASH Shell Scripting
    • About different types of Shells
    • The Bash shell
    • Bash Shell Script
    • Write a Bash Shell script and execute it
  2. Basics of Shell Scripting
    • System variables
    • User defined variables
    • Accepting user input via keyboard
  3. Command Line arguments and quoting
    • Command Line arguments
    • Single quote
    • Double quote
    • Backslash
  4. Globbing and export statements
    • Globbing
    • The export statement
  5. Array Operations in BASH
    • Declaring an Array and Assigning values
    • Initializing an Array during declaration
    • To find length of Bash Array and length of nth element
    • To print whole Bash Array
    • Shell script to illustrate the concepts
  6. More on Arrays
    • Extraction of Array elements
    • Search and replace in an Array element
    • To Add an element to an Array
    • To remove an Element from an Array
    • Shell script to illustrate the concepts
  7. Conditional execution
    • Test
    • Syntax of test
    • if...then
    • Bash script example for if...then
    • if...then...else...if
    • if-else with a password program
  8. Nested and multilevel if statement in BASH
    • Nested if
    • Nested if with flowgraph
    • Nested If-else with Name and Password verification program
    • Multilevel if-then-else
    • Multilevel if-then-else with flowgraph
    • Multilevel if-else with String comparison program
  9. Logical Operators in Bash
    • Logical AND syntax
    • Logical AND workflow
    • Logical OR
    • Logical OR workflow
    • Logical OR and AND with string comparison program
    • Logical NOT syntax
    • Demonstration of logical NOT with an example(Example used: Checks whether a file exists)
  10. Arithmetic comparison in BASH
    • Arithmetic comparison
    • -eq(equal to)
    • -ne(not equal to)
    • Demonstration of -eq and -ne with a word count program(Used: `wc -w`)
    • -lt(lesser than)
    • -le(lesser than or equal to)
    • -gt(greater than)
    • -ge(greater than or equal to)
    • Demonstration of -lt,-gt, -ge and -le with a word count program(Used: `wc -c`)
  11. String and File Attributes comparison in BASH
    • String comparison
    • String comparison using == operator
    • String comparison using != operator
    • Simple program of user id to demnostrate string comparison
    • File attributes comparisons
    • File attributes discussed are -f, -s, -w, -nt, -ot
  12. Conditional Loops
    • The for loop statement
    • for loop with three expression syntax
    • for loop with sequence/range syntax
    • flow of for loop program
    • The while loop statement
    • flow of for loop program
  13. More on Loops
    • A loop within a loop
    • Outer for loop
    • Nested for loop
    • Inner for loop
    • Syntax of nested for loop
    • Finding files in the directories using nested for loop
  14. Case statement in BASH
    • Importance of case statement
    • Syntax of case(With both single and multiple options)
    • Work-flow of case statement
    • Creating menus using case
    • Case statement example
    • Illustration of case statement with simple test for checking disk space

BASH Shell Scripting: Intermediate

  1. Using File Descriptors
    • Introduction on fd
    • Assign the file descriptor (fd) to file for output(exec fd > filename)
    • Explained using an example of 'date' and 'echo'
    • Closing the output fd(exec fd<&-)
    • Explaination on error for the statements defiened after closing fd
    • Assign the file descriptor (fd) to file for input(exec fd < filename)
    • Closing the input file descriptor(exec fd<&-)
    • Explaination on input fd using 'cat'
  2. Basics of functions
    • Writing your first shell function
    • Displaying functions
    • Writing functions
    • Function defintion
    • Calling functions
    • Workflow of function
    • Demonstration of function(Example used: Machine details program)
  3. More on Functions
    • Local variable in a function
    • Global variable in a function
    • Scope of local and global variables
    • Demonstration of local and global variable by an example(Example used: Displaying complete name)
    • Passing command line arguments
    • Demonstration of passing command line arguments by an example(Example used: Displaying simple message)
  4. Arrays & functions
    • Arrays & functions
    • Usability of array by different function call
    • Array example
    • Use of return in a function
    • Use of exit in a function
    • Illustration of return and exit by an example
    • Workflow of functions having return and exit
  5. Advance topics in a function
    • Source command
    • Source command usage example
    • Brief description on background function
    • Illustration of bg function
    • Use of Jobs command to find job number and PID
  6. Recursive function
    • Recursive function meaning and uses
    • Illustration of recursive function(Example used: Factorial of a number)
    • Debugging of bash script(bash -x filename)
    • Work flow of recursive function with the help of flowchart
  7. Basics of Redirection (error handling)
    • Input and Output in bash
    • Redirection definition
    • file descriptors(FD)
    • Standard input
    • Input redirection explained with an example of sorting
    • Standard output
    • Output redirection explained with an example of ls
    • Standard error
    • Error redirection explained with an example of rm
  8. More on Redirection
    • Redirection of both standard error and output using 'command &>filename'
    • Redirection of both standard error and output using 'command >/dev/null 2>&1'
    • Explanation on /dev/null
    • Appending redirected output using '>>'
  9. 'Here' document and 'Here' string
    • Here document
    • Illustration of Here document with 'wc -w'
    • Here string
    • Illustration of Here string with 'wc -w'

BASH Shell Scripting: Advance

  1. Making shell script interactive (using dialog box)
    • Menu driven scripts
    • Bash display dialog boxes
    • A yes/no dialog box
  2. More on interactive shell script(using dialog box)
    • An input dialog box
    • A password box
    • A menu box

Contributors and Content Editors

Ashwini, Lavitha Pereira, Nancyvarkey, PoojaMoolya