Bash Scripting
Class 30th Jenkins May 24th
What is Script and why it is?
Type of Scripting language
Understand "Shebang(#!)""
Difference Between sh shell.sh,bash shell.sh, &./Shell.sh in Bash scripting.
Working with Bash scripting
Logical operators (AND(&&), OR(||)
File operations
Loops(For,while,Until)
Functions
Working with processes
What is Script and why it is?
Type of Scripting language
Understand "Shebang(#!)""
Difference Between sh shell.sh,bash shell.sh, &./Shell.sh in Bash scripting.
Working with Bash scripting
Logical operators (AND(&&), OR(||)
File operations
Loops(For,while,Until)
Functions
Working with processes
Shell script: shell script is series of command, Amazon Linux (they combined bash and shell inherited )
so it will support what every give command whether bash/shell
Amazon linux
[ec2-user@ip-10-0-1-88 ~]$ cd /bin
[ec2-user@ip-10-0-1-88 bin]$ls -lrt
lrwxrwxrwx 1 root root 4 May 28 02:01 sh -> bash
Debian Linux (separate there)
admin@ip-10-0-0-210:~$ cd /bin
admin@ip-10-0-0-210:/bin$ ls -lrt
-rwxr-xr-x 1 root root 1265648 Mar 29 2024 bash
lrwxrwxrwx 1 root root 4 Jan 5 2023 sh -> dash
Understand shebang(#!)
A shebang(#!)is a special directive at the top of a script that ells the system which interpreter(shell) to use when executing the script.
#!/bin/bash -->Use Bash shell to execute the script
#!/bin/sh -->Use sh shell(Could be dash on some systems).
#!/usr/bin/python3-->Use Python 3 interpreter.
Practical 1
Step1:
Amazon Linux
Example Bash script using shebang
#!/bin/bash
echo "Hello, Cloud computing!"
[ec2-user@ip-10-0-1-88 ccit]$ sh hello.sh
Hello cloud computing
[ec2-user@ip-10-0-1-88 ccit]$ bash hello.sh
Hello cloud computing
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
-bash: ./hello.sh: Permission denied
Getting error because of it is not execute mode ,give permission using chmod
[ec2-user@ip-10-0-1-88 ccit]$ ls -lrt
-rw-rw-r-- 1 ec2-user ec2-user 41 May 31 09:07 hello.sh
[ec2-user@ip-10-0-1-88 ccit]$ chmod +x hello.sh
[ec2-user@ip-10-0-1-88 ccit]$ ls -lrt
-rwxrwxr-x 1 ec2-user ec2-user 41 May 31 09:07 hello.sh
Now is is allowing because mode was changed
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Hello cloud computing
Step2:Changed script in bash to sh ,See here no difference shell , if you are give ./ this and try to execute interpreter will check what shebang used in the script
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/sh
echo "Hello cloud computing"
[ec2-user@ip-10-0-1-88 ccit]$ sh hello.sh
Hello cloud computing
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Hello cloud computing
[ec2-user@ip-10-0-1-88 ccit]$ bash hello.sh
Hello cloud computing
Step3: Array type script executing amazon linux and debian linux
amazon linux
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/sh
arr=(one two three)
echo ${arr[0]}
[ec2-user@ip-10-0-1-88 ccit]$ sh hello.sh
one
Debian Linux
Getting error
admin@ip-10-0-0-210:~/ccit$ cat hello.sh
#!/bin/sh
arr=(one two three)
echo ${arr[0]}
admin@ip-10-0-0-210:~/ccit$ sh hello.sh
hello.sh: 2: Syntax error: "(" unexpected
admin@ip-10-0-0-210:~/ccit$ bash hello.sh
one
admin@ip-10-0-0-210:~/ccit$ ./hello.sh
-bash: ./hello.sh: Permission denied
After given permission, after changed script sh to bash , shell no arrey concept only has bash
admin@ip-10-0-0-210:~/ccit$ chmod +x hello.sh
admin@ip-10-0-0-210:~/ccit$ ls -lrt
total 4
-rwxr-xr-x 1 admin admin 48 May 31 09:34 hello.sh
admin@ip-10-0-0-210:~/ccit$ cat hello.sh
#!/bin/bash
arr=(one two three)
echo ${arr[0]}
See below with bash both working ,if you explicitly run shell sh it will through error
admin@ip-10-0-0-210:~/ccit$ ./hello.sh
one
admin@ip-10-0-0-210:~/ccit$ bash hello.sh
one
admin@ip-10-0-0-210:~/ccit$ sh hello.sh
hello.sh: 2: Syntax error: "(" unexpected
I have changed back to sh ,see below sh interpreter trying run shell for the array it will not support getting ,previously same script run with bash.
admin@ip-10-0-0-210:~/ccit$ cat hello.sh
#!/bin/sh
arr=(one two three)
echo ${arr[0]}
admin@ip-10-0-0-210:~/ccit$ ./hello.sh
./hello.sh: 2: Syntax error: "(" unexpected
Practical 2
Inputs reading from the script
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash
echo "Please enter your name"
read uname
echo "The supplied user name is:$uname"
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Please enter your name
Raja
The supplied user name is:Raja
We can given directly using below script
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash
read -p "Please enter your name:" $uname
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Please enter your name:Suresh
Practical 3 : Athematic Operations
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash
num1=10
num2=5
echo "Sum of the numbers:"$((num1+$num2))
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Sum of the numbers:15
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash
num1=10
num2=5
echo "Sum of the numbers:"$((num1+$num2))
echo "Substraction of the numbers:"$((num1-$num2))
echo "Multiplication of the numbers:"$((num1*$num2))
echo "Division numbers:"$((num1/$num2))
echo "Sum of the numbers:"$((num1%$num2))
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Sum of the numbers:15
Substraction of the numbers:5
Multiplication of the numbers:50
Division numbers:2
Sum of the numbers:0
Practical 4 :
Conditional operations
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash
string1="ccit"
string2="ccit"
if [[ $string1==$string2 ]]; then
echo "both are same "
else
echo "both are not same"
fi
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
both are same
Practical 4:
if you are passing any value to variable that variable will change automatically
Number
Equal
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash
string1=5
string2=10
if [[ $string1 -eq $string2 ]]; then
echo "both are same "
else
echo "both are not same"
fi
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
both are not same
Not Equal
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash
string1=5
string2=10
if [[ $string1 -ne $string2 ]]; then
echo "both are not equal "
else
echo "both are same"
fi
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
both are not equal
Note:
String ==,!=
num -eq , -ne
less-than - -lt -le
Greater than -gt -ge
and &
or ||
And operator and OR Operator
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash
echo "Enter username"
read uname
echo "Enter password"
read pass
if [[ $uname == "ccit" && $pass == "123" ]]; then
echo "Valid user"
else
echo "Invalid user"
fi
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Enter username
ccit
Enter password
123
Valid user
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Enter username
ccit
Enter password
345
Invalid user
OR Operator
if [[ $uname == "ccit" || $pass == "123" ]]; then
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Enter username
ccit
Enter password
345
Valid user
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Enter username
raja
Enter password
123
Valid user
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
Enter username
raja
Enter password
34
Invalid user
File exist or not current directory
[ec2-user@ip-10-0-1-88 ccit]$ cat ./hello.sh
#!/bin/bash
filepath="./file.txt"
if [[ -e $filepath ]]; then
echo "file exists"
else
echo "file does not exists"
fi
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
file does not exists
[ec2-user@ip-10-0-1-88 ccit]$ touch file.txt
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
file exists
Looping statements
For Loop
for i (1..23);
done
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash
for i in {1..5}; do
echo "the value is :"$i
done
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
the value is :1
the value is :2
the value is :3
the value is :4
the value is :5
while Loop
[ec2-user@ip-10-0-1-88 ccit]$ cat hello.sh
#!/bin/bash
count=0
while [[ $count -lt 10 ]]; do
echo "the values is :"$count
count=$((count+1))
done
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
the values is :0
the values is :1
the values is :2
the values is :3
the values is :4
the values is :5
the values is :6
the values is :7
the values is :8
the values is :9
For -le lessthan equal to
while [[ $count -le10 ]]; do
[ec2-user@ip-10-0-1-88 ccit]$ ./hello.sh
the values is :0
the values is :1
the values is :2
the values is :3
the values is :4
the values is :5
the values is :6
the values is :7
the values is :8
the values is :9
the values is :10
--Thanks
No comments:
Post a Comment