Linux Bash Scripting
Linux Bash Scripting
Startup files:
When ever a terminal was created (console or remote connection/session), bash executes few files at
Startup in Interactive login shell like /etc/profile or ~/.bash_profile, ~/.bash_login, and ~/.bashrc
(Interactive non-login shell)
~/.bash_profile is read and executed when bash is invoked as an interactive login shell (Login to system
using ssh)
~/.bashrc is executed for an interactive non-login shell (Invoking a new shell on an already logged-in
shell)
Use ~/.bash_profile to run commands that should run only once, such as customizing the $PATH
(environment variables)
Use ~/.bashrc file if you want commands that should run every time you launch a new shell. (aliases and
functions, custom prompts, history customizations etc.,)
~/.bash_profile is only used by BASH shell and ~/.profiles file is read by all shells (First it looks for
~/.bash_profile file, if it doesn’t exist then it will look for ~/.profile)
Variables:
There are two different ways to define a variables in shell: Environment variables and user-defined
variables
Environment variables are pre-defined variables which will be loaded by default when we establish a
connection/login to Machine which terminal will be created, and default variables will be loaded which
shows in UPPER case letters.
#env or #printenv
If you want to set own environment variables to be loaded when connections/login to machine, which
users no need to create these environment variables.
These variables are temporary, since ever session/terminal will have its own environment variables
loaded by default. If we need to add our own environment variables, we need to add in ~/.bashrc or
~/.profile file.
Arrays:
An array is a systematic arrangement of the same type of data. But in Shell script Array is a variable
which contains multiple values may be of same type or different type since by default in shell script
everything is treated as a string. An array is zero-based i.e., indexing start with 0.
Array is defined in three different ways: Indirect array, Explicit array, and Compound assignment
```
ARRAYNAME[INDEX]=value
```
```
declare -a ARRAYNAME
```
```
```
#echo ${ARRAYNAME[INDEX/0/1/2]}
#echo ${ARRAYNAME[*]}
#echo ${ARRAYNAME[@]}
#echo ${#ARRAYNAME[*]}
#echo ${ARRAYNAME[@]:0/1/2/3}
#echo ${ARRAYNAME[@]:STARTINDEX:COUNTELEMENT}
#unset ARRAYNAME[INDEX]
#echo ${ARRAYNAME[@]:index:range}
#unset ARRAYNAME to delete whole array
Conditions:
There are 5 conditional statements
1) If statement
2) If…else statement
3) If…elif...else...fi statement (else if ladder)
4) If…then…else…if…then…fi…fi (Nested if)
5) switch statement (case statement)
if statement
This statement block will process if specific condition is true
Syntax
```
If [ expression ]
then
statement
fi
or
If [ expression ]; then
statement
fi
```
Examples:
If…else statement
If the condition is not true, then else block will be executed
Syntax
```
If [ expression ]
then
statement1
else
statement2
fi
or
If [ expression ]; then
statement1
else
statement2
fi
```
Pattern match conditions, use single braces and see how it works. Then add additional brace to check
patterns
Syntax
```
If [ expression1 ]
then
statement1
statement2
elif [ expression2 ]
then
statement3
statement4
else
statement5
fi
```
```
If [ expression ]
then
statement1
statement2
else
If [ expression2 ]
statement3
statement4
fi
fi
```
Switch/Case Statement
It works as a switch statement like if a value match any of the pattern, then it will be executed
Syntax
```
case in
pattern1)
statement1;;
statement2;;
esac
```
Loops:
There are three types of loops that can created in bash scripting
1) For loop
2) While
3) Until loop
For loop:
It is used to execute set of commands/values for every item in the list.
```
for VARIABLE in 1 2 3 4 5 .. N
do
command
command
command
done
or
do
statement1
statement2
statement3
done
```
$@ will give the arguments passed and $# will give no of arguments passed
While loop:
It allows us to execute a set of commands repeatedly until some condition occurs.
```
while command/condition
do
statements
done
```
Run infinity loop using while
#!/bin/bash
COUNTER=0
done
#!/bin/bash
while :
do
done
#!/bin/bash
while true
do
#!/bin/bash
i=0
while ((i<=12))
do
echo $i
((i++))
done
When something goes wrong and to debug, we can use -x with the shebang of first line of the script:
#!/bin/bash -x
Head:
By default, head will print only first 10 lines, even through the file or input data has more lines
#head filename
#head -n 20 filename or head -n+20 filename This will print specified number of lines -n needs digit
instead if default 10 lines
#head -n -10 filename or head -n-10 filename This will remove last 10 lines of a file
Tail:
By default, tail command will display last 10 lines of a file or a input data
#tail -n 8 filename Specified number of last lines will be printed and tail -n-2 filename it will print
only last two lines
Multiline strings:
```
#!/bin/bash
EOF
or
visualpath
testline
third line
sudheer
EOF
```
AWK command:
cat /etc/passwd | awk -F: '{print $3}' It will print third column in /etc/passwd file (UID)
cat /etc/passwd | awk -F: '/testuser/ {print $3}' it will print UID of user “testuser”
cat /etc/passwd | awk -F: '/testuser/ {print $1, $3}' it will print username and UID for testuser
cat /etc/passwd | awk -F: '/testuser/ {print NR,$1, $3}' It will above info along with line number with
matching once
cat /etc/passwd | awk -F: '/testuser/ {print NR,$1, $3, $NF}' It will print last column as well along with
above info
Sed Command:
sed 's/sudheerdemo/visualpathdemo/' passwd_copy It will replace/substitute the word
sudheerdemo with visualpathdemo in each line of first occurrence
To modify each occurrence in line, we need to pass /g
sed 's/sudheerdemo/visualpathdemo/g' passwd_copy
sed 's/sudheerdemo/visualpathdemo/3g' passwd_copy modifies from nth occurrence till
end
Note: above commands will not actually modify, it will print the results on the terminal output
how it has changed. We can redirect and save the changes in another file if needed
sed '38s/sudheerdemo/visualpathdemo/g' passwd_copy It will modify the changes on
specified line number 38th line here
sed -n 's/sudheerdemo/visualpathdemo/gp' passwd_copy it will print only replaced lines
sed 'nd' passwd_copy (Example: delete 38th line in a file #sed '38d' passwd_copy) It will
delete a line with nth value specified
sed '/visualpath/d' passwd_copy It will delete a line with matched string
sed '/^$/d' passwd_copy Deletes empty lines in a file
sed -i '/^#/d;/^$/d' passwd_copy It deletes empty lines and # commented lines in single task
sed -n '2,5p' passwd_copy print lines specified in the range (2 to 5)
cat passwd_copy | sed -n '/sudheerdemo/ s/\/bin\/sh/\/bin\/bash/p' This command will
search for line with sudheerdemo and in that line it will replace “/bin/sh” to /bin/bash
CUT command:
Syntax:
#cut -d “delimiter” -f “field_number” filename or any other input
Programming