Difference between revisions of "BASH"
PoojaMoolya (Talk | contribs) (→BASH Shell Scripting: Advance) |
|||
(29 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | '''Bash''' is the shell, or command language interpreter, that will appear in the '''GNU operating system'''. | + | '''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). | 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 offers functional improvements over sh for both programming and interactive use. | ||
Line 5: | Line 6: | ||
In addition, most sh scripts can be run by Bash without modification. | 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. | 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. | 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;"> | ||
− | The contributors who helped create the outline, transcribe, create the tutorials are - | + | 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. |
</blockquote> | </blockquote> | ||
+ | |||
+ | '''Learners''': Polytechnic/UG/PG computer hardware students and system administrators will greatly benefit by learning to automate common tasks using BASH. | ||
Line 18: | Line 27: | ||
==BASH Shell Scripting: Basic== | ==BASH Shell Scripting: Basic== | ||
# Introduction to BASH Shell Scripting | # Introduction to BASH Shell Scripting | ||
− | #*The | + | #*About different types of Shells |
+ | #*The Bash shell | ||
#*Bash Shell Script | #*Bash Shell Script | ||
+ | #*Write a Bash Shell script and execute it | ||
# Basics of Shell Scripting | # Basics of Shell Scripting | ||
#*System variables | #*System variables | ||
Line 90: | Line 101: | ||
#*The while loop statement | #*The while loop statement | ||
#*flow of for loop program | #*flow of for loop program | ||
− | #More on Loops | + | #More on Loops |
+ | #*A loop within a loop | ||
+ | #*Outer for loop | ||
#*Nested for loop | #*Nested for loop | ||
+ | #*Inner for loop | ||
#*Syntax of nested for loop | #*Syntax of nested for loop | ||
#*Finding files in the directories using nested for loop | #*Finding files in the directories using nested for loop | ||
Line 103: | Line 117: | ||
==BASH Shell Scripting: Intermediate== | ==BASH Shell Scripting: Intermediate== | ||
− | # Basics of | + | #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 | ||
Line 111: | Line 134: | ||
#*Workflow of function | #*Workflow of function | ||
#*Demonstration of function(Example used: Machine details program) | #*Demonstration of function(Example used: Machine details program) | ||
− | # | + | #More on Functions |
#*Local variable in a function | #*Local variable in a function | ||
#*Global variable in a function | #*Global variable in a function | ||
Line 118: | Line 141: | ||
#*Passing command line arguments | #*Passing command line arguments | ||
#*Demonstration of passing command line arguments by an example(Example used: Displaying simple message) | #*Demonstration of passing command line arguments by an example(Example used: Displaying simple message) | ||
− | #Arrays | + | #Arrays & functions |
− | #*Arrays | + | #*Arrays & functions |
#*Usability of array by different function call | #*Usability of array by different function call | ||
#*Array example | #*Array example | ||
− | #*Use of return | + | #*Use of return in a function |
− | #*Illustration of return and exit | + | #*Use of exit in a function |
− | #* | + | #*Illustration of return and exit by an example |
− | #Advance topics in function | + | #*Workflow of functions having return and exit |
− | #*Source command | + | #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 |
− | + | #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 |
− | #* | + | # 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 |
− | #* | + | #More on Redirection |
− | + | #*Redirection of both standard error and output using 'command &>filename' | |
− | #*Output redirection | + | #*Redirection of both standard error and output using 'command >/dev/null 2>&1' |
− | #* | + | #*Explanation on /dev/null |
− | #* | + | #*Appending redirected output using '>>' |
− | + | #'Here' document and 'Here' string | |
− | + | #*Here document | |
− | # | + | #*Illustration of Here document with 'wc -w' |
− | + | #*Here string | |
− | + | #*Illustration of Here string with 'wc -w' | |
− | + | ||
− | #* | + | |
− | #* | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | #* | + | |
− | #* | + | |
− | + | ||
− | + | ||
− | # | + | |
− | #* | + | |
− | #* | + | |
− | + | ||
− | #* | + | |
− | #* | + | |
− | + | ||
− | + | ||
− | + | ||
− | + |
Latest revision as of 14:54, 9 October 2020
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
- Introduction to BASH Shell Scripting
- About different types of Shells
- The Bash shell
- Bash Shell Script
- Write a Bash Shell script and execute it
- Basics of Shell Scripting
- System variables
- User defined variables
- Accepting user input via keyboard
- Command Line arguments and quoting
- Command Line arguments
- Single quote
- Double quote
- Backslash
- Globbing and export statements
- Globbing
- The export statement
- 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
- 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
- Conditional execution
- Test
- Syntax of test
- if...then
- Bash script example for if...then
- if...then...else...if
- if-else with a password program
- 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
- 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)
- 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 discussed are -f, -s, -w, -nt, -ot
- 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
- 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
- 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
- Displaying functions
- Writing functions
- Function defintion
- Calling functions
- Workflow of function
- Demonstration of function(Example used: Machine details program)
- 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)
- 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
- 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
- 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
- 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
- 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 '>>'
- 'Here' document and 'Here' string
- Here document
- Illustration of Here document with 'wc -w'
- Here string
- Illustration of Here string with 'wc -w'