0% found this document useful (0 votes)
8 views45 pages

Shell Scripts for Basic Programming Tasks

The document contains a series of shell script programs that perform various tasks such as displaying odd numbers in a range, checking for palindromes, sorting numbers using bubble sort, changing file extensions, showing system configurations, performing arithmetic operations, transposing matrices, finding the largest of three numbers, checking file types, converting filenames to uppercase, and deleting lines containing a specific word from files. Each script is accompanied by example outputs demonstrating their functionality. The scripts utilize basic shell commands and constructs like loops, conditionals, and file manipulations.

Uploaded by

Bhagya Shree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views45 pages

Shell Scripts for Basic Programming Tasks

The document contains a series of shell script programs that perform various tasks such as displaying odd numbers in a range, checking for palindromes, sorting numbers using bubble sort, changing file extensions, showing system configurations, performing arithmetic operations, transposing matrices, finding the largest of three numbers, checking file types, converting filenames to uppercase, and deleting lines containing a specific word from files. Each script is accompanied by example outputs demonstrating their functionality. The scripts utilize basic shell commands and constructs like loops, conditionals, and file manipulations.

Uploaded by

Bhagya Shree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Write shell script program to read two numbers (start and


ending endlimit) and display all the odd numbers between start
and endlimit

echo "enter m value"


read m
echo "enter n value"
read n
while [ $m -le $n ]
do
if [ `expr $m % 2` -eq 0 ]
then
echo even=$m
else
echo odd=$m
fi
m=`expr $m + 1`
done

OUTPUT:
(base) suhail@suhail-Inspiron-3505:~$ vi [Link]
(base) suhail@suhail-Inspiron-3505:~$ sh [Link]
enter m value
1
enter n value
15
odd=1
even=2
odd=3
even=4
odd=5
even=6
odd=7
even=8
odd=9
even=10
odd=11
even=12
odd=13
even=14
odd=15

2. Write shell script program to verify whether string is


palindrome or not

echo Enter the string


read s
echo $s>temp
rvs="$(rev temp)"
if [ $s = $rvs ]
then
echo "it is palindrome"
else
echo "it is not a palindrome"
fi

OUTPUT
(base) suhail@suhail-Inspiron-3505:~$ vi [Link]
(base) suhail@suhail-Inspiron-3505:~$ vi [Link]
(base) suhail@suhail-Inspiron-3505:~$ sh [Link]
Enter the string
srs
it is palindrome
(base) suhail@suhail-Inspiron-3505:~$ vi [Link]
(base) suhail@suhail-Inspiron-3505:~$ sh [Link]
Enter the string
sre
it is not a palindrome
3. write a shell script program to sort given list of numbers using
bubble sort

#!/bin/bash
echo "enter maximum number"
read n
echo "enter numbers in array:"
for ((i=0;i<$n;i++))
do
read nos[$i]
done
echo "numbers in an array are:"
for ((i=0;i<$n;i++))
do
echo ${nos[$i]}
done
for ((i=0;i<$n;i++))
do
for ((j=$i;j<$n;j++))
do
if [ ${nos[$i]} -gt ${nos[$j]} ]; then
t=${nos[$i]}
nos[$i]=${nos[$j]}
nos[$j]=$t
fi
done

OUTPUT
(base) suhail@suhail-Inspiron-3505:~$ vi [Link]
(base) suhail@suhail-Inspiron-3505:~$ bash [Link]
enter maximum number
4
enter numbers in array:
2
9
0
1
numbers in an array are:
2
9
0
1
Sorted numbers
0
1
2
9

4. write shell script program to change filename extension

(base) suhail@suhail-Inspiron-3505:~$ cat > [Link]


srs first grade college
bca
cta(base) suhail@suhail-Inspiron-3505:~$ cat > [Link]
abcd
efgh
ijkl
mnop
(base) suhail@suhail-Inspiron-3505:~$ cat > [Link]
apple
mango
grapes
ls
echo "before changing extension"
echo rename all *.txt files to *.text
for f in *.txt; do
mv -- "$f" "${f%.txt}.text"
done
echo "after changing extension"
ls
OUTPUT

(base) suhail@suhail-Inspiron-3505:~$ vi [Link]


(base) suhail@suhail-Inspiron-3505:~$ sh [Link]
[Link] Programs
[Link] Public
[Link] re
[Link] 'Smart dustbin code'
anaconda3 'Smart dustbin code(1)'
arduino 'Smart dustbin [Link]'
Arduino 'Smart dustbin [Link]'
[Link] snap
[Link] 'Step - 1'
[Link] 'Telegram Desktop'
Desktop temp
Documents Templates
Downloads untitled
google-chrome-stable_current_amd64.deb Videos
Music VNC-Viewer-6.22.315-Linux-
[Link]
[Link] [Link]
Pictures
before changing extension
rename all Smart dustbin [Link] [Link] [Link]
[Link] [Link] files to *.text
after changing extension
[Link] Programs
[Link] Public
[Link] re
[Link] 'Smart dustbin code'
anaconda3 'Smart dustbin code(1)'
arduino 'Smart dustbin [Link]'
Arduino 'Smart dustbin [Link]'
[Link] snap
[Link] 'Step - 1'
[Link] 'Telegram Desktop'
Desktop temp
Documents Templates
Downloads untitled
google-chrome-stable_current_amd64.deb Videos
Music VNC-Viewer-6.22.315-Linux-
[Link]
[Link] [Link]
Pictures
(base) suhail@suhail-Inspiron-3505:~$ vi [Link]
(base) suhail@suhail-Inspiron-3505:~$

5. write shell script to show various system configuration like

echo "system configuration details"


echo "***_____________________***"
echo "currently logged user"
whoami
echo "***____________________***"
echo "to display comment shell"
echo $SHELL
echo "***___________________***"
echo "to display home directory"
echo $HOME
echo "***__________________***"
echo "to display os type"
uname
echo "***___________________***"
echo "to display current path settings"
echo $PATH
echo "***__________________***"
echo "to display current working directory"
pwd
echo "***__________________***"
echo "to display asversion and release no, kernel version"
uname -or
echo "***_________________***"
echo "to display all available shells"
cat/etc/shells
chsh -l
echo "***___________________***"
echo "to display mouse settings"
xset m
echo "***___________________***"
echo "to display cpu type, speed"
stty -a
echo "***___________________***"

OUTPUT
(base) suhail@suhail-Inspiron-3505:~$ vi [Link]
(base) suhail@suhail-Inspiron-3505:~$ sh [Link]
system configuration details
***_____________________***
currently logged user
suhail
***____________________***
to display comment shell
/bin/bash
***___________________***
to display home directory
/home/suhail
***__________________***
to display os type
Linux
***___________________***
to display current path settings
/home/suhail/anaconda3/bin:/home/suhail/anaconda3/condabin:/
usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/
games:/usr/local/games:/snap/bin
***__________________***
to display current working directory
/home/suhail
***__________________***
to display asversion and release no, kernel version
5.14.0-1046-oem GNU/Linux
***_________________***
to display all available shells
[Link]: 25: cat/etc/shells: not found
chsh: invalid option -- 'l'
Usage: chsh [options] [LOGIN]

Options:
-h, --help display this help message and exit
-R, --root CHROOT_DIR directory to chroot into
-s, --shell SHELL new login shell for the user account

***___________________***
to display mouse settings
***___________________***
to display cpu type, speed
speed 38400 baud; rows 24; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp =
^Z; rprnt = ^R;
werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl
ixon -ixoff
-iuclc -ixany -imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0
bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop
-echoprt
echoctl echoke -flusho -extproc
***___________________***
(base) suhail@suhail-Inspiron-3505:~$ vi [Link]
(base) suhail@suhail-Inspiron-3505:~$

6. write a shell script that adds, subtracts, miltiplies and dividess


the given 2 integers

clear
input="yes"
while [ $input="yes" ]
do
echo "menu\n
[Link]\n
[Link]\n
[Link]\n
[Link]\n
enter your choice:\c"
read choice
case "$choice" in
1)echo "enter first number"
read num1
echo "enter second number"
read num2
result=`expr $num1 + $num2`
echo answer:$result
;;
2)echo "enter first number"
read num1
echo "enter second number"
read num2
result=`expr $num1 - $num2`
echo answer:$result
;;
3)echo "enter first number"
read num1
echo "enter second number"
read num2
result=`expr $num1 \* $num2`
echo answer:$result
;;
4)echo "enter first number"
read num1
echo "enter second number"
read num2
result=`expr $num1 / $num2`
echo answer:$result
;;
*)echo choose 1 to 4 only
break
;;
esac
echo do you want to continue read input
done

OUTPUT
(base) suhail@suhail-Inspiron-3505:~$ vi [Link]
(base) suhail@suhail-Inspiron-3505:~$ sh [Link]

menu

[Link]

[Link]

[Link]

[Link]

enter your choice:1


enter first number
2
enter second number
3
answer:5
do you want to continue read input
menu

[Link]

[Link]

[Link]

[Link]

enter your choice:2


enter first number
2
enter second number
3
answer:-1
do you want to continue read input
menu

[Link]

[Link]

[Link]

[Link]

enter your choice:3


enter first number
4
enter second number
4
answer:16
do you want to continue read input
menu

[Link]

[Link]

[Link]

[Link]

enter your choice:4


enter first number
12
enter second number
2
answer:6
do you want to continue read input
menu

[Link]

[Link]

[Link]

[Link]
7. reverse the rows and columns:

echo “enter number of rows”


read r
echo “enter number of columns”
read c
i=0
echo “enter elements”
t=`expr $r \* $c`
until [ $i -eq $t ]
do
read a[$i]
i=`expr $i + 1`
done
i=0; k=0
echo “transpose of matrix”
until [ $i -eq $c ]
do
j=0
until [ $j -eq $r ]
do
n=`expr $j \* $c`
m=`expr $n + $i`
b[$k]=${a[$m]}
echo “${b[$m]}”
k=`expr $k + 1`
j=`expr $j + 1`
done

OUTPUT
(base) suhail@suhail-Inspiron-3505:~$ sh [Link]
enter number of rows
2
enter number of columns
2
enter elements
1
2
3
4
transpose of matrix
1
3
2
4

8. write a shell scrit program to find biggest of 3 numbers

first=$1
second=$2
third=$3
if [ $first -gt $second ] ;then
if [ $first -gt $third ] ;then
echo $first is greater number
else
echo $third is greater number
fi
else
if [ $second -gt $third ] ;then
echo $second is greater number
else
echo $third is greater number
fi
fi

OUTPUT
(base) suhail@suhail-Inspiron-3505:~$ vi [Link]
(base) suhail@suhail-Inspiron-3505:~$ sh [Link] 2 3 4
4 is greater number
(base) suhail@suhail-Inspiron-3505:~$ sh [Link] -2 -5 -0
-0 is greater number

9. write a shell script that takes a command line argument and


reports on wherher it is directory or file or something else
f=$1
if [ -f $f ]
then
echo "file"
elif [ -d $f ]
then
echo "directory"
else
echo "not file or directory"
fi

OUTPUT

(base) suhail@suhail-Inspiron-3505:~$ sh [Link] Desktop


directory
(base) suhail@suhail-Inspiron-3505:~$ sh [Link] [Link]
not file or directory
(base) suhail@suhail-Inspiron-3505:~$ sh [Link] [Link]
file
(base) suhail@suhail-Inspiron-3505:~$ vi [Link]
(base) suhail@suhail-Inspiron-3505:~$

10. converts them to uppercase

file=$@
for file in $file
do
if [ -f $file ]
then
ufile=`echo $file | tr "[a-z]" "[A-Z]"`
mv $file $ufile
else
echo file does not exist
fi
done

OUTPUT

(base) suhail@suhail-Inspiron-3505:~$ vi [Link]


(base) suhail@suhail-Inspiron-3505:~$ ls
[Link] [Link] 'Smart dustbin code(1)'
[Link] [Link] 'Smart dustbin [Link]'
[Link] Desktop 'Smart dustbin [Link]'
[Link] Documents snap
[Link] Downloads 'Step - 1'
[Link] google-chrome-stable_current_amd64.deb 'Telegram
Desktop'
[Link] Music temp
[Link] [Link] Templates
[Link] Pictures untitled
anaconda3 Programs Videos
arduino Public VNC-Viewer-6.22.315-
[Link]
Arduino re [Link]
[Link] 'Smart dustbin code'
(base) suhail@suhail-Inspiron-3505:~$ sh [Link] [Link]
[Link]
(base) suhail@suhail-Inspiron-3505:~$ ls
[Link] [Link] 'Smart dustbin code(1)'
[Link] [Link] 'Smart dustbin
[Link]'
[Link] Desktop 'Smart dustbin [Link]'
[Link] Documents snap
[Link] Downloads 'Step - 1'
[Link] google-chrome-stable_current_amd64.deb 'Telegram
Desktop'
[Link] Music temp
[Link] [Link] Templates
[Link] Pictures untitled
anaconda3 Programs Videos
arduino Public VNC-Viewer-6.22.315-
[Link]
Arduino re [Link]
[Link] 'Smart dustbin code'

11. write a shell script that deletes all lines containg a word in one
or more files

files=$@
for sfile in $files
do
if [ -w $sfile ]
then
echo "file content before deleting are as follows"
cat $sfile
echo "enter a pattern to delete line lines in $sfile"
read pat
echo "------------------------------"
sed -i "/$pat/d" $sfile
echo "file content after deleting one as follows"
cat $sfile
echo "--------------------------------"
else
echo "either file does not exist or does not have write permission"
fi
done
OUTPUT

(base) suhail@suhail-Inspiron-3505:~$ vi [Link]


(base) suhail@suhail-Inspiron-3505:~$ cat > a1
welcome
to
srs
bca
cta^Z
[7]+ Stopped cat > a1
(base) suhail@suhail-Inspiron-3505:~$ sh [Link] a1
file content before deleting are as follows
welcome
to
srs
bca
enter a pattern to delete line lines in a1
bca
------------------------------
file content after deleting one as follows
welcome
to
srs
--------------------------------

12. compute gross salary of a employee

echo "enter basic salary"


read bs
if [ $bs -le 1500 ]
then
hra=`echo "$bs * 10/100" | bc`
da=`echo "$bs * 90/100" | bc`
else
nra="500"
da=`echo "$bs * 98/100" | bc`
fi
echo "the basic salary is $bs"
echo "the hra is $hra of the basic salary"
echo "the da is $da of the basic salary"

OUTPUT

(base) suhail@suhail-Inspiron-3505:~$ vi [Link]


(base) suhail@suhail-Inspiron-3505:~$ sh [Link]
enter basic salary
1200
the basic salary is 1200
the hra is 120 of the basic salary
the da is 1080 of the basic salary
(base) suhail@suhail-Inspiron-3505:~$ sh [Link]
enter basic salary
1000
the basic salary is 1000
the hra is 100 of the basic salary
the da is 900 of the basic salary

13. interactive file handiling shell program

while true
do
echo "*****MENU*****"
echo "1: list of files\n
2: copying files\n
3: removing files\n
4: renaming files\n
5: linking files\n
press ctrl+c to exit"
echo "enter your choice\n"
read ch
case "$ch" in
1)echo "the list of filenames"
ls -l
;;
2)echo "enter the source file name"
read file
echo "enter the destination file name"
read dfile
cp $file $dfile
echo "file copied successfully"
;;
3)echo "enter the filename to remove"
read rfile
rm $rfile
echo "file removed successfullly"
;;
4)echo "enter the filename to rename"
read rfile
echo "enter the new filename"
read dfile
mv $rfile $dfile
echo "file renamed successfully"
;;
5)echo "enter the source filename"
read file
echo "enter the filename to link a file"
read afile
ln $file $afile
echo "file linked successfully"
;;
*)echo "invalid option"
echo "enter the correct choice"
esac
done
OUTPUT

(base) suhail@suhail-Inspiron-3505:~$ sh [Link]


*****MENU*****
1: list of files

2: copying files

3: removing files

4: renaming files

5: linking files

press ctrl+c to exit


enter your choice

1
the list of filenames
total 91324
-rw-rw-r-- 1 suhail suhail 145 Aug 17 19:02 [Link]
-rw-rw-r-- 1 suhail suhail 425 Aug 17 19:09 [Link]
-rw-rw-r-- 1 suhail suhail 286 Aug 17 19:13 [Link]
-rw-rw-r-- 1 suhail suhail 851 Aug 17 19:17 [Link]
-rw-rw-r-- 1 suhail suhail 165 Aug 17 18:35 [Link]
-rw-rw-r-- 1 suhail suhail 144 Aug 17 18:37 [Link]
-rw-rw-r-- 1 suhail suhail 409 Aug 17 18:40 [Link]
-rw-rw-r-- 1 suhail suhail 162 Aug 17 18:46 [Link]
-rw-rw-r-- 1 suhail suhail 865 Aug 17 18:48 [Link]
-rw-rw-r-- 1 suhail suhail 760 Aug 17 18:52 [Link]
-rw-rw-r-- 1 suhail suhail 269 Aug 17 18:56 [Link]
-rw-rw-r-- 1 suhail suhail 109 Aug 17 18:58 [Link]
-rw-rw-r-- 1 suhail suhail 15 Aug 17 19:11 a1
*****MENU*****
1: list of files

2: copying files

3: removing files

4: renaming files

5: linking files

press ctrl+c to exit


enter your choice

2
enter the source file name
[Link]
enter the destination file name
[Link]
file copied successfully

*****MENU*****
1: list of files

2: copying files

3: removing files

4: renaming files

5: linking files

press ctrl+c to exit


enter your choice
3
enter the filename to remove
a1
file removed successfullly
*****MENU*****
1: list of files

2: copying files

3: removing files

4: renaming files

5: linking files

press ctrl+c to exit


enter your choice

4
enter the filename to rename
[Link]
enter the new filename
[Link]
file renamed successfully

*****MENU*****
1: list of files

2: copying files

3: removing files

4: renaming files

5: linking files
press ctrl+c to exit
enter your choice

5
enter the source filename
[Link]
enter the filename to link a file
a1
file linked successfully
*****MENU*****
1: list of files

2: copying files

3: removing files

4: renaming files

5: linking files

press ctrl+c to exit


enter your choice

14. develop an interactive script that asks for a word and file name
and thentells how many times that word occured in thefile

ls
echo "enter the file name"
read f
if [ -r $f ]
then
echo "enter the pattern you wanna search"
read pat
res=`grep -ow "$pat" $f | wc -l`
echo "the total number of times $pat repeated in $res"
else
echo "file does not exist or readable"
fi

OUTPUT

(base) suhail@suhail-Inspiron-3505:~$ vi [Link]


(base) suhail@suhail-Inspiron-3505:~$ sh [Link]
[Link] [Link] [Link] Arduino Documents
[Link] 'Smart dustbin code' 'Step - 1' Videos
[Link] [Link] [Link] [Link] Downloads
Pictures 'Smart dustbin code(1)' 'Telegram Desktop'
[Link]
[Link] [Link] a1 [Link] google-chrome-
stable_current_amd64.deb Programs 'Smart dustbin [Link]'
temp [Link]
[Link] [Link] anaconda3 [Link] [Link]
Public 'Smart dustbin [Link]' Templates
[Link] [Link] arduino Desktop Music re
snap untitled
enter the file name
a1
enter the pattern you wanna search
abcd
the total number of times abcd repeated in 0

15. write shell script to perform following string operations


echo enter text
read text
echo start index for sub string
read fs
echo last index for sub string
read ls
echo substring
echo $text | cut -c $fs-$ls
size=${#text}
echo "the length of a text is $size"

OUTPUT

(base) suhail@suhail-Inspiron-3505:~$ vi [Link]


(base) suhail@suhail-Inspiron-3505:~$ sh [Link]
enter text
welcome to srs
start index for sub string
2
last index for sub string
8
substring
elcome
the length of a text is 14

16. count number of characters, words and blank in a given text

text=$1
echo "number of characters is"
wc -c $text
echo "number of words"
wc -w $text
echo "number of spaces"
tr -cd ' ' <$text | wc -c

OUTPUT

(base) suhail@suhail-Inspiron-3505:~$ vi [Link]


(base) suhail@suhail-Inspiron-3505:~$ cat > s1
welcome to
srs cta
bca dept
chitradurga
^Z
[2]+ Stopped cat > s1
(base) suhail@suhail-Inspiron-3505:~$ sh [Link] s1
number of characters is
40 s1
number of words
7 s1
number of spaces
3

17. generate prime number

clear
echo "enter a value"
read n
i=1
p=1
while [ $i -le $n ]
do
j=2
p=1
while [ $j -le `expr $i / 2` ]
do
if [ `expr $i % $j` -eq 0 ]
then
p=0
break
fi
j=`expr $j + 1`
done
if [ $p -eq 1 ]
then
echo $i
fi
i=`expr $i + 1`
done

OUTPUT

enter a value
15
1
2
3
5
7
11
13

18. converrt decimal to binary and vice versa

echo "choice
[Link] binary to decimal
[Link] decimal to binary"
read choice
echo "enter the value"
read val
if [ $choice -eq 1 ]; then
echo "obase=10; ibase=2; $val"|bc
elif [ $choice -eq 2 ]; then
echo "obase=2; ibase=10; $val"|bc
else
echo "wrong choice"
fi

OUTPUT
(base) suhail@suhail-Inspiron-3505:~$ vi [Link]
(base) suhail@suhail-Inspiron-3505:~$ sh [Link]
choice
[Link] binary to decimal
[Link] decimal to binary
1
enter the value
11011
27
(base) suhail@suhail-Inspiron-3505:~$ sh [Link]
choice
[Link] binary to decimal
[Link] decimal to binary
2
enter the value
545
1000100001

19. u="srs"
dp="srs"
echo "enter the user name"
read user
if [ $u = $user ] ;then
echo "enter the password"
read p
if [ $dp = $p ] ;then
echo "login successful"
stty echo
exit
fi
fi
echo "login failed the terminal is locked"
stty -echo
OUTPUT

(base) suhail@suhail-Inspiron-3505:~$ vi [Link]


(base) suhail@suhail-Inspiron-3505:~$ sh [Link]
enter the user name
srs
enter the password
srs
login successful
(base) suhail@suhail-Inspiron-3505:~$ sh [Link]
enter the user name
srs
enter the password
sre
login failed the terminal is locked
(base) suhail@suhail-Inspiron-3505:~$

20. write shell script which receives two files names as arguments.
clear
f1=$1
f2=$2
res=`cmp $f1 $f2`
if [ ${#res} -eq 0 ] ;then
echo "both files have same content"
rm $f2
else
echo "$f1 and $f2 are different"
fi

OUTPUT

(base) suhail@suhail-Inspiron-3505:~$ sh [Link] p1 p3


p1 and p3 are different
(base) suhail@suhail-Inspiron-3505:~$ sh [Link] p1 p2
both files have same content

1.

output:
2.
3.
4.
5.
6.
8.
9.
10.
11.

12.
13.

14.
15.

16.
17.

18.
19.

20.

You might also like