Skip to main content

Learn Programming in UNIX



Q: - Write a program to input two numbers and add them?

            gedit add.sh
            clear
            echo “enter two numbers:”
            read a
            read b
            sum=`expr$a+$b`
            echo $sum


Syntax for if-else:-

(1)         if test condition
            then
                ---------
                ---------
            else
                ---------
                ---------
            fi

(2)         if[condition]
            then
               ---------
                ---------
            else
                ---------
                ---------
            Fi

Q: - Write a program to input a numbers and check it is negative or not?

            clear
            echo “enter a numbers:”
            read a
            if[$a-ge 0]
            then
            echo “no. is positive”
            else
            echo “no. is negative”
            fi







Q: - Write a program to input a numbers and check it is even or odd?
            clear
            echo “enter a numbers:”
            read a
            r= `expr $a%2`
            if test $r –eq 0
            then
            echo “no. is even”
            else
            echo “no. is odd”
            fi
Q: - Write a program to input two numbers and swap them?       
         clear
echo "enter the value of a :"
read a
echo "enter the value of b"
read b
t=$a
a=$b
b=$t
echo " value of a : $a"
echo " value of b: $b"

Q: - Write a program to input two numbers and find greatest number?

         clear
echo "enter the value of a :"
read a
echo "enter the value of b"
read b
if test $a -gt $b
then
echo "largest  value: $a"
else
echo "largest  value: $b"
fi



Q: - Write a program to input a numbers and check number is positive or not?

clear
echo "enter the value of a :"
read a
if test $a -ge 0
then
echo "no. positive"
else
echo "no. negative"
fi


Syntax for ‘for’ loop:-

         For((initial;sondition;increment/decrement))
    Do
         --------------------------------
         --------------------------------
         --------------------------------
    Done


Q: - Write a program to input a numbers and print the table of that number?

        
clear
echo "enter any no.:"
read a
for((i=1;i<=10;i++))
do
t=`expr $a \* $i`
echo " $a * $i " $t
done


Syntax for ‘while’ loop:-


    While test condition
    Do
         ------------
         ------------
         ------------
    Done

Q: - Write a program to input a numbers and print digit by digit?

   
clear
echo "enter any no.:"
read a
while test $a -gt 0
do
t=`expr $a % 10`
echo $t
a=`expr $a / 10`
done

Q: - Write a program to input a numbers and add every digit?

clear
echo "enter any no.:"
read a
while test $a -gt 0
do
t=`expr $a % 10`
s=`expr $s + $t`
a=`expr $a / 10`
done
echo "sum of every digit: $s"


Syntax for switch case:-

    Case $variable name in
         1)
             --------
             --------
             --------
             --------
             --------
             --------
             ;;
         2)
             --------
             --------
             --------
--------
             --------
             --------

             ;;
        
         *)
             --------
             --------
             --------
--------
             --------
             --------

             ;
    Esac


        




Example for switch case:-
clear
echo "1)addition"
echo "2)substraction"
echo "3)division"
echo "4)multiplication"
echo "enter two number:"
read a
read b
echo "enter your choice:"
read ch
case $ch in
1) result=`expr $a + $b`;;
2) result=`expr $a - $b`;;
3) result=`expr $a / $b`;;
4) result=`expr $a \* $b`;;
*) echo "invalid choice"
esac
echo $result
Q: - Write a program to input a numbers and check no. is perfect or not?
clear
echo "enter a no:"
read a
sum=0
mid=`expr $a/2`
for((i=1;i<=$mid;i++))
do
r=`expr $a % $i`
if test $r -eq 0
then
sum=`expr $sum + $i`
fi
done
if test $sum -eq $a
then
echo "no is perfect"
else
echo "not perfect"
fi

Q: - Write a program to input a numbers and check no. is prime or not?
clear
echo "enter a no:"
read a
f=0
for((i=1;i<=$a;i++))
do
r=`expr $a % $i`
if test $r -eq 0
then
f=`expr $f + 1`
fi
done
if test $f -eq 2
then
echo "no is prime"
else
echo "not prime"
fi
Q: - Write a program to input a numbers and check no. is Armstrong or not?
clear
echo "enter a no:"
read a
sum=0
f=$a
while test $a -ne 0
do
r=`expr $a % 10`
a=`expr $a / 10`
sum=`expr $sum + $r \* $r \* $r`
done
if test $sum -eq $f
then
echo "no is armstrong"
else
echo "not armstrong"
fi
Q: - Write a program to print n term Fibonacci series?
clear
echo "how many terms:"
read a
f=0
p=1
echo $f
echo $p
d=`expr $a - 2`
for((i=1;i<=d;i++))
do
next=`expr $f + $p`
f=$p
echo $next
p=$next
done

Q: - Write a program to input a number and find its factorial?

clear
echo "enter a no:"
read n
fact=1
for((i=$n;i>=1;i--))
do
fact=`expr $fact \* $i`
done
echo "factorial is = $fact"

Q: - Write a program to input a file name and display its contents?
clear
echo "enter file name:"
read s
cat $s




Q: - Write a program to input a file name and display its contents in reverse ordered?

clear
echo "enter file name:"
read s
rev $s



Q: - Write a program to input a file name and encrypt it in to a file name encrypt.txt?

clear
echo "enter file name:"
read fn
tr[a-z A-Z 0-9][d-z c-x 1-5]<$fn>encrypt.txt
cat $encrypt.txt




Q: - Write a program to input a string and find the length?

clear
echo "enter a string:"
read a
len=`echo $a|wc -c`
len=`expr $len - 1`
echo $len








Q: - Write a program to input two strings and concatenate them?

clear
echo "enter two strings:"
read a
read b
cat $a + $b > $c
echo $c

Command line argument

    $# = Total number of argument

    $1 = First argument

    $2 = Second argument

    $* = Total argument to print/show



Q: - Write a program to input a file name  with CLA and display the content?

Clear
Cat $1


Q: - Write a program to print all the arguments?

Clear
Echo $*

Comments

Popular posts from this blog

O Level Practical Queestion Paper (MS-OFFICE, MSDOS, MS-EXCEL, ICT Resources)

MSDOS 1. Write command to display all files having extension ‘jpg’. 2. Write command to display all files of all types starting with letter ‘m’. 3. Write command to display all files having names up to ten characters with second last letter as ‘n’, e.g. intelligent. 4. Write a command to copy the file mars of type exe from the directory ‘space’ of drive U to another directory “universe” in drive S. 5. Write a command to delete the directory as well as the files of the directory ‘world’ on drive E. 6. Write command to display all file names on the disk in drive A and having an extension of no more than two characters.   7. Write command to copy all the files beginning with ‘m’ and whose file names has a ‘txt’ extension from drive A to the ‘\document’ directory on drive C. 8. Write set of commands to create a subdirectory of the root directory called ‘exercise’, and then move into it 9. Perform the following tasks using DOS commands in the DOS sh...

IICS College 15th August Celebration Day

IICS COLLEGE  15th August Celebration Day