mersenneforum.org Please take a look my shell Script in Unix
 Register FAQ Search Today's Posts Mark Forums Read

 2005-09-21, 11:21 #1 tinhnho   Jan 2005 13 Posts 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 <>$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
 2005-09-21, 11:59 #2 Wacky     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.
 2005-09-21, 20:20 #3 Ken_g6     Jan 2005 Caught in a sieve 5·79 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.

 Similar Threads Thread Thread Starter Forum Replies Last Post wombatman Software 10 2018-04-09 02:10 pinhodecarlos Software 19 2014-04-17 15:04 davieddy Puzzles 29 2012-08-27 12:26 Xyzzy Linux 5 2004-01-26 10:31 Cruncher Software 1 2003-12-17 05:53

All times are UTC. The time now is 23:48.

Wed Aug 10 23:48:46 UTC 2022 up 34 days, 18:36, 2 users, load averages: 0.92, 1.00, 1.02