0% found this document useful (0 votes)
7 views5 pages

Korn Shell Scripting Exercises

USP Lab

Uploaded by

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

Korn Shell Scripting Exercises

USP Lab

Uploaded by

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

WEEK-12 (Korn Shell Scripting)

1. Reverse Numbers:
Write a shell script to display the reverse of a given number.
2. File Handling Menu:
Write an interactive shell program that offers the user a choice of copying, removing,
renaming, or linking files. The program should prompt the user for the necessary
information, such as file names and new names.
3. Fibonacci Series:
Write a shell script which will display the Fibonacci series up to a given number of
terms.
4. Palindrome Check:
Write a shell script to display if a given string is a palindrome from the command-line
arguments.
5. Factorial Calculation:
Write a shell script to find the factorial of a given number.

1. Write a shell script to display the reverse of a given number.


#!/bin/ksh
echo "Enter a number:"
read number
reverse=0
while [ $number -gt 0 ]
do
remainder=$(( $number % 10 ))
reverse=$(( $reverse * 10 + $remainder ))
number=$(( $number / 10 ))
done
echo "Reversed Number is: $reverse"
2. Write an interactive shell program that offers the user a choice of copying, removing,
renaming, or linking files. The program should prompt the user for the necessary
information, such as file names and new names.
#!/bin/ksh
while true; do
echo "File Handling Menu"
echo "1. Copy File"
echo "2. Remove File"
echo "3. Rename File"
echo "4. Create Symbolic Link"
echo "5. Exit"
echo "Enter your choice: "
read choice

case $choice in
1)
echo "Enter the source file name:"
read src
echo "Enter the destination file name:"
read dest
cp $src $dest
echo "File copied successfully."
;;
2)
echo "Enter the file name to remove:"
read file
rm $file
echo "File removed successfully."
;;
3)
echo "Enter the current file name:"
read oldname
echo "Enter the new file name:"
read newname
mv $oldname $newname
echo "File renamed successfully."
;;
4)
echo "Enter the target file name:"
read target
echo "Enter the link name:"
read linkname
ln -s $target $linkname
echo "Symbolic link created successfully."
;;
5)
break
;;
*)
echo "Invalid choice, please try again."
;;
esac
done
3. Write a shell script which will display the Fibonacci series up to a given number of
terms.
#!/bin/ksh

echo "Enter the number of terms for the Fibonacci series:"


read terms

a=0
b=1

echo "Fibonacci Series:"


for (( i=0; i<terms; i++ ))
do
echo "$a"
fn=$((a + b))
a=$b
b=$fn
done

4. Write a shell script to display if a given string is a palindrome from the command-line
arguments.
#!/bin/ksh

if [ -z "$1" ]; then
echo "Usage: $0 <string>"
exit 1
fi
input=$1
reverse=$(echo $input | rev)

if [ "$input" = "$reverse" ]; then


echo "$input is a palindrome."
else
echo "$input is not a palindrome."
Fi
5. Write a shell script to find the factorial of a given number.
#!/bin/ksh

echo "Enter a number:"


read number
factorial=1

for (( i=1; i<=number; i++ ))


do
factorial=$((factorial * i))
done

echo "Factorial of $number is $factorial."

You might also like