Linux-Shell Programming

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

     #/bin/bash
     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?

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

     #/bin/bash
     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?

     #/bin/bash
     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?

     #/bin/bash
     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?

     #/bin/bash
     echo "hi"
     a=$?
     if [ $a -eq 0 ]
     then
     echo "Execution successfully completed"
     else
     echo "Execution failed"
     fi
    
  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”?

     #/bin/bash
     echo "Enter a number: "
     read a
     if [ $a -gt 0 ]
     then
     echo "Number is positive"
     else
     echo "Number is negative"
     fi
    
  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
    Linux”?

     #/bin/bash
     echo "1. Windows"
     echo "2. Linux"
     echo "    Select [1 or 2]"
     read ch
     if [ $ch -eq 1 ]
     then
     echo "Windows"
     else if [ $ch -eq 2 ]
     then
     echo "Linux"
     else
     echo "You dont like Windows or Linux"
     fi
     fi
    
  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?

     #/bin/bash
     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?

    #/bin/bash
    cut -c 5-8 textile
    
  11. Write a simple program using awk?

    #/bin/bash
    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”?

    #/bin/bash
    echo "Enter a pattern to search"
    read pat
    grep $pat textile
    if [ $? -eq 0 ]
    then
    echo "Word Found"
    else
    echo "Not found"
    fi