Shell Script to launch C program

Hi there, im new too shell scripting and was wondering if it is possible to create a shell script to take in a variable and load a c program.

My C program is a file monitor, and is started by using the terminal and using to following code

./monitor FileToBeMonitored

is it possible to have the user to input the directory of the file to be monitored, and the the script to run the program with the users input?

my script so far allows the user to define which file they want monitored, but i don't know how to implement the monitoring program

thanks for the help

gary

You want to create several instances of the monitor program each looking at a different file found in the directory given as an argument to the script ?

ie:

#!/bin/sh

if [ -d $1 ]; then
  for i in $1/* ; do
    ./monitor $i &
  done
fi

thanks for the reply. yes this is what i am trying to do. for the script to allow the user to define the file they wish to watch, then the script takes it in, and loads the monitor with their selected file. iv implemented your code and im getting an error message. this is a section of my code so far:

echo "please enter the file you wish to monitor";
read fname;
while true; do
read -p "are you sure you want to monitor the file: $fname?" yn

case $yn in
      [Yy]*) if [ -d $fname]; then 
           for i in $fname/*; do
               ./desktop/monitor $fname &
           done
       fi;;

[Nn]* ) exit;;

the error message im gettin is [: missing `]'
its from the line:

[Yy]*) if [ -d $fname]; then 

thank you so much for your help redhead

[Yy]*) if [ -d $fname ]; then

Don't forget the inner spaces between '[ ]'

ahhh yes, so simple. It is working now (i believe) although the output that would usually be displayed when loaded in the terminal is not coming up. Iv had a look about and i believe i need to redirect the output to be visible in the shell script. would i do this with something like

n >& m	

Which merges the output from stream n with stream m.

Your script, as shown above, doesn't redirect any output. The standard output of the 'monitor' program goes to standard output (screen). If there's none, that's because the 'monitor' program doesn't provide it.
What happens if you type the commands in a terminal i.e.?

$ ./desktop/monitor $fname &

$fname is your read directory, it is not the filename to monitor, the filename to monitor will be stored in $i within your for loop.
SO it should be:

echo "please enter the file you wish to monitor";
read fname;
while true; do
read -p "are you sure you want to monitor the file: $fname?" yn

case $yn in
      [Yy]*) if [ -d $fname ]; then 
           for i in $fname/*; do
               ./desktop/monitor $i &
           done
       fi;;

[Nn]* ) exit;;

But if it's only one filename, then theres no need for the directory check ie:

echo "please enter the file you wish to monitor";
read fname;
while true; do
read -p "are you sure you want to monitor the file: $fname?" yn

case $yn in
      [Yy]*) if [ -f $fname ]; then 
               ./desktop/monitor $fname &
           fi;;

[Nn]* ) exit;;

thanks again for the replies. when i use the terminal and use the following code

./monitor fname

results are displayed in the terminal window detailing any changes to the files that are being monitored (i.e. file altered, file deleted etc.) Although my script is the same as the post above, my results are not being shown. neither in the terminal or in the shell script (where i would rather they were)

thanks again

any ideas of what i need to make the results display? iv ran some tests and the shell script gets stuck sayin "Is this the correct file you wish to monitor?" if you type "y", it just keeps asking the same thing...

gary

You are not breaking out of the while loop after a [Yy] choice is made, so you loop through again, with the "are you sure" question. If you answer Y or y again, you'll launch yet another monitor for the same file.

You should probably add a statement that the monitor was launched for the file the user asked for and then exit the script after actually launching the monitor, unless you want to ask for a different file, in which case you need to re-work your loop a bit.

The "" on both the [ Yy ] and [ Nn ] choice seems odd. Better to just have '[ Yy ])' and ')' as choices for your case statement.

yes that sounds like what i need. although how to i make so that the program displays the output, cos i can imagine if i add a statement asking "are you finished" i'll probably get stuck in that loop. or would the results be displayed after i the monitoring was finished, the way it runs now is live so as the file is altered, the terminal is updated.

on the "*" part of your post, im not too familiar with shell scripts, and didnt know you could do it another way.

any more help would be incredible

Taking the code you had and reworking it a little, and adding another case statement and prompt you could do this:

while true; do
read -p "please enter the file you wish to monitor" fname
read -p "are you sure you want to monitor the file: $fname? (Y/N)" yn

  case $yn in
    [Yy]) if [ -d $fname ]; then 
              ./desktop/monitor $fname &
              echo "monitor for $fname launched."
            fi;;

    *) echo "exiting script" 
        exit;;
  esac

  # if you got here, you've launched a monitor
  # so ask if another one should be launched

  read -p "Do you want to launch another monitor? (Y/N)" yn

  case $yn in
      [Yy]) ;;         # leave case statement and continues the while loop
           *) echo "Exiting script"
               exit ;;
  esac

done

Basically you only handle the [Yy] condition in the case statements anything else exits the script. You can handle the [Nn] condition if you really want to, but what should happen if the user picks something other than [Yy] or [Nn]? The '*' is the default action if none of the other actions enumerated are picked. Since I've only enumerated the [Yy] anything else will exit the script.

hi, sorry for late reply, iv been away.

iv input you code and its helped alot, although im still not gettin the output. ran in the terminal without script works fine, but obvs i want it as my gui.

so im thinking, is there a way to use the shell script to load my monitor prog in the terminal and for it to bring the terminal up, to see results, or even better, in the script window itself. its a constant monitorin program and therefore it always updating...

thanks