View Full Version: programming C in unix

C++ Learning Community > C++ for Linux > programming C in unix


Title: programming C in unix
Description: Someone please take a look


tinhnho - September 18, 2005 06:22 PM (GMT)
Hi everyone

I am trying to do this problem, but somehow i dont really know where to start, would someone help me please, thanks first.

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:
-------------------------------------------------

Thanks for reading.

gnschmidt - September 18, 2005 10:16 PM (GMT)
Hi tinhnho

I take it you definitely want to use C rather than C++? There are plenty of tutorials out there that explain this kind of thing in detail (my personal favourite is Kernighan and Ritchie's C Programming Language 2nd edn) but one approach would be:

a) test argc to make sure the correct number of command-line arguments has been used, or print a usage message to stdout.

b) open file for reading and check as appropriate. (You mention making sure it is a regular text file. How do you define this? It could be ASCII, extended ASCII, UTF8, UTF16 of varying endianness, etc. This is more tricky than tutorials tend to suggest.)

c) the loop itself is flexible. Personally I find that recursion causes least clutter and indentation, so a function
CODE

int menu();

that waits for input and calls itself after an option has been selected and processed might work. (Alternatively, take your pick from while, for, do.)

I hope this isn't too skeletal. It's easier to respond if you run into a particular problem.

One thing you could consider is changing the way in which your program receives user input. Prompting the user for filenames, presenting options and looping, etc. works very well for individual files, but less so for multiple files. It can be very helpful to use this kind of program in shell scripts (as you're using Unix), in which case any active user input becomes a real drawback.

If, for example, you define the options d (datestamp) and f (force), you could type:
CODE

myprog -df *.txt

You'll find a good example of simple option flag handling in Kernighan and Ritchie.

Good luck!

tinhnho - September 18, 2005 11:32 PM (GMT)
thanks for the reply, but the question is asking for creating shell script in Unix, and it is similar to C.

myork - September 19, 2005 02:15 AM (GMT)

A shell script. That is like C. Well a goot starting point would be the C-Shell (/bin/csh)

The first line of a shell script is the shell being used:

CODE
#!/bin/csh


Then comes your program.

Some Hints:

Syntax:
CODE
if (<test>) then
<code>
endif
To test for a file you can use:
CODE
if (-f <fileName>)



tinhnho - September 19, 2005 03:05 AM (GMT)
hi there

here what i have
CODE

#!/bin/csh
printf " Please enter the file name: \n"
if (<test>) then
<code>
endif

...


is this right ? would you please help me, i've been looking for somebook about shell script in Unix, but there isn't any at my local store, and couldn't find online neither

myork - September 19, 2005 01:06 PM (GMT)
Try:
CODE
#!/bin/csh

echo " Please enter the file name:"
set fileName=$<

if (-f ${fileName}) then
   echo "Found File ${fileName}"
endif


NB: There MUST be no lines above #!/bin/csh

tinhnho - September 21, 2005 11:12 AM (GMT)
hi there

Thanks for the idea, i have worked on it and 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

Incubator - September 22, 2005 10:04 AM (GMT)
i dont know if it can be applied in csh, but the following code makes reusable code:

CODE

start_over() {
   # shell commands here
}

do_something_else() {
  #do other stuff
}

#entering the main code
if (<test>) then #success
  <code>
else
   start_over
endif







Hosted for free by InvisionFree