Wednesday, July 17, 2013

Unix commands

UniX Exercise 1



  1. Write a command to print the ouput “Today is Tuesday, 5th day of month, year”.
Sol.            date +”Today is %a, %d of %b, %Y”

  1. Assume that files in a directory are “filename_dd_mm_yy”, write a command to list the files created today.
Sol.            ls *_`date +”%d_%m_%y”`

  1. Write a command to copy a file x to y using cat.
Sol.            cat x > y

  1. Write a command to copy the contents of 2 files into a third file.
Sol.            cat x y > z

  1. Write a command to copy the contents of x, user input, y into a file.
Sol.            cat x - y > z

  1. Write a command to copy the contents of x and y prefixing the file name before starting file contents.
Sol.            ls x > z; cat x >> z; ls y >> z; cat y >> z

                                          (Or)

                  (ls x; cat x; ls y; cat  y) > z

  1. Write a command to generate the following into a file
Files of the directory on

           

Total files in the directory are
Sol.            (echo –e “Files of the directory `pwd` on `date +%D`\n\n”;
 ls -l;echo –e “\n\n Total files in the directory are `ls –l | wc –l`”) > z

  1. Assume package name of each java program is in line 1, write a command to display all package names.
Sol.            head -1 *.java

  1. Write a command to display top 3 files which were created recently.
Sol.            ls -ltr | head -4 | tail -3

                                          (Or)

ls -ltr | tail +2 | head -3

  1. Write a command to display lines 23 to 44 of file y.
Sol.            head -44 y | tail +23
  1. Write a command to count number of words in last but one line of file y.
Sol.            tail -2 y | head -1 | wc -w

  1. Write a command to display 15th line of every file in the current dir.
Sol.            cannot be solved with commands need to write shell script.

  1. Write a command to display the total number of bytes used by all files of the current directory.
Sol.            wc –c * | tail -1

                              (or)
                 
                  cat * | wc -c
     
  1. Write a command to copy file x to y only if y not exits.
Sol             ls y || cp x y

  1. Write a command to convert all characters of file x to capitals.
Sol             cat x | tr “a-z” “A-Z”

                              (or)
                 
                  tr “a-z” “A-Z” < x

  1. Write a command to count number of spaces in line 15 of file x.
Sol             head -15 x | tail -1 | tr –cd “ “ | wc -c

  1. Write a command to convert all tabs to spaces of a file.
Sol             cat x | tr “\t” “ “

  1. Write a command to join all lines of a file.
Sol             cat x | tr “\n” “ “

  1. Write a command to join lines 15 & 24 of a file.
Sol             (head -15 x | tail -1; head -24 x | tail -1) | tr “\n” “ “

(or)
                  echo ‘head -15 x | tail -1` `head -24 x | tail -1`


  1. Write a command to display machine hardware type.
Sol             uname –a | cut –d” “ –f6

  1. Write a command to display file permissions and filename.
Sol             ls -l | tr -s " " | cut -d" " -f1,9

  1. Write a command to display the file mask applied for file owner.
Sol             umask | cut –c 1

  1. Write a command to display empno, empname and sal from emp.db.
Sol             cut -d"|" -f1-2,6 emp.db

  1. Write a command to display the total time taken for the above query.
Sol             time cut -d"|" -f1-2,6 emp.db | tail -3 | head -1

  1. Write a command to calculate total amount used by company as salaries.
Sol             echo `cut -d"|" -f6 emp.db | tail +3 | tr "\n" "+"` 0 | bc

  1. Write a command to calculate total amount used by company as salaries along with the message “Total Salary of all employees is : ”.
Sol             echo -e "Total salary of all employess is  : \c";
echo `cut -d"|" -f6 emp.db | tail +3 | tr "\n" "+"` 0 | bc

  1. Write a command to count number of employees in department 10.
Sol             cut -d"|" -f8 emp.db | grep -wc 10

  1. Write a command to list all employees in the department in which SMITH is working.
Sol             cut -d"|" -f2,8 emp.db  | \grep -w `cut -d"|" -f2,8 emp.db  |
grep -w SMITH | cut -d"|" -f2

Hibernate tutorial

Hibernate Tutorial
Bash Shell Script

write a c shell script that takes an integer number from the keyboard and displays the fibonacci numbners equal to the number entered from the keyboard. thus if the user enters 7, your script displays the first seven fibonacci numbers

#!/bin/bash
#####################################################
# File Name: sy_4_g
#
# Usage: ./sy_4_g
#
# Author: Suresh Yadagiri
# Date:  August 06,2010
#
# Requirements:
#     write a c shell script that takes an integer number
#     from the keyboard and displays the fibonacci numbners
#     equal to the number entered from the keyboard.
#     thus if the user enters 7, your script displays the
#     first seven fibonacci numbers
#
#
#####################################################

function printusageinfo(){
echo -e "\nUsage: $0 \n"
}

function is_integer() {
    printf "%d" $1 > /dev/null 2>&1
    return $?
}

if [ $# -eq 0 ]
then
        echo "At least one argument expected"
        printusageinfo
        exit 1
fi

if [ $# -gt 1 ]
then
        echo "Too many arguments"
        printusageinfo
        exit 1
fi

if ! is_integer $1;
then
        echo "Integer argument expected"
        printusageinfo
        exit 1
fi

if [ $1 -lt 2 ]
then
        echo "invalid arguments.enter greater value"
        printusageinfo
        exit 1
fi

echo "The first $1 fibonacci numbers are"
counter=2
n1=0
n2=1
echo $n1
echo $n2
until [ $counter -eq $1 ]
do
  current=`expr $n1 + $n2`
  echo $current
  counter=`expr $counter + 1`
  n1=$n2
  n2=$current
done
exit 0

This script takes integer numbers as command line arguments and displays their sum on the screen if run with no arguments, it informs user with usage message

#!/bin/bash
#######################################################
# File Name     :    sy_3_g
# Author:    :    Suresh Yadagiri
# Date        :    July 27,2010
# Descriptiom    :   
#  This script takes integer numbers as command line
#  arguments and displays their sum on the screen    
#  if run with no arguments, it informs user
#  with usage message
#
######################################################

printusageinfo(){
    echo "Usage: $0 number-list"
    exit 1
}
function is_integer() {
    printf "%d" $1 > /dev/null 2>&1
    return $?
}

if [ $# = 0 ]
then
    echo "Usage: $0 number-list"
    exit 1
fi

sum=0 #Running sum initialized to zero
count=0 #To count number of arguments

while [ $# != 0 ]
do
if is_integer $1; then
     # "$1 is an integer"
    sum=`expr $sum + $1`
    count=`expr $count + 1`
    shift # Shift the counted number out
else
        echo "$1 is not an integer"
    printusageinfo   
fi

done

#displaying final sum

echo "The sum of given $count numbers is $sum."
exit 0

write a Bourne shell script that takes a file name and a directory name as command line arguments and removes the file if it is found under the given directory and is a simple file. If the file(the first argument) is a directory, it is removed (including all the files and subdirectories under it).

#!/bin/bash
#########################################################
# File Name     :       sy_2_g
# Author:       :       Suresh Yadagiri
# Date          :       August 10,2010
# Descriptiom   :
#  write a Bourne shell script that takes a file name and
#  a directory name as command line arguments and removes
#  the file if it is found under the given directory and
#  is a simple file. If the file(the first argument) is a
#  directory, it is removed (including all the files and
#  subdirectories under it).
#
#########################################################
function printusage(){
echo -e "\nusage:\n$0 filename directoryname"
echo "    or   "
echo -e "$0 directoryname directoryname"
exit 1
}
if (( $# < 2 || $# > 2 ))
then
    echo "Invalid arguments"
    printusage
fi

if [ ! -d $2 ] ; then
     echo " $2 is not a directory or exists"
    printusage
fi

if [ ! -d "$2/$1" ] && [ ! -f "$2/$1" ] 
then
    echo "\"$1\" is not a directory or a file inside \"$2\"."
    printusage
fi

if [ -d "$2/$1" ] ; then
    echo "\"$1\" directory removed from $2"
    rm -r "$2/$1"
else
    echo "\"$1\" file removed from $2"
    rm -f "$2/$1"
fi

This script takes integer numbers as the command line arguments and displays a list of their squares amd the sum of numbers in the list of squares

#!/bin/bash
#######################################################
# File Name     :    sy_1_g
# Author:    :    Suresh Yadagiri
# Date        :    July 27,2010
# Descriptiom    :   
# This script takes integer numbers as
# the command line arguments and displays
# a list of their squares amd the sum
# of numbers in the list of squares    
#####################################################

printusageinfo(){
        echo "Usage: $0 number-list"
        exit 1
}
function is_integer() {
    printf "%d" $1 > /dev/null 2>&1
    return $?
}

if [ $# = 0 ]
then
        echo "Usage: $0 number-list"
        exit 1
fi

index=0         #Running sum initialized to zero
sumofsquares=0         #sum of squares
count=0         #To count number of arguments

while [ $# != 0 ]
do
if is_integer $1; then
         # "$1 is an integer"
         numbers[$index]=`expr $1`
     let square=`expr $1*$1`
     squares[$index]=$square    
     sumofsquares=`expr $sumofsquares +  $square`
         count=`expr $count + 1`
         shift # Shift the counted number out
     index=`expr $index + 1`
else
        echo "$1 is not an integer"
        printusageinfo
fi

done

#displaying numbers and their squares
echo "Number        Square"
index=0
while [ $index -lt $count  ]
do
    echo " ${numbers[$index]}        ${squares[$index]}"
    index=`expr $index + 1`

done

#displaying sum of squares
echo "The sum of squres is $sumofsquares ."
exit 0

This script takes list if login names as its arguments and displays the number of terminals that each user us logged on to in a LAN Environment

#!/bin/bash
#
#
# File Name    :    page1_14
# Author    :    Suresh Yadagiri
# Date        :        July 27,2010
# Description    :
# This script takes list if login names as its
# arguments and displays the number of terminals
# that each user us logged on to in a LAN
# Environment   
#

function printusageinfo(){
echo -e "\nUsage: $0 login_names_list\n"
}


if [ $# -eq 0 ]
then
    echo "At least one argument expected"
    printusageinfo
    exit
fi


while [ $# != 0 ]
do
noofterminals=`who -a  | cut -d' ' -f1 | grep -w "$1" | wc -l`
echo "user : $1     No.of terminals logged on: $noofterminals"
shift
done

write a bourne shell script cv that takes the side of a cube as a command line argument and displays the volume of the cube

#!/bin/bash
#############################################################
# Name: sy_9
#
# Usage: ./sy_9
#
# Author: Suresh Yadagiri
# Date:  August 06,2010
# Requirements:
#  write a bourne shell script cv that takes the side of a cube
#  as a command line argument and displays the volume of the
#  cube
#
##############################################################

function printusageinfo(){
echo -e "\nUsage: $0 \n"
}
function is_integer() {
    printf "%d" $1 > /dev/null 2>&1
    return $?
}

if [ $# -eq 0 ]
then
        echo "At least one argument expected"
        printusageinfo
        exit
fi

if [ $# -gt 1 ]
then
        echo "only one argument expected"
        printusageinfo
        exit
fi

if ! is_integer $1;
then
        echo -e "\nInteger argument expected"
        printusageinfo
        exit 1
fi

if [ $1 -lt 0 ]
then
        echo "Side of cube must be positive"
        printusageinfo
        exit
fi
volume=`expr $1 \* $1 \* $1`
echo "Volume of cube of side $1 is : $volume"

write a Bourne shell script doman2ip that takes a list of domain names as command line arguments and displays their IP addresses. use the nslookup command. the following is a sample run of this program

#!/bin/bash
#####################################################
# Name: sy_8
#
# Usage: ./sy_8
#
# Author: Suresh Yadagiri
# Date:  August 06,2010
# Requirements:
#
#   write a Bourne shell script doman2ip that takes a list of
#   domain names as command line arguments and displays their
#   IP addresses. use the nslookup command. the following is
#   a sample run of this program
#  
#   $domanin2ip  up.edu  redhat.com
#    Name  : up.edu
#    Address : 192.220.208.9
#
#    Name  : redhat.com
#    Address : 207.175.42.154
#####################################################


function printusageinfo(){
echo -e "\nUsage: $0 \n"
}


if [ $# -eq 0 ]
then
        echo "At least one argument expected"
        printusageinfo
        exit
fi


while [ $# != 0 ]
do
nslookup $1 | tail +5
shift
done

write a Bourne shell script that takes a directory as an argument and removes all the ordinary files under it that have .o,o.ps and .jpg extensions. if no arguement is specified the current directory is used

#!/bin/bash
################################################################
# Name: sy_6
#
# Usage: ./sy_6
#
# Author: Suresh Yadagiri
# Date:  August 06,2010
# Requirements:
#
#   write a Bourne shell script that takes a directory as an
#   argument and removes all the ordinary files under it that
#   have .o,o.ps and .jpg extensions. if no arguement is
#   specified the current directory is used
#
################################################################

function printusageinfo(){
 echo -e "\nUsage: $0 directory_name"
}

if [ $# -gt 1 ]
then
        echo "only one argument expected"
        exit 1
fi

if [ $# -eq 1 -a ! -d "$1" ]
then
        echo "$1 is not a directory or not exists"
        exit 1
fi

if [ $# = 0 ]
 then
    directory=`pwd`
 else
    directory=$1
fi

#Get file count in the given directory ; if directory
#is emptry then display appropriate message and
#quit

#echo $directory

option=`echo $directory/*.o`
rm $option 2>/dev/null

option=`echo $directory/*.ps`
rm $option  2>/dev/null

option=`echo $directory/*.jpg`
rm $option  2>/dev/null

echo " .o,.ps.jpg files removed from $directory"

write a script that takes two integer command line arguments.the script displays the numbers between the two integer (including the number) in asscending order if the first number is smaller than the second and in descending order if the first number is greater than the second. Name the script count_up_down

#!/bin/bash
############################################################
# Name: sy_5
#
#
# Usage: ./sy_5
#
# Author: Suresh Yadagiri
# Date:  August 06,2010
#
# Requirements:
# write a script that takes two integer command line arguments.the script
# displays the numbers between the two integer (including
# the number) in asscending order if the first number is
# smaller than the second and in descending order if the
# first number is greater than the second. Name the script
# count_up_down
#
############################################################

function printusageinfo(){
echo -e "\nUsage: $0 \n"
}
function is_integer() {
    printf "%d" $1 > /dev/null 2>&1
    return $?
}
if [ $# -eq 0 ]
then
        echo "Two arguments expected"
        printusageinfo
        exit 1
fi
if [ $# -gt 3 ]
then
        echo "Too many arguments "
        printusageinfo
        exit 1
fi

if ! is_integer $1 -o  ! is_integer $2;
then
        echo -e "\nInteger arguments expected"
        printusageinfo
        exit 1
fi

number1=$1
number2=$2

#echo " $number1  $number2"

if [ $number1 -lt $number2 ]
then
   until [ $number1 -eq $number2 ]
   do
      echo " $number1 "
      number1=`expr $number1 + 1`
   done
  echo " $number1 "
exit 0
fi

if [ $number1 -gt $number2 ]
then
   until [ $number1 -eq $number2 ]
   do
      echo "$number1"
      number1=`expr $number1 - 1 `
    done
    echo "$number1"
exit 0
fi

echo " Numbers are equal "
exit 0

write This script prompts user id and displays user login name, user name and home directory of the user

#!/bin/bash
#####################################################################
# file name:    sy_4
# Author :    Suresh Yadagiri
# date    :    August 10 2010
# Description:
#    This script prompts user id and
#     displays user login name, user name and home
#     directory of the user. if user id is not
#     found in /etc/passwd file then it displays
#     appropriate error message
#######################################################################

if [ $# -gt 0 ]
then
echo "No arguments expected"
echo "Usage: $0"
exit 1
fi

echo -e "Enter user id :\c"
read userid
userloginname=`grep ":$userid:" /etc/passwd | cut -f1 -d :`

#Display error message is userid is not found
if [ "$userloginname" == "" ]
then
echo "user id is not found. Account may be not  local account"
exit 1
fi
#display user information
#userloginname=`grep ":$userid:" /etc/passwd | cut -f1 -d :`
username=`grep ":$userid:" /etc/passwd | cut -f5 -d :`
homedirectory=`grep ":$userid:" /etc/passwd | cut -f6 -d :`

echo "user login name        :     $userloginname"
echo "user  name        :          $username"
echo "user Home directory     :       $homedirectory"

write Shell script to show menu options and perform actions

#!/bin/bash
################################################################################
# Name: sy_3
#
# Menu options.
#
# Usage: sy_3
#
# Author: Suresh Yadagiri
# Date: 2010/07/20
# Write a Bourne Shell Script that displays the following menu and
# primpts you for one-character input to invoke a menu options as fillows
#   
#   a. list all files in the present working directory
#   b. display today's date and time
#   c. Invoke shell script for problem 14
#   d. display whethe a file is simple file or a directory
#   e. create a backup for a file
#   f. start a telnet session
#   g. start an ftp session
#
#   c. requires that you ask for a list of login names
#   Problem 14:   write a Bourne shell script that takes a list of
#   login names as its arguments and display the number of terminals
#   that each user is logged on to in a LAN environment
#     
#   d.insert a prompt for file names before invoking a shell command/program
#      
#   e.insert a prompt for file names before invoking a shell command/program
#       
#   f.insert a prompt for a domain name or IP address before initiating telnet
#   or frp session
#   Quit only one option x entered
#
#     Usage: ./sy_3
##################################################################################

function verifyfile(){
filename=$1
if [ -d $filename ]
then
echo "$filename  is a directory"
return
fi

if [ ! -f $filename ]
then
echo "\"$filename\" File or Directory not exists"
else
    echo "$filename is ordinary file"
fi
   
}

#
#
#

function createbackup(){

    filename=$1
    if [ -d $filename ]
    then
        echo "$filename  is a directory.enter file name"
        return
    fi

    if [ ! -f $filename ]
    then
        echo "File is not exists"
    fi

    backupfilename=${filename}_backup
    #echo "backup file name is $backupfilename"
    cp -f "$filename" $backupfilename >/dev/null 2>>/dev/null
    if [ $? -eq 0 ]
    then
    echo "Backup created and file name is $backupfilename"
    else
    echo "Backup creation failed"
    fi   

}

function handleftp(){
ftp $1 < /dev/tty
}


function handletelnet(){
telnet $1
}

while true
do
clear
echo "Use One of the following options:"

echo "    a|A:    List all files in the present working directory"
echo "    b|B:    Display today's date and time"
echo "    c|C:    Invoke shell script [display no.of terminal logged on]"
echo "    d|D:    Display whether a file is a simple file or direcotry"
echo "    e|E:    create a backup for a file"
echo "    f|F:    start the telnet session"
echo "    g|G:    start an ftp session"
echo "    x|X:    Exit"

echo -e "Enter your option and hit : \c"

read option

case "$option" in
    a|A)
         ls;   
        ;;
    b|B)   
        echo "Today date and time is `date`"   
        ;;
    c|C)
        echo "Enter login names separated by space: "   
        read loginnames
            ./sy_page1_14 $loginnames
        ;;
    d|D)   
          echo -e "Enter a file name to check its type: \c"   
        read filename
        verifyfile $filename
        ;;
    e|E)
          echo -e "Enter a file name for backup: \c"   
        read filename
        createbackup $filename
        ;;
    f|F)
        #echo "you entered f"
        echo -e "Enter IP address/domain name for telnet session: \c"
        read ipaddress
        handletelnet $ipaddress
        ;;
    g|G)
        #echo "you entered g"
        echo -e "Enter IP address/domain name for ftp session: \c"
        read ipaddress
        handleftp $ipaddress
        ;;
    x|X)
        echo "Thank you ....Bye"
        break;
        exit 0;
        ;;
    *)
        echo "Invalid Option; try again"
        ;;
esac
echo -e "Press [enter] key to continue. . .\c";
read presskey
done
exit 0

write a shell script that takes a directory as a required argument and displays the name of all zero length files in it. Do appropriaet error checking

#!/bin/bash
##########################################################################
# File Name:     sy_2
# Author:    Suresh Yadagiri
# Written:    July 25,2010
# Purpose:    To display zero size files in directory
# Problem:
#     write a shell script that takes a directory as a required argument
#    and displays the name of all zero length files in it.
#    Do appropriate error checking
#
#      Usage: ./sy_2
#
#
##########################################################################

if [ $# = 0 ]
 then
    echo "One Argument expected"   
    echo "Usage: $0 directory_name"
    exit 1
elif [ $# != 1 ]
then
    echo "only one argument expected"
    echo "Usage : $0 directory name"
       exit 1
elif [ ! -d "$1" ]
then
    echo "$1 is not a directory or not exists"
    echo "Usage: $0 directory name"  
    exit 1
fi
directory=$1
#Get file count in the given directory ; if directory
#is emptry then display appropriate message and
#quit
file_count=`ls $directory | wc -w`  #it get no.of files in directory
if [ $file_count -eq 0 ]
then
    echo "$1 is Empty directory"
    exit 0
fi
#for each file in the direcotry specified, find the
# file size. and display if its empty file
echo "zero length files in $directory are "
ls "$directory" | more |
while read file
do
filepath="$directory"/"$file"
#echo $file
if [ -f "$filepath" ]
then
    set -- `ls -l "$filepath"`
#    echo "file size is : $5 "
    file_size=$5
    if [ $file_size -eq 0 ]
    then
        #echo "$9"
        echo "$file"
    fi
fi
done




Write a shell script that takes an ardinary file as argument and removes the file if its size is zero.otherwise, the script displays file's name, size, number of hard links,owner,and modify date (in this order) on one line.

#!/bin/bash
#############################################################################
# File Name:   sy_1                                                         #
# Author:       Suresh Yadagiri                                             #
# Written:      July 25,2010                                                #
# Purpose:                                                                  #
#      Write a shell script that takes an ardinary file as argument         #
#      and removes the file if its size is zero.otherwise, the script       #
#      displays file's name, size, number of hard links,owner,and modify    #
#      date (in this order) on one line.                                    #
#      you script must do appropriate error checking                        #
#                                                                           #
#     Usage: ./sy_1                                   #
#                                                                           #
#                                                                           #
#############################################################################

if [ $# = 0 ]
 then
        echo "One Argument expected"
        echo "Usage: $0 ordinary_file"
        exit 1
elif [ $# != 1 ]
then
        echo "only one argument expected"
        echo "Usage : $0 ordinary_file"
       exit 1
fi

filename=$1

if [ -d $filename ]
then
echo "File is directory"
echo "Usage: $0 ordinary_file"
exit 1
fi

if [ ! -f $filename ]
then
echo "File is not exists "
echo "Usage: $0 ordinary_file"
exit 1
fi

set -- `ls -al $filename`


if [ $5 -eq 0 ]
then
echo "Its empty file. file deleted"
rm -f $filename
exit 0
fi
echo "File is not empty. File detail are: "

#file name
#size
#no.of hard links
#owner
#modify date
echo -e "File Name:                     $9"
echo -e "File size:              $5 bytes"
echo -e "Hard Links:             $2"
echo -e "file Owner:             $3"
echo -e "File modification time :     $6 $7 $8"