Skip to content

Aravind1318/OS-Linux-commands-Shell-script

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 

Repository files navigation

OS-Linux-commands-Shell-scripting

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

Screenshot from 2025-04-25 20-48-26

cat < file2

OUTPUT

Screenshot from 2025-04-25 20-49-29

Comparing Files

cmp file1 file2

OUTPUT

Screenshot from 2025-04-25 20-50-30

comm file1 file2

OUTPUT

Screenshot from 2025-04-25 20-51-16

diff file1 file2

OUTPUT

Screenshot from 2025-04-25 20-51-53

#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

Screenshot from 2025-04-25 20-54-35

cut -d "|" -f 1 file22

OUTPUT

Screenshot from 2025-04-25 20-56-30

cut -d "|" -f 2 file22

OUTPUT

Screenshot from 2025-04-25 20-57-59

cat < newfile

Hello world
hello world
^d

cat > newfile Hello world hello world

grep Hello newfile

OUTPUT

Screenshot from 2025-04-25 21-00-00

grep hello newfile

OUTPUT

Screenshot from 2025-04-25 21-01-01

grep -v hello newfile

OUTPUT

Screenshot from 2025-04-25 21-01-18

cat newfile | grep -i "hello"

OUTPUT

Screenshot from 2025-04-25 21-01-42

cat newfile | grep -i -c "hello"

OUTPUT

Screenshot from 2025-04-25 21-02-14

grep -w -n world newfile

OUTPUT

Screenshot from 2025-04-25 21-02-47

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

Screenshot from 2025-04-25 21-04-39

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

OUTPUT

Screenshot from 2025-04-25 21-05-10

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

OUTPUT

Screenshot from 2025-04-25 21-05-39

egrep '(^hello)' newfile

OUTPUT

Screenshot from 2025-04-25 21-06-27

egrep '(world$)' newfile

OUTPUT

egrep '(World$)' newfile

OUTPUT

Screenshot from 2025-04-25 15-49-47

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

OUTPUT

Screenshot from 2025-04-25 15-50-18

egrep '[1-9]' newfile

OUTPUT

Screenshot from 2025-04-25 15-50-50

egrep 'Linux.*world' newfile

OUTPUT

Screenshot from 2025-04-25 15-51-12

egrep 'Linux.*World' newfile

OUTPUT

egrep l{2} newfile

OUTPUT

egrep 's{1,2}' newfile

OUTPUT

Screenshot from 2025-04-25 15-51-35

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

Screenshot from 2025-04-27 12-00-10

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

OUTPUT

Screenshot from 2025-04-27 12-01-25

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

OUTPUT

Screenshot from 2025-04-27 12-01-44

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

OUTPUT

Screenshot from 2025-04-27 12-02-15

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

OUTPUT

Screenshot from 2025-04-27 12-02-46

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

OUTPUT

Screenshot from 2025-04-27 12-03-04

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

OUTPUT

Screenshot from 2025-04-27 12-03-25

seq 10

OUTPUT

Screenshot from 2025-04-27 12-03-40

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

OUTPUT

Screenshot from 2025-04-27 12-03-59

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

OUTPUT

Screenshot from 2025-04-27 12-04-14

seq 3 | sed '2a hello'

OUTPUT

Screenshot from 2025-04-27 12-04-39

seq 2 | sed '2i hello'

OUTPUT

Screenshot from 2025-04-27 12-04-58

seq 10 | sed '2,9c hello'

OUTPUT

Screenshot from 2025-04-27 12-05-13

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

OUTPUT

Screenshot from 2025-04-27 12-05-28

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

OUTPUT:

Screenshot from 2025-04-27 12-05-42

#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

Screenshot from 2025-04-27 12-06-24

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

Screenshot from 2025-04-27 12-06-54

#Using tr command

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

OUTPUT

Screenshot from 2025-04-27 12-07-19

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

Screenshot from 2025-04-27 12-09-14

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

OUTPUT

Screenshot from 2025-04-27 12-09-57

#Backup commands tar -cvf backup.tar *

OUTPUT

432115930-2ccfbed2-734e-4e45-bbe2-1e8e07e28f89

mkdir backupdir

mv backup.tar backupdir

tar -tvf backup.tar

OUTPUT

432116050-b090597c-0ecd-419f-a678-1ffb97768c94

tar -xvf backup.tar

OUTPUT

432116125-1b40b14d-9db7-4c77-b36d-c850d4e1fc36

gzip backup.tar

ls .gz

OUTPUT

432116203-a7eadb43-e5b0-4ac1-9762-57787e6d65c9

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

cat << stop > herecheck.txt

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

cat herecheck.txt

OUTPUT

Screenshot from 2025-04-27 12-12-22

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

308621144-aa9e221b-1914-41c6-96a4-a3480fcc931c

ls file1

OUTPUT

308621222-7aa3c8d8-08cf-4789-be3e-8eb056aec0c6

echo $?

./one bash: ./one: Permission denied

echo $?

OUTPUT

308621428-b392932f-12c9-47bf-9d86-fb44457adb34

abcd

echo $?

OUTPUT

308621520-6cac1aec-beb9-4683-91ba-2a6e062a4fd3

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

308621595-f8e710dd-372f-4b4c-9db1-efd9aa88e581

chmod 755 strcomp.sh

./strcomp.sh

OUTPUT

308621672-23f2ccf2-f9e4-4dff-98fc-0b3812c642a5

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

./psswdperm.sh

OUTPUT

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

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

308621891-f0646e68-4bbe-4c0e-bf34-7212526995d8

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

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

308622068-a2ca4461-3b62-41b0-afa0-eb9523ebc501

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

308622136-8d68fc65-0b14-4777-af74-cefbc365c662

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

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

cat untiltest.sh

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

$ chmod 755 untiltest.sh

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

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

\#!/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

\#!/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

#!/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

308622542-ef2f1f17-feed-4357-a5a0-e63e33ea14be

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

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

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

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

308622796-d60ca9a2-6750-4f0e-bd8a-bad4b996fbce

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

$ 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

308622964-cd016d47-b62c-4697-8211-2ccbb4e91301

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

308623114-33663a39-1b90-46dc-a249-29cce8e18d43

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

308623114-33663a39-1b90-46dc-a249-29cce8e18d43

$ ./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

./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

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

cat argshift.sh

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

OUTPUT

./argshift.sh 1 2 3

308623958-22c9f9e9-ae69-4152-9768-0122fe2f2a21

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

308624028-538413e2-0bfb-498f-bd90-181bb3e71a69

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:

308624193-736ba1b8-9102-4b04-bf53-666e03ad5961

RESULT:

The Commands are executed successfully.

About

Operating systems Lab exercise

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published