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

Linuxx

The document contains multiple shell scripts that perform various tasks including printing prime numbers, checking for palindromes, generating Fibonacci series, summing numbers, finding maximum and minimum values in a list, listing files in a directory, converting file contents between uppercase and lowercase, reversing a number, summing digits of a number, and creating and compressing files. Each script is accompanied by a brief description of its functionality. The scripts utilize basic shell commands and control structures.

Uploaded by

Vinayaka Nikhil
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 views10 pages

Linuxx

The document contains multiple shell scripts that perform various tasks including printing prime numbers, checking for palindromes, generating Fibonacci series, summing numbers, finding maximum and minimum values in a list, listing files in a directory, converting file contents between uppercase and lowercase, reversing a number, summing digits of a number, and creating and compressing files. Each script is accompanied by a brief description of its functionality. The scripts utilize basic shell commands and control structures.

Uploaded by

Vinayaka Nikhil
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
You are on page 1/ 10

1) program to print prime numbers from m to n

echo enter m and n


read m n
for a in $(seq $m $n)
do
k=0
for i in $(seq 2 $(expr $a - 1))
do
if [ $(expr $a % $i) -eq 0 ]
then
k=1
break
fi
done
if [ $k -eq 0 ]
then
echo $a
fi
done
1) program to check whether the given word is palindrome or
not
#!/bin/bash
echo "Enter a String"
read input
reverse=""
len=${#input}
for (( i=$len-1; i>=0; i-- ))
do
reverse="$reverse${input:$i:1}"
done
if [ $input == $reverse ]
then
echo "$input is palindrome"
else
echo "$input is not palindrome"
fi
1) Fibonacci
#!/bin/bash
a=0
b=1
echo "Enter the limit"
read N
echo "The Fibonacci series is : "
echo -n "$a "
echo -n "$b "
for (( i=0; i<$N;i++))
do
fn=$((a + b))
echo -n "$fn "
a=$b
b=$fn
done
1) Sum of n numbers
echo "Enter Size(N)"
read N
i=1
sum=0
echo "Enter Numbers"
while [ $i -le $N ]
do
read num
sum=$((sum + num))
i=$((i + 1))
done
echo $sum
1) program to find maximum and minimum numbers from the
given list of integers
#!/bin/bash
arrayName=(1 2 3 4 5 6 7)
max=${arrayName[0]}
min=${arrayName[0]}
for i in "${arrayName[@]}"
do
if [[ "$i" -gt "$max" ]];
then
max="$i"
fi
if [[ "$i" -lt "$min" ]];
then
min="$i"
fi
done
echo "Max is: $max"
echo "Min is: $min"
1) Program to list all the files in a directory
# !/bin/bash
echo "enter directory name"
read dir
if[ -d $dir]
then
echo "list of files in the directory"
ls –l $dir|egrep ‘^d’
else
echo "enter proper directory name"
fi
3. Write a script to convert the contents of a given file from uppercase to
lowercase
and also count the number of lines, words and characters of the resultant file.
#!/bin/sh
getFile(){
echo -n "Enter File Name:"
read txtFileName
if [ ! -f $txtFileName ]; then
echo "File Name $txtFileName does not exists."
exit 1
fi
}
clear

echo "1. Uppercase to Lowercase "


echo "2. Lowercase to Uppercase"
echo "3. Exit"
echo -n "Enter your Choice(1-3):"
read Ch

case "$Ch" in
1)

getFile

echo "Converting Upper-case to Lower-Case "


tr '[A-Z]' '[a-z]' <$txtFileName
;;

2)
getFile
echo "Converting Lower-Case to Upper-Case "
tr '[a-z]' '[A-Z]' <$txtFileName
;;
*)
echo "Exiting..."
exit
;;
Esac
4. Write a shell script to find the reverse of a given number.
echo " Enter a positive integer "
read n
sd=0
rev=0
while [ $n -gt 0 ]
do
sd=$(( $n % 10 ))
rev=$(( $rev * 10 + $sd ))
n=$(( $n / 10 ))
done
echo "Reverse number of entered digit is $rev"

5) Program to print sum of digits


echo "Enter a number"
read num
sum=0
while [ $num -gt 0 ]
do
mod=$((num % 10)) #It will split each digits
sum=$((sum + mod)) #Add each digit to sum
num=$((num / 10)) #divide num by 10.
done
echo $sum

6) program to create file and compress it


FILE_LIST="file1 file2 file3 file4 file5"
for f in $FILE_LIST;
do
tar -rvf archive.tar $f
done
gzip archive.tar

You might also like