-   Programming (
-   -   Please take a look my shell Script in Unix (

tinhnho 2005-09-21 11:21

Please take a look my shell Script in Unix
Hi everyone

I am trying to do this problem. and here it the 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:
#Set up menu

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!"
echo "The File Entered Is Not A Text File!"
if(-z $f_name) then #determine the size
echo "The File *$f_name* Contains No Data!"
echo "The File *$f_name* Contains Data!"
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
if ($answer =~ [Nn]) then
echo "Data Has Not Been Deleted!"
echo "Invalid Input!"
echo "The File Entered Does Not Exist!"
goto start_over;

# 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

* * * * * * * * * * * * * * * * * *


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

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

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

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


goto main_menu;[/CODE]

Somehow it still get the error message, and here is the error:
% ./
./ start_over:: command not found
Enter a text file: ./ line 39: syntax error near unexpected token `else'
./ 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

Wacky 2005-09-21 11:59

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.

Ken_g6 2005-09-21 20:20

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.

All times are UTC. The time now is 04:02.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2022, Jelsoft Enterprises Ltd.