Skip to main content

Shell Scripting basics

 


Shell:


Its like a container.

interface between users and kernal / OS.

CLI is a shell

Find your shell:

echo $0: gives the output which shell you are using. cat /etc/shells: gives the output of available shells.

your shell? /etc/passwd


................................. 

Shell Scripting:

What is shell Script?

Shell Script is the executable file containing multiple shell commands that are executed sequentially. The file can contain:

1. shell

2. comments

3. commands

4. statements




shell script should have executable permissions.

shell script has to be called absolute path.

if it is called from the current location then, ./script.sh


..................................... 


Shell Script-Basic Scripts:

Question: Write the script to print "hello world" on the terminal. Answer:

content of script1.sh should be:


#!/bin/bash 
echo Hello World

chmod a+x script1.sh: give the executible permission to all for this script.

/script1.sh: run the script.


....,...................... 

Question: Defining Tasks


#!/bin/bash

# Define small tasks

whoami

echo             #echo command will give the space in the output.

pwd

echo

hostname

echo

Is-ltr

echo

............................ 

Question: Defining variables


#!/bin/bash

# Example of defining variables

a=Imran

b=Afzal

c='Linux class'

echo "My first name is $a"

echo "My surname is $b"

echo 'My class is $c'

...................................... 

Question: Read Input


#!/bin/bash

# Read user input

echo "What is your first name?"

read a

echo

echo "What is your last name?"

read b

echo

echo Hello $a $b



........................... 


Question: Scripts to run commands within. 


#!/bin/bash
#Script to run commands within
clear 
echo "Hello `whoami`"
echo
echo "Today is `date`" 
echo
echo "Number of user login: `who | wc -1`"
echo


.................. 

Question : rename the filename. 



#!/bin/bash

#This script will rename a file

echo Enter the file name to be renamed

read oldfilename

echo Enter the new file name

read newfilename

mv $oldfilename $newfilename

echo The file has been renamed as $newfilename


.......,.................... 


If-then Scripts:



Question: Check the variable


#!/bin/bash 
count=100 
if [ $count -eq 100 ]
then

echo Count is 100
else
echo Count is not 100 
fi

***********************************


Question: Check if a file error.txt exist


#!/bin/bash
clear 
if [ -f /home/iafzal/error.txt ] 
then
echo "File exist" 
else
echo "File does not exist "
fi


†********************************

Question:Check if a variable value is met



#!/bin/bash
a=`date | awk '{print $1}'`
if [ "$a" == Mon]
then
echo Today is $a
else
echo Today is not Monday
fi





************************************

Question: Check the response and then output



#!/bin/bash

clear

echo

echo "What is your name?"

echo

read a

echo

echo Hello $a sir

echo

echo "Do you like working in IT? (y/n)"

read Like

echo

if [ "$Like" == y ]

then

echo You are cool

elif [ "$Like" == n]

then

echo You should try IT. it's a good field

echo

fi


******************************


For loop Scripts: 





Simple for loop output 



#!/bin/bash
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done

***********************************

Simple for loop output




#!/bin/bash
 for i in eat run jump play
do
echo See Imran $i 
done


*************************************
Question:
for loop to create 5 files named 1-5

#!/bin/bash 
for i in (1..5)

do

touch $i
done

************************************

Question:
for loop to delete 5 files named 1-5

#!/bin/bash 
for i in (1..5)

do

rm $i

done



**************************************


Specify days in for loop



#!/bin/bash

 i=1

for day in Mon Tue Wed Thu Fri

do

echo "Weekday $((i++)): $day"

done


*******************************†******

List all users one by one from /etc/passwd file

#!/bin/bash

i=1

for username in `awk -F: '{print $1)' /etc/passwd

do

echo "Username $((i++)): $username"

done

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>





Comments

Popular posts from this blog

Post build configurations on Redhat VM

  ************************************* Post build configuration on Linux VM: ************************************* Set the hostname. hostnamectl set-hostname <Servername> ***************************************** Network configuration : Make sure VM gets the ipv4 ip address either from DHCP or assign the static ip address to it.   Question : How to assign the static ip address to the linux machine using the nmcli ?  Answer: fire the below commands: nmcli device nmcli connection modify enpos3 ipv4.addresses 10.253.1.34/24 nmcli connection modify enpos3 ipv4.gateway 10.253.1.1 nmcli connection modify enpos3 ipv4.method manual nmcli connection modify enpos3 ipv4.dns 8.8.8.8 nmli connection down enpos3 nmcli connection up enpos3 ip address show enpos3 nmcli connection show ************************†****************** Register to RHΝ. Register to redhat network if the linux vm is redhat vm. subscription-manager register --org <org>  --activationkey <activat...

AWS cloud practitioner notes

 AWS Certified cloud practitioner: What is cloud computing? cloud computing is the on demand delivery of the compute power, database storage, applications and other IT resources through a cloud services platform with pay-as-you-go pricing. you can provision exactly the right type and size of the computing resources you need. you can access many resources as you need almost instantly like servers, storage,databases and application services as well. Amazon web services owns and maintains the network connected hardware required for these application services, while you provision and use what you need via a web application. ***************************************** Deployment models of Cloud: Private Cloud: Cloud service used by a single organisation, not exposed to the public. complete control. security of the sensitive applications meeting specific business needs. ********************** Public Cloud: Cloud resources owned and operated by the third party. cloud service provider delive...