Linux-Shell Programming

  1. Write a program to print details of currently logged in users,
    print date and calendar?

     echo "Current login users: `who`"
     echo "Date: `date`"
     echo "Calendar: `cal`"
  2. Write a program to define user defined variables ‘x’ with value
    ‘10’ and ‘y’ ‘20’ and print it on screen?

     echo "a = $a , b = $b"
  3. Write a program to print current shell, present working directory,
    and username, using system variable?

     echo "Shell: $SHELL"
     echo "Current Directory: `pwd`"
     echo "User name: `whoami`"
  4. Write a program to print the sum of two numbers a=10, b=20?

     echo "Sum: `expr $a + $b`"
  5. Write a program to read two numbers from the keyboard and to print
    the sum, product and quotient of the numbers?

     echo "Enter the first number: "
     read a
     echo "Enter the second number: "
     read b
     sum=`expr $a + $b`
     echo "Sum is: $sum"
     pro=`expr $a \* $b`
     echo "Product is: $pro"
     quo=`expr $a / $b`
     echo "Quotient is: $quo"
  6. Write a program to obtain and print the status of any one command?

     echo "hi"
     if [ $a -eq 0 ]
     echo "Execution successfully completed"
     echo "Execution failed"
  7. Write a program to check the following condition:
    Read a number from the keyboard; if that number is greater than ‘0’
    then print as “Positive”, else print as “Negative”?

     echo "Enter a number: "
     read a
     if [ $a -gt 0 ]
     echo "Number is positive"
     echo "Number is negative"
  8. Write a program to implement choices:
                             1. Windows
                             2. Linux
                             Select your choice [1 or 2]
    Read the choice from keyboard. If that is ‘1’ the print “Windows”, if
    that is ‘2’ then print “Linux” else print “You don’t like Windows or

     echo "1. Windows"
     echo "2. Linux"
     echo "    Select [1 or 2]"
     read ch
     if [ $ch -eq 1 ]
     echo "Windows"
     else if [ $ch -eq 2 ]
     echo "Linux"
     echo "You dont like Windows or Linux"
  9. Write a program to prepare students record consisting of name,
    roll no, and marks of three subjects. Take sum of the three marks and
    print all details of student with total mark?

     echo "Enter Student Details"
     echo "Name: "
     read name
     echo "Roll No: "
     read roll
     echo "Mark1: "
     read m1
     echo "Mark2: "
     read m2
     echo "Mark3: "
     read m3
     tot=`expr $m1 + $m2 + $m3`
     echo "Student Record"
     echo "Name: $name"
     echo "Roll No: $roll"
     echo "Total marks: $tot"
  10. Write a program to print cut 5 to 8 columns from any textile?

    cut -c 5-8 textile
  11. Write a simple program using awk?

    awk '{ print $1 }' textile
  12. Write a program to search a specific word from a file, first read
    word from keyboard then using grep command search that word, and take
    status of that command, if status is success the print “Word Found”
    else print “Word not Found”?

    echo "Enter a pattern to search"
    read pat
    grep $pat textile
    if [ $? -eq 0 ]
    echo "Word Found"
    echo "Not found"