Passing arguments with " symbols

I have an executable that processes under the basic premise of:

program file_in file_out "parameters a b c"

However, we often create statements like the following:

program file1 + file2 + file3 file_out "parameters a b"

or even:

program file1 + file2 + file3 file_out "parameters a b" "parameters a d"

Please note that the paramaters can have variable text, there can be more than one set of parameters, but they must be inside double-quotes.

My problem is that I need to modify the file permissions on the file_out. When I thought that the first example above was the only way to run, that was easy since I could do a:

FILE_OUT="$2"
chmod g+w $FILE_OUT

But, my FILE_OUT could be at $2 or in the above examples $6 or in lots of other places.

I was thinking of outputting each $variable to a file, grep to exclude the " lines, and then the last line would be my output. However, the following code is not maintaining the " symbols; thus I cannot grep.

NUM_VARS="$#"
count=1
while [ $count -le "$NUM_VARS" ]
   do
   echo "$count" "$#" "$*"
   echo "$1" >>$temp_file
   shift
   count=$((count+1))
done

program file1 + file2 + file3 fileout "parm1 a b" "parm2 b d"
My output $temp_file looks like:

file1
+
file2
+
file3
fileout
parm1 a b
parm2 b d

Thoughts anyone?

Switch files and parms on the command line since what you have is a non-standard implementation. According to the standard parameters must precede files that are provided on the command line implying that the output file would be the last one on the command line.

The order shown is the order the line must be in. I am trying to front-end an existing executable, and that executable is requiring the input to be laid out in that manner - I wish I could change the order of the input parameters.
But, I must follow the:

program file_in file_out "parameters a b c"

How about testing for the existence of a file and shifting the command line parameters conditionally??
The input files should all exist (barring typos or errors) but the output file doesn't as it will be created after processing through the executable. Is that a correct assumption??
I modified your while loop based on that premise...

NUM_VARS="$#"
count=1
while [ $count -le "$NUM_VARS" ]
do
   if [ ! -f $1 ]; then
      OFILE=$1
   else
      shift
   fi
   count=$((count+1))
done
echo Output File is $OFILE

If that's the only file which is created during the execution of this program, maybe you can twiddle your umask

You can grep the parameters for a space, if you find a space the previous parameter is the output file, something like:

#!/bin/sh

for i in "$@"; do
  if echo "$i" | grep ' ' > /dev/null 2>&1; then
    echo file_out = "${out_file}"
  fi
  out_file="$i"
done

Regards

Using echo | grep is really kind of excessive when you have case

case $i in *' '*) echo file_out="$out_file";; esac

Right, but the intention was to give an approach how to accomplish his goal.

Regards