Anything you can run normally on the command line can be put into a bash script and it will do the same thing.
which $SHELL
# tells us that in which shell we are currently using
We will go through step wise step upside so just relax and try out everything that is written here.
echo "Hey everyone"
# it displays 'Hey everyone'
Let's make it automated via bash/shell scripting, Here we use Vim editor -
vi f1.sh
#by typing this you can write into the file
#!/bin/bash - this is called 'shebang' this is important to run a bash script
#!/bin/bash
echo "Hey everyone"
#save the file
Here we use the 'vi' text editor by which we can write into the file, to get to know more about it - how to use vi editor' check out my blog on Linux.
to run the script type in the terminal
bash f1.sh #by typing this script will start to execute
You Like it ๐ ??? Let's move further...
Now automate it more :
'sleep 3' means stop executing commands for 3 seconds, we can replace '3' with any no.
vi f2.sh
#text editor opens up
#!/bin/bash
echo "Hey Stalker"
sleep 3 #by typing this your stop executing for 3 seconds you can change it accordingly
echo "Ab idhr aa hi gye ho to subscribe krke jana "
sleep 2 # stop executing for 2 sec
echo " are yaar kuch to sharam krle subscribe karr !!!"
#save the file
you can also run the script by typing :
./f2.sh
you maybe encountered some error like 'permission denied 'like this :
to solve this error you have to modifying the file permissions by:
chmod 700 f2.sh
#means for 'user' you have permissions to 'read ' 'write' and 'execute' the file
now the issue is resolved now you can run the file by typing :
./f2.sh
#this time it will be run
so if next time this command will not work then try the 'chmod' command.
Variables-
vi f3.sh
#text editor opens up type 'i' to insert
#!/bin/bash
name="Dhoni"#this is called variable
echo "kya kar rhe ho $name "#here we use $name variable
sleep 1
echo "kuch nhi $name blog likh rha hu bash scipting pe"
#save the file
now run this script :
chmod 700 f3.sh
./f3.sh
#to run the script
Alright now let's move further :
Now we take input from the user who runs the script-
vi f4.sh
#!/bin/bash
echo "Hey Mr. what's your name ?"
read name
sleep 2
echo "Alright $name nice name btw !!"
sleep 1
echo "Ok $name remember one thing 'assan hai!!!'"
#SAVE THIS FILE
Now run the script by :
chmod 700 f4.sh #to make it executable
./f4.sh
Arguments -
When we run the script, by giving the arguments then it added to the place holder like '$1' and '$2'
vi f5.sh
#!/bin/bash
age=$1 #positionl parameter (aka Argments)
place=$2 #do no leave any space there otherwise script may not be run
echo "You know i am $age years old"
sleep 1
echo "Btw where you from ?"
sleep 1
echo "I am from $place "
sleep 1
echo "Oh!!! wow that's a great place"
#save the file
now run the script we have to add arguments here
./f5.sh 21 Himachal # '21' and 'Himachal' are called arguments.
Run various commands on script :
vi f6.sh
#text editor open
#!/bin/bash
user=$(whoami)
date=$(date)
whereiam=$(pwd)
sleep 3
echo "You are currently logged in as $user and you are in $whereiam directory and today is $date"
Run the script by :
chmod 700 f6.sh
./f6.sh
$RANDOM-
New Variable
$RANDOM - pre-built-in variable in the Linux
Make a variable -
TWITTER ="helloabhii" #vairable created / Variable should be in capital letter
echo $TWITTER
helloabhii #printed by machine
#it cannnot be run now in our script because of child process
export twitter
e
Now it can be run in the bash script, but only till the session is terminated
To make a permanent variable
make a flow here Abhi
vi f7.sh
#open the editor
#!/bin/bash
echo "How old are you ?"
read age
echo "You are $age years old ?"
sleep 2
echo "calculating"
sleep 1 #this is to look's cool
echo "........."
sleep 1
echo "*......"
echo "**....."
echo "***...."
sleep 2
echo "*******"
getrich=$((($RANDOM % 15 ) + $age ))
echo "You will be become a millionare when you are $getrich years old "
Conditions-
conditionals and if statement:
vi f8.sh
#!/bin/bash
echo "Hey do you like grapes ? (y/n)"
read grapes
if [[ $grapes == "y" ]]; then
echo "amazing shibey!!!"
else
echo "i don't want to talk to you."
fi #opposite of if
#save the file
Let's see another one #fun game:
vi f9.sh
#!/bin/bash
#first beast battle
beast=$(($RANDOM % 2 )) #means give random no. less than 2
echo "Your first beast approach. Prepare for the battle. Pick a number
between 0-1.(0/1)"
read tarnished
if [[ $beast == $tarnished ]]; then
echo "Beast VANQUISHED!!! Congrats fellow tarnished"
else
echo "You died"
fi
In bash scripting, we can reassign the variables
vi f10.sh
#!/bin/bash
#first beast battle
beast=$(($RANDOM % 2 )) #means give random no. less than 2
echo "Your first beast approach. Prepare for the battle. Pick a number
between 0-1.(0/1)"
read tarnished
if [[ $beast == $tarnished ]]; then
echo "Beast VANQUISHED!!! Congrats fellow tarnished"
else
echo "You died"
fi
sleep 2
echo "Boss battle.Get scared. It's Magrit.Pick a number between 0-9.(0-9)"
read tarnished
beast=$(($RANDOM % 10))
if [[ $beast == $tarnished ]]; then
echo "Beast VANQUISHED !!! congrat's fellow tarnished"
else
echo "You Died !!!"
fi
Other conditions in Bash Scripting-
or-
if [[ $beast == $tarnished || $tarnished == "toffee" ]]; then
#these '||' means 'or'
# means either 1 is true or 2nd is true.
#you have to try it by youself chnage the 'last 5th line '
and-
if [[ $beast == $tarnished && 47>23 ]]; then
Nested If Statement (Bash Conditionals)-
vi f11.sh
#!/bin/bash
#first beast battle
beast=$(($RANDOM % 2 )) #means give random no. less than 2
echo "Your first beast approach. Prepare for the battle. Pick a number
between 0-1.(0/1)"
read tarnished
if [[ $beast == $tarnished ]]; then
echo "Beast VANQUISHED!!! Congrats fellow tarnished"
else
echo "You died"
fi
sleep 2
echo "Boss battle.Get scared. It's Magrit.Pick a number between 0-9.(0-9)"
read tarnished
beast=$(($RANDOM % 2))
if [[ $beast == $tarnished || $arnished == "coffee" ]]; then
if [[ $user == "root" ]]; then #if you are root user beast automatically died
echo "Beast VANQUISHED !!"
fi
else
echo "You Died !!!"
fi
elif statement-
g
vi f12.sh
#!/bin/bash
#first beast battle
beast=$(($RANDOM % 2 )) #means give random no. less than 2
echo "Your first beast approach. Prepare for the battle. Pick a number
between 0-1.(0/1)"
read tarnished
if [[ $beast == $tarnished ]]; then
echo "Beast VANQUISHED!!! Congrats fellow tarnished"
else
echo "You died"
fi
sleep 2
echo "Boss battle.Get scared. It's Magrit.Pick a number between 0-9.(0-9)"
read tarnished
beast=$(($RANDOM % 2))
if [[ $beast == $tarnished || $arnished == "coffee" ]]; then
echo "Beast VANQUISHED !!"
elif [[ $user == "helloabhii" ]]; then #helloabhii is my machine username
echo "Hey Abhii !!! for you always wins, You VANQUISHED Beast !!!"
else
echo "You Died !!!"
fi
till the username is 'helloabhii' you always win
case statement(switch)-
vi f13.sh
#!/bin/bash
echo "Do you know Java Programming?"
read -p "Yes/No? :" Answer
case $Answer in
Yes|yes|y|Y)
echo "That's amazing."
echo
;;
No|no|N|n)
echo "It's easy. Let's start learning from javatpoint."
;;
esac
loops-
for loop-
#!/bin/bash
for x in 1 2 3 4 5 6 7 8 9 10 ; #cups= variable 3
do
echo "Hey, You're had $x cup of coffee today."
done
you can write line no.2 as ' for cups in {1..10}; 'also
while loop-
#!/bin/bash
x=1
while [[ $x -1e 100 ]]
do
echo "Hey, I just drink $x glasses of water"
(( x++ ))
done
#!/bin/bash
x=1
while [[ $x -le 100 ]] #this is also while loop
do
read -p "glass $x : Press enter to continue"
(( x++ ))
done
echo "Hey, I just drink $x glasses of water"
until loop-
executes until a specific command
#!/bin/bash
until [[ $order == "water" ]]
do
echo "Would you like juice or water ?"
read order
done
echo "Excellent choice, here is your water"
break and continue-
'break' will break the loop and 'continue' will skip that particular iteration
vi f14.sh
for i in {1..10}
do
if [ $i -eq 3]
then
break #/continue same as
fi
echo $i
done
function-
we can create a function in bash script also as in other languages.
#!/bin/bash
function sayHello() {
echo "Hey abhii!!!"
}
#here sayHello is function
sayHello
we've reached the end of this topic
I hope you all like it and we will get back soon with the next topic.
Thank you for reading this. โค๏ธ