View Single Post
Old 2005-09-21, 11:21   #1
tinhnho
 
Jan 2005

D16 Posts
Default Please take a look my shell Script in Unix

Hi everyone

I am trying to do this problem. and here it the problem

Problem:
Create a shell script that can accepts a file name from the command line. This file name will need to be checked to make sure it is a regular (text) file.

Once the file is verified, you wiwll need to check to see if there is any data in it., and if there is, prompt the user if it is ok to delete the data in the file, or just leave the file alone. Whether there is data that can be deleted, or it is new file, you will then give the user a menu of option on what they want to do with that file.

The menu will allow the user insert a datestamp into your file(a), edit the file to insert text(B), copy your file to another fileĀ©, and exit(d). This menu is interactive, and will need to loop untill the user chooses the exit command(e). NOTE: the menu option to copy myfile.txt to another file should prompt for a new filename and verify that file does not exit. Along with the menu and selection options you should also have a way to have the user knopw that they chose an incorrect item (say they chose "a", which is not an option), you will need to warn the user and then reprompt with the menu.

The command line may look like this:
$file menu myfile.txt

.... or simply the filemenu command for your shell script without the command line argument.


The menu of options might look like ;
------------------------------------------------
1) Put datestamp into myfile.txt - appended
2) Edit myfile.txt
3) Copy myfile.txt to another file
4) Edit
Please select one of the foloowing:
-------------------------------------------------

Here is my code:
Code:
#!/bin/bash
#Set up menu

 start_over:
    printf "Enter a text file: "
    set f_name = "$<"
   
 # Check to see if the file exists, if it is a text file, and if the 
 # user wishes to delete the contents
   
  if (-e $f_name) then                  #if file exists
        echo "The File Entered Does Exist!"
     if (-f $f_name) then              #if file contains text 
            echo "The File Entered Is A Text File!"
        else
            echo "The File Entered Is Not A Text File!"
     endif
        if(-z $f_name) then              #determine the size
            echo "The File *$f_name*  Contains No Data!"
  else 
      echo "The File *$f_name* Contains Data!"
        endif
        else
  printf "Would You Like To Delete The Contents Of The File?"
     set answer = "$<"                   #set the answer var
  if ($answer =~ [Yy]) then
      echo "" > $f_name               #redirects nothing to clear the file
  else
      if ($answer =~ [Nn]) then
        	echo "Data Has Not Been Deleted!"
        else
        	echo "Invalid Input!"
        endif
 endif
 else
  echo "The File Entered Does Not Exist!"
 goto start_over;
 endif               

# main menu 
main_menu:
cat <<end_menu_data

***********************************
* * * * * * MAIN MENU * * * * * * * 

    1. Datestamp The File
    2. Edit The File 
    3. Copy The File to Another File
    4. Exit
    
* * * * * * * * * * * * * * * * * * 
***********************************
end_menu_data

printf "PLEASE ENTER YOUR CHOICE: "

set choice = "$<"           

switch ($choice)            # set up the menu var
case 1:
   date >> $f_name         # will display date, redirect it and append to 
breaksw                    # the end of file
  
case 2:
    printf "\n Please Edit The File Using Emacs! \n "
    emacs $f_name &               # emacs to edit the desired file
    goto main_menu;                  # return to thr main menu
breaksw

case 3:
     printf "Please Enter The File To Copy To: "
     set f_name2 = "$<"           # set the name for the second file 
     cp $f_name $f_name2       # copies file1 into file2
     goto menu_perm;                 # acssesing the permissions
breaksw

case 4:    
    printf "\nHave A Good Day\n"
    exit(0);  # exit the program
breaksw

default:
    printf "\n$choice Is An Invalid Input,Please Try Again\n"; #invalid input
    goto main_menu;                  # return to thr main menu  
breaksw

endsw

goto main_menu;
Somehow it still get the error message, and here is the error:
Code:
% ./myhw1.sh
./myhw1.sh: start_over:: command not found
Enter a text file: ./myhw1.sh: line 39: syntax error near unexpected token `else'
./myhw1.sh: line 39: `            else'
i tried to take of else at line 39, but still get same error but at difference line, would you please check, sorry i am so new with shell script in unix. Thanks
tinhnho is offline   Reply With Quote