- Author: John Doe
- Created: 7th July 2020
- Last Modified: 7th July 2020
- Description: Creates a backup in ~/bash_course folder of all files in the home directory
- Usage: backup_scrip
Find your octal code on permissions-calculator.org By default, go with 744 (rwxr--r--) or 754 (rwxr-xr--)
1. Edit your ~/.profile file to add a custom folder to your PATH
export PATH="$PATH:/path/to/script_directory
2. Reload the ~/.profile file
source ~/.profile
Parameter: A Parameter is an entity that can store the value
* Variable
* Position Parameter
* Special Parameter
Variable: Variable are the parameter that you can chnage value of.
* User defined variable
* Shell Variable
Creating a User defined variable : name=value (No Space around = Sign) Retriving value: ${Parameter}
- Shell variable are the variable, insted of being created by you, are actully created by the shell
- There are two types of shell variable: bourne shell variable and bash shell variable
- Some commonly used Shell variable HOME, PATH, USER, HOSTNAME, HOSTTYPE, PS1
- Shell Variable are name all uppercase
- command substitution is shell feature that allow you to grab the output of command and do stuff with it.
- command substitution syntax : $(command)
- command substitution to save date in user defined variable
Synatx: currenttime=$(date +%H:%M:%S)
Arithmatic Expansions Is used to perform mathmatical oprations
Syntax: $(( expression ))
When Bash receives command line, it will follow 6 steps to interpret.
- Tokenisation
- Command Identification
- Expansions
- Quote Removal
- Redirection
- Execute
During the tokenisation, bash read the command line for unquoted metacharachters, and use them to divide the command into word and oprators.
List of Metacharachters
1. SpaceTab
2. NewLine
3. |
4. &
5. ;
6. (
7. )
8. <
9. >
Words : are tokens that do not contain unquoted metacharachters
2. Command Identification : Bash will then break the command line down into simple and compound command.
1. Simple command : Set of word terminated by oprators
+ The first word is command line
+ Subsequent words are taken as indivisual arguments to that command.
2. Oprators: The Token that contain at least 1 unquoted metacharachters.
List of Control oprators List of Redirection oprators
NewLine <
| >
|| <<
& >>
&& <&
; >&
;; >|
;& <<-
|& <>
(
)
Example
echo $name > out Here: Space and > unquoted metacharachters
Example 1
echo a b c d echo 1 2 3 - Tokenisation
Example 2
echo a b c d; echo 1 2 3
This is interpreted as two simple commands because there is control oprator(;) that end the first command.
Example 3
echo $name > out
Interpreted as one simple command, including redirection operator
Compound command : Start with reserved word and are terminated by corresponding reserved word.
Example
if [[2 -gt 1]];then
echo "Welcome"
fi
- Earliar stages are given higer precedence than later one.
- Same stages given equal precedence and processed from left to right.
Four Stage of Processing Expansions
1. Brace Expansions
2. Parameter Expansions ==> Arithmatic Expansions ==> command Substitution ==> Tild Expansions ==> Word Splitting ==> Globbing
The Shell remove all unquoted backslashes, Single quote charachters and double quote charachters that did not result from shell expansion.
- echo "Hello"
Result : echo Hello
The double quote removed because they are not quoted do not result from expansion
- echo '"Hello"'
Result : echo "Hello"
The double quote are retained, however because they are quoted by single quote.
- echo \"Hello\"
Result : "Hello"
The backslashes are removed, because they are unquoted and don't result from an expansion
The double quote are retained, because they are quoted by ther preceding backslashes.
- path="C:\Users\Amit\Documents"
Result : echo C:\Users\Amit\Documents
Redirection Oprators to determine where the standard input, standard output and standard error data streams for the command should connect to
1. Not All data stream
2. A data stream can only connect to one location at a time
3. Redirection processed from left to right
Example 1
command < file : Redirect the containt of file to the standard input command.
Example 2
command > file : Truncat the file and then the standard output of command to it
Example 3
command >> file : Appends standard output of command to file.
Example 4
command 2 > file : Truncat file and then redirects standard error of command to it.
Example 5
command 2 >> file : Appends standard error to a file
Example 6
command 2 & > file : Truncat file, and then redirects both standard output and standard error of command to it.
Example 7
command &>> file : Appends both standard output and standard error of command to file.
At this stage the shell has completed its processing of the command line and it now executes the command that have resulted from all the above steps.
Postional Parameter : The Shell assigns number called positional parameter to each command-line arrgument that is eneterd ($1, $2, $3...)
Example : myscript Amit /home/Amit Blue
#!/bin/bash
echo "My name is $1"
echo "My home directory is $2"
echo "My Favourite colour is $3"
The Read command ask the input from user and save this input into variable Syntax for Read command read variable
-p : Promt to user about what information they must enter
-t : timeout if the user doesn't enter value within time second
-s : Prevent the input that the user enters from being shown into the terminal
-N : Limit the user response to exactly charachters
#!/bin/bash
read -t 5 -p "Input your first name within 5 seconds: " name
read -n 2 -p "Input your age (max 2 digits): " age
read -s -N 5 -p "Enter your zip code (exactly 5 digits): " zipcode
echo "$name, $age, $zipcode" >> data.csv
Select command provides the user with a dropdown menu to select from. User can select an option from list of oprations
PS3="Please select an option below: "
select variable in options; do
commands...
break
done
#!/bin/bash
PS3="What is the day of the week?: "
select day in mon tue wed thu fri sat sun; do
echo "The day of the week is $day"
break
done
List : When You Put one or more command on a given line
List Oprators : Control Oprators that enable us to create list of command that operat in a diffrent way
| Operator | Example | Meaning |
|---|---|---|
| & | command1 & command2 | Send command1 into background and run command2 in current shell |
| ; | command1 ; command2 | The Shell will only run command2 if command1 successful |
| && | command1 && command2 | The Shell will only run command2 if command1 successful |
Test Command : a command that can be used in bash to compare diffrent pice of information.
Syntax : [ Expresion ]
| Operator | Example | Meaning |
|---|---|---|
| -eq | [ 2 -eq 2 ] | successful if the two number are equal |
| -ne | [ 2 -ne 2 ] | successful if the two number are Not equal |
| = | [ $a = $b ] | Successful if the two string are equal |
| != | [ $a != $b ] | successful if the two string are not equal |
| -z | [ -z$c ] | successful if the string is empty |
| -n | [ -n$c ] | successful if the string is not empty |
| -e | [ -e /path/to/file ] | successful if file path exist |
| -f | [ -f /path/to/file ] | successful if file path exist and is a regular file |
| -d | [ -x /path/to/file ] | successful if file path exist and is a directory |
| -x | [ -x /path/to/file ] | successful if file path exist and is a executeable file by current user. |
Start and End using reserved words "If" and "fi"
check the exit status of command and only run the command if certaion condition is true
if test1; then
command #Only run if test1 passes
elif test2; then
command #Only run if test1 fail and test2 passes
elif testN; then
command #Only run if All previous fail and testN passes
else
command #only run if all test fail
fi
#!/bin/bash
read -p "Please enter a number" number
if [ $number -gt 0 ]; then
echo "Your number is greater than 0"
elif [ $number -lt 0 ]; then
echo "Your number is less than 0"
else
echo "Your number is 0!"
fi
Case statements provide us with an elegant way to implement branching logic.
Case statements start and end using the reserved words "case" and "esac"
case "$variable" in # don't forget the $ and the double quotes!
pattern1)
Commands ...
;;
pattern2)
Commands ...
;;
patternN)
Commands ...
;;
*)
Commands ... # run these if no other pattern matches
;;
esac
#!/bin/bash
read -p "Please enter a number: " number
case "$number" in
"") echo "You didn't enter anything!"
[0-9]) echo "you have entered a single digit number";;
[0-9][0-9]) echo "you have entered a two digit number";;
[0-9][0-9][0-9]) echo "you have entered a three digit number";;
*) echo "you have entered a number that is more than three digits";;
esac