Skip to content

NIKILADEVARAJ/OS-Linux-commands-Shell-script

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 

Repository files navigation

Operating systems Lab exercise

Linux commands-Shell scripting

Linux commands-Shell scripting

AIM:

To practice Linux Commands and Shell Scripting

DESIGN STEPS:

Step 1:

Navigate to any Linux environment installed on the system or installed inside a virtual environment like virtual box/vmware or online linux JSLinux (https://bellard.org/jslinux/vm.html?url=alpine-x86.cfg&mem=192) or docker.

Step 2:

Execute the following commands

Step 3:

Testing the commands for the desired output.

COMMANDS:

Create the following files file1, file2 as follows:

cat > file1

chanchal singhvi
c.k. shukla
s.n. dasgupta
sumit chakrobarty
^d

cat > file2

anil aggarwal
barun sengupta
c.k. shukla
lalit chowdury
s.n. dasgupta
^d

Display the content of the files

cat < file1

OUTPUT

WhatsApp Image 2025-09-28 at 10 55 05_28884e70

cat < file2

OUTPUT

WhatsApp Image 2025-09-28 at 10 52 23_ea385d49

Comparing Files

cmp file1 file2

OUTPUT

WhatsApp Image 2025-09-28 at 10 56 22_11d1556b comm file1 file2

OUTPUT

WhatsApp Image 2025-09-28 at 10 57 56_248a36fb

diff file1 file2

OUTPUT

WhatsApp Image 2025-09-28 at 10 59 15_9ec5bb29

#Filters

Create the following files file11, file22 as follows:

cat > file11

Hello world
This is my world
^d

cat > file22

1001 | Ram | 10000 | HR
1002 | tom |  5000 | Admin
1003 | Joe |  7000 | Developer
^d

cut -c1-3 file11

OUTPUT

WhatsApp Image 2025-09-28 at 11 00 33_714138b9

cut -d "|" -f 1 file22

OUTPUT

WhatsApp Image 2025-09-28 at 11 01 18_26fa5ec4

cut -d "|" -f 2 file22

OUTPUT

WhatsApp Image 2025-09-28 at 11 02 06_427f7904

cat < newfile

Hello world
hello world
^d

cat > newfile Hello world hello world

OUTPUT

WhatsApp Image 2025-09-28 at 11 03 29_a1fc5459

grep Hello newfile

OUTPUT

WhatsApp Image 2025-09-28 at 11 05 10_1b16b51e

grep hello newfile

OUTPUT

WhatsApp Image 2025-09-28 at 11 05 23_649c0e80

grep -v hello newfile

OUTPUT

WhatsApp Image 2025-09-28 at 11 06 23_6a071847

cat newfile | grep -i "hello"

OUTPUT

WhatsApp Image 2025-09-28 at 11 07 39_e287eb64

cat newfile | grep -i -c "hello"

OUTPUT

WhatsApp Image 2025-09-28 at 11 07 28_50658503

grep -R parrot /etc

OUTPUT

image

grep -w -n world newfile

OUTPUT

WhatsApp Image 2025-09-28 at 12 42 18_893ffaa3

cat < newfile

Hello world
hello world
Linux is world number 1
Unix is predecessor
Linux is best in this World
^d

cat > newfile

Hello world
hello world
Linux is world number 1
Unix is predecessor
Linux is best in this World
^d

egrep -w 'Hello|hello' newfile

OUTPUT

WhatsApp Image 2025-09-28 at 12 44 28_8cab69e4

egrep -w '(H|h)ello' newfile

OUTPUT

WhatsApp Image 2025-09-28 at 12 45 01_a9b6ce99

egrep -w '(H|h)ell[a-z]' newfile

OUTPUT

WhatsApp Image 2025-09-28 at 12 45 26_43bd2544

egrep '(^hello)' newfile

OUTPUT

WhatsApp Image 2025-09-28 at 12 45 57_2ad78dbd

egrep '(world$)' newfile

OUTPUT

WhatsApp Image 2025-09-28 at 12 46 25_1117dda5

egrep '(World$)' newfile

OUTPUT

WhatsApp Image 2025-09-28 at 12 46 54_33ac2936

egrep '((W|w)orld$)' newfile

OUTPUT

WhatsApp Image 2025-09-28 at 12 47 32_0e8c06ba

egrep '[1-9]' newfile

OUTPUT

WhatsApp Image 2025-09-28 at 12 48 01_e73b6c06

egrep 'Linux.*world' newfile

OUTPUT

WhatsApp Image 2025-09-28 at 12 48 29_d8d0c27b

egrep 'Linux.*World' newfile

OUTPUT

WhatsApp Image 2025-09-28 at 12 48 51_cd13d069

egrep l{2} newfile

OUTPUT

WhatsApp Image 2025-09-28 at 12 49 38_df4ad6d8

egrep 's{1,2}' newfile

OUTPUT

WhatsApp Image 2025-09-28 at 12 50 25_3406bf72

cat > file23

1001 | Ram | 10000 | HR
1001 | Ram | 10000 | HR
1002 | tom |  5000 | Admin
1003 | Joe |  7000 | Developer
1005 | Sam |  5000 | HR
1004 | Sit |  7000 | Dev
1003 | Joe |  7000 | Developer
1001 | Ram | 10000 | HR
^d

sed -n -e '3p' file23

OUTPUT

WhatsApp Image 2025-09-28 at 12 52 01_d69b32db

sed -n -e '$p' file23

OUTPUT

WhatsApp Image 2025-09-28 at 12 52 50_8ca398b5

sed -e 's/Ram/Sita/' file23

OUTPUT

WhatsApp Image 2025-09-28 at 12 53 47_90e525d9

sed -e '2s/Ram/Sita/' file23

OUTPUT

WhatsApp Image 2025-09-28 at 12 54 41_e42e68a4

sed '/tom/s/5000/6000/' file23

OUTPUT

WhatsApp Image 2025-09-28 at 12 55 24_11ecd4a4

sed -n -e '1,5p' file23

OUTPUT

WhatsApp Image 2025-09-28 at 12 57 07_a5a5ab73

sed -n -e '2,/Joe/p' file23

OUTPUT

WhatsApp Image 2025-09-28 at 12 58 18_c05e9eb5

sed -n -e '/tom/,/Joe/p' file23

OUTPUT

WhatsApp Image 2025-09-28 at 12 58 58_316331d4

seq 10

OUTPUT

WhatsApp Image 2025-09-28 at 12 59 52_fa136b2c

seq 10 | sed -n '4,6p'

OUTPUT

WhatsApp Image 2025-09-28 at 13 01 00_02a05d4a

seq 10 | sed -n '2,~4p'

OUTPUT

WhatsApp Image 2025-09-28 at 13 01 35_e8cb905a

seq 3 | sed '2a hello'

OUTPUT

WhatsApp Image 2025-09-28 at 13 05 34_55ef1967

seq 2 | sed '2i hello'

OUTPUT

WhatsApp Image 2025-09-28 at 13 06 02_beecef1b

seq 10 | sed '2,9c hello'

OUTPUT

WhatsApp Image 2025-09-28 at 13 08 14_b52c1493

sed -n '2,4{s/^/$/;p}' file23

OUTPUT

WhatsApp Image 2025-09-28 at 13 08 35_322a7035

#Sorting File content cat > file21

1001 | Ram | 10000 | HR
1002 | tom |  5000 | Admin
1003 | Joe |  7000 | Developer
1005 | Sam |  5000 | HR
1004 | Sit |  7000 | Dev

sort file21

OUTPUT

WhatsApp Image 2025-09-28 at 13 10 57_3c232e4f

cat > file22

1001 | Ram | 10000 | HR
1001 | Ram | 10000 | HR
1002 | tom |  5000 | Admin
1003 | Joe |  7000 | Developer
1005 | Sam |  5000 | HR
1004 | Sit |  7000 | Dev

uniq file22

OUTPUT

WhatsApp Image 2025-09-28 at 13 13 04_748558fe

#Using tr command

cat file23 | tr [:lower:] [:upper:]

OUTPUT

catWhatsApp Image 2025-09-28 at 13 15 06_2d18bddc

cat < urllist.txt

www. yahoo. com
www. google. com
www. mrcet.... com
^d

cat > urllist.txt

www. yahoo. com
www. google. com
www. mrcet.... com

cat urllist.txt | tr -d ' '

OUTPUT

WhatsApp Image 2025-09-28 at 13 17 50_b6e74eda

cat urllist.txt | tr -d ' ' | tr -s '.'

OUTPUT

WhatsApp Image 2025-09-28 at 13 18 04_222180e1

#Backup commands tar -cvf backup.tar *

OUTPUT

WhatsApp Image 2025-09-28 at 13 21 42_fe299f63

mkdir backupdir

mv backup.tar backupdir

cd backupdir

tar -tvf backup.tar

OUTPUT

image

tar -xvf backup.tar

OUTPUT

WhatsApp Image 2025-09-28 at 13 23 43_ef60f9f0

gzip backup.tar

ls .gz

OUTPUT

WhatsApp Image 2025-09-28 at 13 24 47_26f6137e

gunzip backup.tar.gz

OUTPUT

WhatsApp Image 2025-09-28 at 13 26 59_fb4b47e7

Shell Script

echo '#!/bin/sh' > my-script.sh
echo 'echo Hello World‘; exit 0 >> my-script.sh

chmod 755 my-script.sh ./my-script.sh

OUTPUT

WhatsApp Image 2025-09-28 at 13 27 35_d7dd032b

cat << stop > herecheck.txt

hello in this world
i cant stop
for this non stop movement
stop

cat herecheck.txt

OUTPUT

WhatsApp Image 2025-09-28 at 13 30 02_5e9a28fa

cat < scriptest.sh

\#!/bin/sh
echo “File name is $0echo "File name is " `basename $0`
echo “First arg. is ” $1
echo “Second arg. is ” $2
echo “Third arg. is ” $3
echo “Fourth arg. is ” $4
echo 'The $@ is ' $@
echo 'The $\# is ' $1#
echo 'The $$ is ' $$
ps
^d

cat scriptest.sh

\#!/bin/sh
echo “File name is $0echo "File name is " `basename $0`
echo “First arg. is ” $1
echo “Second arg. is ” $2
echo “Third arg. is ” $3
echo “Fourth arg. is ” $4
echo 'The $@ is ' $@
echo 'The $\# is ' $\#
echo 'The $$ is ' $$
ps

chmod 777 scriptest.sh

./scriptest.sh 1 2 3

OUTPUT

WhatsApp Image 2025-09-28 at 13 48 17_78326827

ls file1

OUTPUT

WhatsApp Image 2025-09-28 at 13 48 38_0387690b

echo $?

OUTPUT

./one bash: ./one: Permission denied image

echo $?

OUTPUT

WhatsApp Image 2025-09-28 at 13 49 03_3215c1e8

abcd

echo $?

OUTPUT

WhatsApp Image 2025-09-28 at 13 49 27_5ca2bd11

mis-using string comparisons

cat < strcomp.sh

\#!/bin/bash
val1=baseball
val2=hockey
if [ $val1 \> $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi
^d

cat strcomp.sh

\#!/bin/bash
val1=baseball
val2=hockey
if [ $val1 \> $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi

##OUTPUT WhatsApp Image 2025-09-28 at 13 52 57_991a7ccb

chmod 755 strcomp.sh

./strcomp.sh

OUTPUT

WhatsApp Image 2025-09-28 at 13 51 28_613067e2

check file ownership

cat < psswdperm.sh

\#!/bin/bash
if [ -O /etc/passwd ]
then
echo “You are the owner of the /etc/passwd file”
else
echo “Sorry, you are not the owner of the /etc/passwd file”
fi
^d

cat psswdperm.sh

/#!/bin/bash
if [ -O /etc/passwd ]
then
echo “You are the owner of the /etc/passwd file”
else
echo “Sorry, you are not the owner of the /etc/passwd file”
fi

##OUTPUT WhatsApp Image 2025-09-28 at 13 54 48_0e9be609 ./psswdperm.sh

OUTPUT

WhatsApp Image 2025-09-28 at 13 55 00_cc4827bc

check if with file location

cat>ifnested.sh

\#!/bin/bash
if [ -e $HOME ]
then
echo$HOME The object exists, is it a file?if [ -f $HOME ]
then
echo “Yes,$HOME it is a file!else
echo “No,$HOME it is not a file!if [ -f $HOME/.bash_history ]
then
echo “But $HOME/.bash_history is a file!fi
fi
else
echo “Sorry, the object does not exist”
fi
^d

cat ifnested.sh

\#!/bin/bash
if [ -e $HOME ]
then
echo “$HOME The object exists, is it a file?”
if [ -f $HOME ]
then
echo “Yes,$HOME it is a file!”
else
echo “No,$HOME it is not a file!”
if [ -f $HOME/.bash_history ]
then
echo “But $HOME/.bash_history is a file!”
fi
fi
else
echo “Sorry, the object does not exist”
fi

./ifnested.sh

OUTPUT

WhatsApp Image 2025-09-28 at 13 57 51_239da918

using numeric test comparisons

cat > iftest.sh

\#!/bin/bash
val1=10
val2=11
if [ $val1 -gt 5 ]
then
echo “The test value $val1 is greater than 5”
fi
if [ $val1 -eq $val2 ]
then
echo “The values are equal”
else
echo “The values are different”
fi
^d

cat iftest.sh

\#!/bin/bash
val1=10
val2=11
if [ $val1 -gt 5 ]
then
echo “The test value $val1 is greater than 5”
fi
if [ $val1 -eq $val2 ]
then
echo “The values are equal”
else
echo “The values are different”
fi

$ chmod 755 iftest.sh

$ ./iftest.sh ##OUTPUT WhatsApp Image 2025-09-28 at 13 59 32_d68e4b49

check if a file

cat > ifnested.sh

\#!/bin/bash
if [ -e $HOME ]
then
echo$HOME The object exists, is it a file?if [ -f $HOME ]
then
echo “Yes,$HOME it is a file!else
echo “No,$HOME it is not a file!if [ -f $HOME/.bash_history ]
then
echo “But $HOME/.bash_history is a file!fi
fi
else
echo “Sorry, the object does not exist”
fi
^d

cat ifnested.sh

\#!/bin/bash
if [ -e $HOME ]
then
echo$HOME The object exists, is it a file?if [ -f $HOME ]
then
echo “Yes,$HOME it is a file!else
echo “No,$HOME it is not a file!if [ -f $HOME/.bash_history ]
then
echo “But $HOME/.bash_history is a file!fi
fi
else
echo “Sorry, the object does not exist”
fi

$ chmod 755 ifnested.sh

$ ./ifnested.sh ##OUTPUT WhatsApp Image 2025-09-28 at 14 03 30_9e32c766

looking for a possible value using elif

cat elifcheck.sh

\#!/bin/bash
if [ $USER = Ram ]
then
echo "Welcome $USER"
echo "Please enjoy your visit"
elif [ $USER = Rahim ]
then
echo "Welcome $USER"
echo "Please enjoy your visit"
elif [ $USER = Robert ]
then
echo "Special testing account"
elif [ $USER = gganesh ]
then
echo "$USER, Do not forget to logout when you're done"
else
echo "Sorry, you are not allowed here"
fi

$ chmod 755 elifcheck.sh

$ ./elifcheck.sh

OUTPUT

WhatsApp Image 2025-09-28 at 14 01 49_1539b12f

testing compound comparisons

cat> ifcompound.sh

\#!/bin/bash
if [ -d $HOME ] && [ -w $HOME ]
then
echo "The file exists and you can write to it"
else
echo "I cannot write to the file"
fi

$ chmod 755 ifcompound.sh $ ./ifcompound.sh

OUTPUT

WhatsApp Image 2025-09-28 at 14 05 10_e0d4506f

using the case command

cat >casecheck.sh

case $USER in
Ram | Robert)
echo "Welcome, $USER"
echo "Please enjoy your visit";;
Rahim)
echo "Special testing account";;
gganesh)
echo "$USER, Do not forget to log off when you're done";;
*)
echo "Sorry, you are not allowed here";;
esac

$ chmod 755 casecheck.sh

$ ./casecheck.sh WhatsApp Image 2025-09-28 at 14 06 07_0bc42c00

cat > whiletest

#!/bin/bash
#while command test
var1=10
while [ $var1 -gt 0 ]
do
echo $var1
var1=$[ $var1 - 1 ]
done

$ chmod 755 whiletest.sh

$ ./whiletest.sh WhatsApp Image 2025-09-28 at 14 07 14_be0675e1

cat untiltest.sh

\#using the until command
var1=100
until [ $var1 -eq 0 ]
do
echo $var1
var1=$[ $var1 - 25 ]
done

$ chmod 755 untiltest.sh

WhatsApp Image 2025-09-28 at 14 08 00_feeef4b1

cat forin1.sh

\#!/bin/bash
\#basic for command
for test in Alabama Alaska Arizona Arkansas California Colorado
do
echo The next state is $test
done

$ chmod 755 forin1.sh

WhatsApp Image 2025-09-28 at 14 09 13_995ad4c1

cat forin2.sh

\#!/bin/bash
\# another example of how not to use the for command
for test in I don't know if this'll work
do
echo “word:$testdone

$ chmod 755 forin2.sh

cat forin2.sh WhatsApp Image 2025-09-28 at 14 09 34_f401ae7a

\#!/bin/bash
\# another example of how not to use the for command
for test in I don't know if this'll work
do
echo “word:$testdone

$ chmod 755 forin2.sh

$ ./forin2.sh

cat forin3.sh WhatsApp Image 2025-09-28 at 14 11 29_fa6914a0

\#!/bin/bash
\# another example of how not to use the for command
for test in I don\'t know if "this'll" work
do
echo "word:$test"
done

$ ./forin3.sh

cat forin1.sh WhatsApp Image 2025-09-28 at 14 12 31_5b47c856

#!/bin/bash
# basic for command
for test in Alabama Alaska Arizona Arkansas California Colorado
do
echo The next state is $test
done

$ chmod 755 forin1.sh

OUTPUT

WhatsApp Image 2025-09-28 at 14 16 11_4a549af9

cat forinfile.sh

#!/bin/bash
# reading values from a file
file="cities"
for state in `cat $file`
do
echo "Visit beautiful $file
done

$ chmod 777 forinfile.sh $ cat cities Hyderabad Alampur Basara Warangal Adilabad Bhadrachalam Khammam

OUTPUT

WhatsApp Image 2025-09-28 at 14 15 52_cb5dacab

cat forctype.sh

#!/bin/bash
# testing the C-style for loop
for (( i=1; i <= 5; i++ ))
do
echo "The value of i is $i"
done

$ chmod 755 forctype.sh $ ./forctype.sh

OUTPUT

WhatsApp Image 2025-09-28 at 14 19 19_55d5476e

cat forctype1.sh

#!/bin/bash
# multiple variables
for (( a=1, b=5; a <= 5; a++, b-- ))
do
echo "$a - $b"
done

$ chmod 755 forctype.sh $ ./forctype1.sh

OUTPUT

WhatsApp Image 2025-09-28 at 14 17 32_22a1895b cat fornested1.sh

#!/bin/bash
# nesting for loops
for (( a = 1; a <= 3; a++ ))
do
echo "Starting loop $a:"
for (( b = 1; b <= 3; b++ ))
do
echo " Inside loop: $b"
done
done

$ chmod 755 fornested1.sh

$ ./fornested1.sh

OUTPUT

WhatsApp Image 2025-09-28 at 14 20 03_b9da08b9

cat forbreak.sh

#!/bin/bash
# breaking out of a for loop
for var1 in 1 2 3 4 5
do
if [ $var1 -eq 3 ]
then
break
fi
echo "Iteration number: $var1"
done
echo "The for loop is completed“

OUTPUT

WhatsApp Image 2025-09-28 at 14 20 45_ac151c27

$ chmod 755 forbreak.sh

$ ./forbreak.sh

cat forbreak.sh

#!/bin/bash
# breaking out of a for loop
for var1 in 1 2 3 4 5
do
if [ $var1 -eq 3 ]
then
continue
fi
echo "Iteration number: $var1"
done
echo "The for loop is completed“

$ chmod 755 forcontinue.sh

$ ./forcontinue.sh

OUTPUT

WhatsApp Image 2025-09-28 at 14 21 36_9adcdc0c

cat exread.sh

#!/bin/bash
# testing the read command
echo -n "Enter your name: "
read name
echo "Hello $name, welcome to my program. "

$ chmod 755 exread.sh

$ ./exread.sh

OUTPUT

WhatsApp Image 2025-09-28 at 14 22 19_243b31dd

cat exread1.sh

#!/bin/bash
# testing the read command
read -p "Enter your name: " name
echo "Hello $name, welcome to my program. “

$ chmod 755 exread1.sh

OUTPUT

WhatsApp Image 2025-09-28 at 14 23 07_c883219a

$ ./exread1.sh

cat funcex.sh

#!/bin/bash
# trying to access script parameters inside a function
function func {
echo $[ $1 * $2 ]
}
if [ $# -eq 2 ]
then
value=`func $1 $2`
echo "The result is $value"
else
echo "Usage: badtest1 a b"
fi

OUTPUT

./funcex.sh

WhatsApp Image 2025-09-28 at 14 24 12_bd573391

./funcex.sh 1 2

cat argshift.sh

#!/bin/bash 
 while (( "$#" )); do 
  echo $1 
  shift 
done

$ chmod 777 argshift.sh

OUTPUT

$ ./argshift.sh 1 2 3 WhatsApp Image 2025-09-28 at 14 26 30_63728ee1

cat argshift1.sh

 #/bin/bash 
 # store arguments in a special array 
args=("$@") 
# get number of elements 
ELEMENTS=${#args[@]} 
 # echo each element in array  
# for loop 
for (( i=0;i<$ELEMENTS;i++)); do 
    echo ${args[${i}]} 
done

$ chmod 777 argshift.sh

OUTPUT

$ ./argshift.sh 1 2 3 WhatsApp Image 2025-09-28 at 14 26 30_63728ee1

cat argshift.sh

#!/bin/bash 
set -x 
while (( "$#" )); do 
  echo $1 
  shift 
done
set +x

OUTPUT

./argshift.sh 1 2 3 WhatsApp Image 2025-09-28 at 14 27 44_c9bec06d

cat > nc.awk

BEGIN{}
{
print len=length($0),"\t",$0 
wordcount+=NF
chrcnt+=len
}
END {
print "total characters",chrcnt 
print "Number of Lines are",NR
print "No of Words count:",wordcount
}

cat>data.dat

bcdfghj
abcdfghj
bcdfghj
ebcdfghj
bcdfghj
ibcdfghj
bcdfghj
obcdfghj
bcdfghj
ubcdfghj

awk -f nc.awk data.dat

OUTPUT

WhatsApp Image 2025-09-28 at 14 28 35_ac007caf

cat > palindrome.sh

#num=545
echo "Enter the number"
read num
s=0
rev=""
temp=$num
while [ $num -gt 0 ]
do
	# Get Remainder
	s=$(( $num % 10 ))
	# Get next digit
	num=$(( $num / 10 ))
	# Store previous number and
	# current digit in reverse
	rev=$( echo ${rev}${s} )
done
if [ $temp -eq $rev ];
then
	echo "Number is palindrome"
else
	echo "Number is NOT palindrome"
fi

OUTPUT

WhatsApp Image 2025-09-28 at 14 29 17_93596289

RESULT:

The Commands are executed successfully.

About

Operating systems Lab exercise

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published