![]() |
![]() |
#1 |
Jan 2005
1310 Posts |
![]()
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; 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' |
![]() |
![]() |
![]() |
#2 |
Jun 2003
The Texas Hill Country
32×112 Posts |
![]()
Your block structure is messed up. In the first 40 or so lines, there are 5 "if", 5 "endif" and 6 "else".
It looks as if you have added some lines to an existing structure without following a consistent indentation format. I would suggest that you clean up the indentation so that the location of the error becomes more visible. |
![]() |
![]() |
![]() |
#3 |
Jan 2005
Caught in a sieve
39510 Posts |
![]()
Another problem is that your code is for C shell (csh), not the Bourne-Again Shell (bash). You can tell this by the "endif"s - bash uses "fi".
I tried it, and with one else removed, what there is of your code works in C shell. Don't forget to add the menu_perm section. |
![]() |
![]() |
![]() |
Thread Tools | |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Connecting to the bash shell in Windows 10 | wombatman | Software | 10 | 2018-04-09 02:10 |
General Unix job-scheduling discussion | pinhodecarlos | Software | 19 | 2014-04-17 15:04 |
Gravity Force of a Spherical Shell | davieddy | Puzzles | 29 | 2012-08-27 12:26 |
Shell script puzzle #1 | Xyzzy | Linux | 5 | 2004-01-26 10:31 |
Unix client? | Cruncher | Software | 1 | 2003-12-17 05:53 |