Showing posts with label UNIX Shell Scripts. Show all posts
Showing posts with label UNIX Shell Scripts. Show all posts

Friday, 12 December 2014

Shell Script for replacing a particular character of a file

Write a Shell Script for replacing a particular character of a file

Solution:
echo " Enter the name of the file to be edited"
read f
echo " Enter the character to be searched"
read s
echo " Enter the character to be substituted"
read w
echo " File after Modification"
cat $f | tr $s $w   

Execution:


Shell Script to check for Positive, negative and zero numbers

Write a Shell Script to check whether the entered number is positive, negative or zero.
Solution:
echo " Enter a number"
read n
if test $n -eq 0
then
echo "The entered number is zero"
elif test $n -lt 0
then echo "The number is negative"
else
echo " The number is positive"
fi   

Execution:

Shell Script to Delete a directory

Write a Shell Script to Delete a directory

Solution:
echo " Enter the name of the directory to be deleted"
read s
c=`ls $s | wc -l`
if test $c = 0
then
rmdir $s
else
rm -r $s
fi     

Output:
Enter the name of the directory to be deleted

abc

Shell Script to list the contents of a directory

Write a Shell Script to list the contents of a directory

Solution:

echo "Enter the name of the directory"
read d
ls $d

Shell Scripts to check whether the entered character is Vowel or not

Write a Shell Scripts to check whether the entered character is Vowel or not

Solution:
echo "Enter a character"
read a
case $a in
a) echo "It is a vowel";;
e) echo "It is a vowel";;
i) echo "It is a vowel";;
o) echo "It is a vowel";;
u) echo "It is a vowel";;
*) echo "Not a vowel";;
esac   

Execution:

Shell Script to find the sum of two numbers

Write a Shell Script to find the sum of two numbers

Solution:
echo "Enter the two numbers "
read a
read b
c=`expr $a + $b`
echo "Sum is $c"

Execution:

Shell Script to check whether a file is Writable

Write a Shell Script to check whether a file is Writable

Solution:

echo "Enter a file name"
read a
if [ ! -w $a ]
then
echo "File is not writable so you cannot edit the file"
else
echo "You can edit the file"
fi

Execution:


Shell Scripts to Copy the files

Write a Shell Script to Copy the files

Solution:
echo “Copy Files”
echo “Enter The Source File"
read s
echo “Enter The Destination File”
read d
cp $s $d


Execution:





Shell Script to see if a user is already exists

Write a Shell Script to see if a user is already exists

Solution:

echo "Enter The User Name"
read userid
if grep "^$userid:" /etc/passwd > /dev/null
then
echo "$userid already exists"
else
echo “The user is not exist”
exit
fi

Execution:

Shell Script for Counting upto required number

Write a Shell Script for Counting upto required number

Solution:
echo Enter any number
read a
echo Counting Starts
n=1
while [ $n -le $a ]
do
echo $n
n=`expr $n + 1`
done

Execution:


Shell Script for Celsius to Fahrenheit conversion

Write a Shell Script for Celsius to Fahrenheit conversion

Solution:
echo Enter Temperature in Celsius
read c
c=`echo $c \* 9 | bc`
c=`echo $c / 5 | bc`
c=`echo $c + 32 | bc`
echo Hence Temperature in Fahrenheit : $c

Execution:


Shell Script for String Concatenation

Write a Shell Script for String Concatenation

Solution:

echo String Concatenation
echo Enter First String
read s1
echo Enter second String
read s2
echo Concatenated String is: $s1 $s2

Execution:


Shell Script to Display Table of any number

Write a Shell Script to Display Table of any number

Solution:
echo Enter any number
read a
n=1
echo The table for your entered number is:
while [ $n -lt 11 ]
do
b=`expr $a \* $n`
echo "$n * $a = $b"
n=`expr $n + 1`
done


Execution:


Shell Script for Swapping two numbers

Write a Shell Script to Swap the two number

Solution:

echo Enter the two numbers 
read a b
if test $a -gt $b
then
a=`expr $a - $b`
b=`expr $a + $b`
a=`expr $b - $a`
else
b=`expr $b - $a`
a=`expr $a + $b`
b=`expr $a - $b`
fi
echo Swapped Value: $a and $b

Execution:


Shell Scripts to check for Prime numbers

Write a Shell Script to check whether a given number is Prime or not

Solution:
echo Enter any number
read num
b=`expr $num / 2`
if test $b -eq 1
then
echo It is a Prime number
fi
n=2
x=0
while [ $b -ge $n ]
do
c=`expr $num % $n`
if test $c -eq 0
then
echo Number is not prime
x=0
break
else
x=1
fi
n=`expr $n + 1`
done
if test $x -eq 1
then
echo It is a Prime number
fi

Solution:

Shell Script for Palindrome number

Write a shell script to check whether a given number is palindrome or not

Solution:
echo Enter the number
read num
d=`expr $num`
c=0
while [ $d -gt 0 ]
do
b=`expr $d % 10`
d=`expr $d / 10`
c=`expr $c \* 10 + $b`
done
if test $num -eq $c
then
echo The Given number is Palindrome
else
echo The number is not a Palindrome
fi

Execution:


Thursday, 11 December 2014

Shell Script to find the Largest of three numbers

Write a Shell Script to find the Largest of three numbers

Solution:

echo Enter the Three numbers
read a b c
if test $a -gt $b
then
if test $a -gt $c
then
echo $a is greatest
else
echo $c is greatest
fi
elif test $b -gt $c
then
echo $b  is greatest
else
echo $c is greatest
fi

Execution:

Shell Script to find the Larger of two numbers

Write a Shell Script to find the Larger of two numbers

Solution:

echo Enter the  two numbers
read a b
if test $a -gt $b
then
echo $a is greater than $b
else
echo $b is greater than $a
fi

Execution:

Shell Script to find the Reverse and Sum of a number

Write a Shell Script to find the Reverse and Sum of a the entered numbers

Solution:

echo Enter any number
read n
rev=0
sd=0
sum=0
while [ $n -gt 0 ]
do
sd=`expr $n % 10`
rev=`expr $rev \* 10 + $sd`
sum=`expr $sum + $sd`
n=`expr $n / 10`
done
echo "Reverse number is $rev"
echo " sum of digits are $sum"  

Execution:

Shell Script for Fibonacci series

Write a Shell Script to find the Fibonacci series of any number

Solution:
echo Enter any number
read a
x=0
y=1
z=0
echo Fibonacci Series upto $a will be:
echo $x
echo $y
while [ $a -ge $z ]
do
z=`expr $x + $y`
if test $z -ge $a
then
exit
fi
echo $z
x=`expr $y`
y=`expr $z`
done

Execution: