UNIX Shell scripting/Menu

When the script is executed, the user should be presented with a menu of 3 (three) options: 

  1. Display information and search for a station in Rail-Stations.txt file:

The program displays the user’s username and the absolute path names of login directory, the current date and time, followed by the total number of the non-directory files in your login directory.

Then it accepts an input (using read command) of a positive number N that is not greater than the number of stations in Rail-Stations.txt, and displays the last N stations in Rail-Stations.txt file.

Then it accepts an input of a string value and looks for the stations in Rail-Stations.txt file for all the stations that contain the input string. If found, it displays all found stations as output, otherwise it displays an error message like “xxxx was not found in any station”. Here xxxx is the input string. (Hint: This can be done by redirecting the search results to a file and then checking whether the file is empty or not). For example, if the input string is “ville”, the output should be Roseville, Erskineville, and Hurstville. If the input string is “ABC”, the output will be “ABC was not found in any station”.

Any invalid input data will cause an appropriate error message displayed on screen.
  1. Find the first letter in alphabet from the input and count the input letters:

Use read commands to accept the input of a series of upper case alphabetic letters, one at a time. The input ends with a 0. Find and display the first letter in alphabetic order among all the valid input letters and the count of total number of valid input letters. For example, input of D, G, T, W, Q, 3, B, 1, D, S, 0 should result in B and 8, with the error messages indicating that 3 and 1 are invalid input characters. Any invalid input character (eg. #, $, 3, a) will cause an error message and be ignored.

  1. Exit the program:

Before exit, ask user to confirm by entering a “Y” (“y”). If confirmed, the program exits, otherwise, return to the menu.

The script should get user’s choice of menu items and perform the chosen task, and repeat the procedure until user has chosen “3” and confirmed with a “Y/y” to exit. During the execution, any incorrect choice or invalid input should result in an appropriate error message. I have done upto this... echo "Enter the choice by entering the number (1-3 only); read choice case $choice in "1")

....Question:1..........

echo Displays information and search stations in file Stations.TXT echo "User's username : 'who am i'" echo "Absolute path name of login directory : '~/A4" echo "Current date and time : 'date'" echo "Total number of the non-directory files in login directory : 'ls -al | grep ^-" read N if [$N -gt 45]; then tail -N Rail-Stations.txt fi echo Please enter a string value read string grep $string Stations.TXT if [$String]; then echo "$string was found" else echo "$string was not found in any station" fi esac exit echo echo "# Are you sure? (Y/N)" read exitOrNot case $exitOrNot in Y|y) choice=3 echo echo "> Thank you for playing!" ;;

    N|n)
            choice=0
    ;;

    *)
            echo
            echo ">  Invalid choice"
            choice=0
    ;;
    esac

;;

Invalid option

*) choice=0 echo echo "# Please enter a valid number" ;;

esac done


ANSWERS:


echo "Enter the choice by entering the number (1-3 only);
read choice case $choice in "1")

Question 1

echo Displays information and search stations in file Stations.TXT
echo "User's username : 'who am i'"
echo "Absolute path name of login directory : '~/A4"
echo "Current date and time : 'date'"
echo "Total number of the non-directory files in login directory : 'ls -al  | grep ^-"
read N
if [$N -gt 45];
then
  tail -N Rail-Stations.txt
fi
echo Please enter a string value
read string
grep $string Stations.TXT
if [$String]; then
  echo "$string was found"
else
  echo "$string was not found in any station"
fi
esac
exit
"2")

Question 3

echo "#  Are you sure to exit? (Y/N)"
read exitOrNot
case $exitOrNot in 
  Y|y)
    choice=3
    echo
    echo ">  Thank you!"
  ;;
  N|n)
    choice=0
  ;;
  *)
    echo
    echo ">  Invalid choice"
    choice=0
  ;;
esac
;;

#Invalid option
*)
  choice=0
  echo
  echo "#  Please enter a valid number"
;;
esac
done
exit


 MORE:


 ? Find and replace: \'
 ? Find and replace: \'
 ? Find and replace: \'
 ? replace with sed on Mac OS X Leopard doesn't do what expected
 ? Change find and replace every iteration
 ? Using sed to replace text with spaces with a defined variable with slashs
 ? Grep - find and replace
 ? sed command - find and replace while excluding specific pattern in find
 ? Fixed width file String replacement
 ? Use Sed to find and replace json field