Wednesday, July 17, 2013

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

2 comments:

  1. specified for each menu:
    If the user enters option 1, your program should display the list of entries in the current directory. For option 2, display the current date in full format. For option 3, provide your shell level. For option 4, your program should prompt the user for the directory he/she wish to compare the number of entries with that of the current directory, and display the
    Welcome to <>’s menu
    1. List all files (in the current directory) 2. Current date 3. Shell level 4. Compare entries in directories 5. Login count 6. Quit
    Choose your option…..
    Computer and Network Support Technician School of Applied Technology, Humber College
    Assignment 4 TECH172

    Page 2 of 7

    directory name which contains more entries. If the user enters a directory name which does not exist, your program should state that the directory does not exist. For option 5, show the number of time you logged in. To quit, user will choose option 6. Your program should continue to execute until the user selects option 6

    ReplyDelete
  2. can someone help me answer this question.

    Write a shell script program that will display the following menu and perform the actions specified for each menu:
    If the user enters option 1, your program should display the list of entries in the current directory. For option 2, display the current date in full format. For option 3, provide your shell level. For option 4, your program should prompt the user for the directory he/she wish to compare the number of entries with that of the current directory, and display the
    Welcome to <>’s menu
    1. List all files (in the current directory) 2. Current date 3. Shell level 4. Compare entries in directories 5. Login count 6. Quit
    Choose your option…..
    Computer and Network Support Technician School of Applied Technology, Humber College
    Assignment 4 TECH172

    Page 2 of 7

    directory name which contains more entries. If the user enters a directory name which does not exist, your program should state that the directory does not exist. For option 5, show the number of time you logged in. To quit, user will choose option 6. Your program should continue to execute until the user selects option 6.

    ReplyDelete