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

Linux basic commands

 Linux basic commands: du  -sh  *  |  sort  -h  -r   |  head  -n  40  :    list out first 40 files in the directory that are taking more space in the directory.  cd : change directory Is-l listing the items in long listing format  pwd : print working directory Is-I format: type :no of links:owner : group:size :month :day :time :name cd/: go to/directory whoami: tells us by which username we are logged in. touch jerry: creates the file named jerry in present working directory. cp jerry lex: copy the content of jerry file and paste it to lex file. vi text1: creates the file text1 and open it in vi editor mkdir superman: creates the directory called superman mkdir abc def  : creates 2 folder in one command. touch filename wont work in /etc/ folder if logged in by normal account. man cp: shows manual for cp command. echo "india is my country"> file1 puts the text in file1. rm filename: remove the filename  mv lex luther renames the file from lex to luther  mv luther /h

patching tasks

 Patching a Linux system is a critical task to ensure that the system remains secure, stable, and up-to-date with the latest features and fixes. Here’s a comprehensive guide to the tasks involved in Linux patching: 1. Pre-Patching Preparation Backup System : Ensure you have a full system backup, including critical data, configuration files, and applications. Test the backup to verify its integrity. Check Disk Space : Verify that you have enough disk space, particularly on /var , /tmp , and /boot partitions. Review Current Patch Level : Determine the current patch level and installed packages using package management tools like yum , apt , dpkg , or rpm . Check System Logs : Review system logs to identify any issues that might affect the patching process. Test in a Staging Environment : If possible, apply patches in a staging environment that mirrors production to identify potential issues. Notify Stakeholders : Inform stakeholders about the scheduled maintenance window and expected do