Bash shell scripting:
Shell is a UNIX term for an interface between a user and an operating system service. Shell provides users with an interface and accepts human-readable commands into the system and executes those commands which can run automatically and give the program’s output in a shell script.
Bash is a default interpreter on many GNU/Linux systems.
Bash Shell Scripts are written using text editors. On your Linux system, open a text editor program, open a new file to begin typing a shell script or shell programming, then give the shell permission to execute your shell script and put your script at the location from where the shell can find it.
Steps to create a shell script:
1. Create a file using a "vi" editor (or any other editor). Name script file with ".sh" extension.
2. Start the script with "#!/bin/bash"
3. Write some code and save the file as "filename.sh".
4. For executing the script type "bash filename.sh"
Example1: Simple script "hello.sh" to take your name as input and respond back.
#!/bin/bash
echo "what is your name?"
read name
echo "How do you do, $name?"
read remark
echo "I am $remark too!"
The first line of the above script is what defines the script as a bash script. "#!" is an operator called shebang which directs the script to the interpreter location. So, if we use "#!/bin/bash" the script gets directed to the bash shell.
In order to execute the script, the file "hello.sh" needs to be made executable by the below command.
$ chmod +x hello.sh
Note: ".sh" extension doesn't hold any value. It's not a mandatory entry but this extension makes it immediately clear while checking multiple files that it is plausibly a shell script.
To execute the script use the below command:
$ ./hello.sh
or
$ bash hello.sh
Output:
[ec2-user@ip-172-31-41-211 Training_bash_scripts]$ bash hello.sh
what is your name?
nikhil
How do you do, nikhil?
awesome
I am awesome too!
#!/bin/bash
tar -czf /tmp/ec2-user_home_directory.tar.gz /home/ec2-user
ls -ltr /tmp/ec2-user_home_directory.tar.gz
The above script will take all the files in the ec2-user working directory and creates a compressed tar ball and saves it in /tmp directory.
Output:
[ec2-user@ip-172-31-41-211 Training_bash_scripts]$ ./backup.sh
tar: Removing leading `/' from member names
-rw-rw-r-- 1 ec2-user ec2-user 78344 Feb 23 16:33 /tmp/ec2-user_home_directory.tar.gz
Variables are the essence of programming. Variables allow a programmer to store data, alter and reuse them throughout the script.
Example3: variables.sh
#!/bin/bash
greeting="Welcome"
user=$(whoami)
day=$(date +%A)
echo "$greeting back $user! Today is $day, which is the best day of the entire week!"
echo "Your Bash shell version is: $BASH_VERSION. Enjoy!"
Let's look at the script more closely. First, we have declared a variable "greeting" and assigned a string value "Welcome" to it. The next variable "user" contains a value of the user name running a shell session. This is done through a technique called command substitution. Meaning that the output of the "whoami" command will be directly assigned to the user variable. The same goes for our next variable "day" which holds the name of today's day produced by "date +%A" command.
Output:
[ec2-user@ip-172-31-41-211 Training_bash_scripts]$ ./variables.sh
Welcome back ec2-user! Today is Tuesday, which is the best day of the entire week!
Your Bash shell version is: 4.2.46(2)-release. Enjoy!
Example4:
$ a=4
$ b=5
$ echo $[$a + $b]
Output:
[ec2-user@ip-172-31-41-211 Training_bash_scripts]$ echo $[$a + $b]
9
Example5: Backup script "backup_2.sh" for all users.
#!/bin/bash
# This bash script is used to backup a user's home directory to /tmp/.
user=$(whoami)
input=/home/$user
output=/tmp/${user}_home_$(date +%Y-%m-%d_%H%M%S).tar.gz
tar -czf $output $input
echo "Backup of $input completed! Details about the output backup file:"
ls -l $output
Note: Every line starting with # sign except shebang will not be interpreted by bash and will only serve as a programmer's internal notes.
Secondly, the script uses a new shell scripting trick ${parameter} called parameter expansion. In our case, curly braces {} are required because our variable $user is followed by characters that are not part of its variable name.
Output:
[ec2-user@ip-172-31-41-211 Training_bash_scripts]$ ./backup_2.sh
tar: Removing leading `/' from member names
Backup of /home/ec2-user completed! Details about the output backup file:
-rw-rw-r-- 1 ec2-user ec2-user 78808 Feb 23 16:38 /tmp/ec2-user_home_2021-02-23_163806.tar.gz
Functions allow a programmer to organize and reuse code, hence increasing the efficiency, execution speed as well as readability of the entire script.
You can think of the function as a way to group a number of different commands into a single command. This can be extremely useful if the output or calculation you require consists of multiple commands, and it will be expected multiple times throughout the script execution. Functions are defined by using the function keyword and followed by function body enclosed by curly brackets.
Example6: function.sh, simple shell function to be used to print user details and will make two function calls, thus printing user details twice upon script execution.
#!/bin/bash
# This bash script is used to backup a user's home directory to /tmp/.
user=$(whoami)
input=/home/$user
output=/tmp/${user}_home_$(date +%Y-%m-%d_%H%M%S).tar.gz
# The function total_files reports a total number of files for a given directory.
function total_files {
find $1 -type f | wc -l
}
# The function total_directories reports a total number of directories
# for a given directory.
function total_directories {
find $1 -type d | wc -l
}
tar -czf $output $input 2> /dev/null
echo -n "Files to be included:"
total_files $input
echo -n "Directories to be included:"
total_directories $input
echo "Backup of $input completed!"
echo "Details about the output backup file:"
ls -l $output
Output:
[ec2-user@ip-172-31-41-211 Training_bash_scripts]$ ./function.sh
Files to be included:79
Directories to be included:23
Backup of /home/ec2-user completed!
Details about the output backup file:
-rw-rw-r-- 1 ec2-user ec2-user 79049 Feb 23 16:39 /tmp/ec2-user_home_2021-02-23_163958.tar.gz
Example7: compare.sh
#!/bin/bash
string_a="UNIX"
string_b="GNU"
echo "Are $string_a and $string_b strings equal?"
[ $string_a = $string_b ]
echo $?
num_a=100
num_b=100
echo "Is $num_a equal to $num_b ?"
[ $num_a -eq $num_b ]
echo $?
Output:#!/bin/bash
num_a=100
num_b=200
if [ $num_a -lt $num_b ]; then
echo "$num_a is less than $num_b!"
fi
Output:
[ec2-user@ip-172-31-41-211 Training_bash_scripts]$ ./condition.sh
100 is less than 200!
#!/bin/bash
num_a=400
num_b=200
if [ $num_a -lt $num_b ]; then
echo "$num_a is less than $num_b!"
else
echo "$num_a is greater than $num_b!"
fi
Output:
[ec2-user@ip-172-31-41-211 Training_bash_scripts]$ ./if-else-condition.sh
400 is greater than 200!
When using bash positional parameters, this is rather an easy task. Positional parameters are assigned via command line arguments and are accessible within a script as $1, $2...$N variables. During the script execution, any additional items supplied after the program name are considered arguments and are available during script execution.
Useful system variables:
$0 The name of the bash script
$1 - $9 The first 9 arguments to the bash script.
$# How many arguments were passed to the bash script.
$@ All exit status of the most recently run process.
$? The exit status of the most recently run process.
$$ The process ID of the current script.
$User The username of the user running the script.
$HOSTNAME The hostname of the machine the script is running on.
$SECONDS The number of seconds since the script was started.
$RANDOM Returns a different random number each time is it referred to.
$LINENO Returns the current line number in the bash script.
Example10: parameters.sh
#!/bin/bash
echo $1 $2 $4
echo $#
echo $*
Output:
[ec2-user@ip-172-31-41-211 Training_bash_scripts]$ ./parameters.sh nikhil rahul ali priya sheker
nikhil rahul priya
5
nikhil rahul ali priya sheker
For Loop:
For loop is used to iterate through any given code for any number of supplied items in the list.
Example11: for-loop.sh
#!/bin/bash
for i in 1 2 3; do
echo $i
done
Output:
[ec2-user@ip-172-31-41-211 Training_bash_scripts]$ ./for-loop.sh
1
2
3
Example12: while-loop.sh
#!/bin/bash
counter=0
while [ $counter -lt 3 ]; do
let counter+=1
echo $counter
done
Output:
[ec2-user@ip-172-31-41-211 Training_bash_scripts]$ ./while-loop.sh
1
2
3
Until Loop:
The last loop we are going to cover in this scripting tutorial is until loop. The until loop does the exact opposite of the while loop. Until loop also acts on a preset condition. However, the code enclosed between DO and DONE is repeatedly executed only until this condition changes from false to true.
Example13: until-loop.sh
#!/bin/bash
counter=6
until [ $counter -lt 3 ]; do
let counter-=1
echo $counter
done
Output:
[ec2-user@ip-172-31-41-211 Training_bash_scripts]$ ./until-loop.sh
5
4
3
2
#!/bin/bash
# This bash script is used to backup a user's home directory to /tmp/.
function backup {
if [ -z $1 ]; then
user=$(whoami)
else
if [ ! -d "/home/$1" ]; then
echo "Requested $1 user home directory doesn't exist."
exit 1
fi
user=$1
fi
input=/home/$user
output=/tmp/${user}_home_$(date +%Y-%m-%d_%H%M%S).tar.gz
function total_files {
find $1 -type f | wc -l
}
function total_directories {
find $1 -type d | wc -l
}
function total_archived_directories {
tar -tzf $1 | grep /$ | wc -l
}
function total_archived_files {
tar -tzf $1 | grep -v /$ | wc -l
}
tar -czf $output $input 2> /dev/null
src_files=$( total_files $input )
src_directories=$( total_directories $input )
arch_files=$( total_archived_files $output )
arch_directories=$( total_archived_directories $output )
echo "########## $user ##########"
echo "Files to be included: $src_files"
echo "Directories to be included: $src_directories"
echo "Files archived: $arch_files"
echo "Directories archived: $arch_directories"
if [ $src_files -eq $arch_files ]; then
echo "Backup of $input completed!"
echo "Details about the output backup file:"
ls -l $output
else
echo "Backup of $input failed!"
fi
}
for directory in $*; do
backup $directory
let all=$all+$arch_files+$arch_directories
done;
echo "TOTAL FILES AND DIRECTORIES: $all"
Output:
[ec2-user@ip-172-31-41-211 Training_bash_scripts]$ sudo ./backup_3.sh ec2-user rahul
########## ec2-user ##########
Files to be included: 87
Directories to be included: 23
Files archived: 87
Directories archived: 23
Backup of /home/ec2-user completed!
Details about the output backup file:
-rw-r--r-- 1 root root 80088 Feb 23 16:54 /tmp/ec2-user_home_2021-02-23_165437.tar.gz
########## rahul ##########
Files to be included: 4
Directories to be included: 1
Files archived: 4
Directories archived: 1
Backup of /home/rahul completed!
Details about the output backup file:
-rw-r--r-- 1 root root 514 Feb 23 16:54 /tmp/rahul_home_2021-02-23_165437.tar.gz
TOTAL FILES AND DIRECTORIES: 115
Emperor Casino - Shootercasino
ReplyDeleteWith a 100% up to 샌즈카지노 $750 Welcome Bonus, play online casino with best games and bonuses! Play 인카지노 at the 제왕카지노 best casino online! Rating: 5 · 1 vote