Invoking CGI executable after setenv (in bash)

Hello,
I have an executable cgi program that I can run manually from my Linux shell after setting environmental variables on the previous line, like this:

setenv QUERY_STRING "workdir=/u/here/there/&nb1=5&nb2=1000"
MyExecutable.cgi

How can I imitate this behavior in a bash script?

I tried invoking them as two separate system commands by using backquotes `, but the second command will not remember the previous one

Also, what if the value of nb1 is in fact a $variable and not 5, would that cause problems?

(I'm 2-days old in bash!)
Thanks

export QUERY_STRING="workdir=/u/here/there/&nb1=5&nb2=1000"
MyExecutable.cgi

It shouldn't, though it could depend on the contents of the variable.

This solution did not work well for me because I run this cgi many times in a for loop, each time changing the directory in my QUERY_STRING. It seems that if I do "export", then only the first occurrence of the cgi runs as it is supposed to, the rest will just be repetitions of the first one!! Any quick fix?

Thanks

My crystal ball is being repaired, so please post your code.

Sorry, here it is:

#!/bin/bash

##First I define some variables:
Executable=/u/myname/someplace/SomeProgram.cgi
WorkDir=/u/myname/here/there
subdirName=XYZ
NB1=5
NB2=1000

for ID in `ls $WorkDir`; do
        #Define some variables specific to this iteration
        DIR=$WorkDir/$ID/$subdirName/
        OutFile=${DIR}somefile.txt

        export QUERY_STRING "workdir=$DIR&nb1=$NB1&nb2=$NB2"
        
        #call the executable, I extract some lines from its output, and I fix them with perl regexprep:
        $Executable | tail -$(( $NBCLUSTERS*2+6 )) | head -$(( $NBCLUSTERS*2+4 )) | perl -pi -e 's/<b>|<\/b>//g' > $OutFile
        
        echo "Done!"
done

Firstly, specify what shell interpreter your script is for.
If it's bash script, add the first line in the script as (or wherever you 'bash' binary lives):

#!/bin/bash

Secondly, 'man bash' yields the following:

     export [-fn] [name[=word]] ...
     export -p
          The supplied names are marked for automatic  export  to
          the  environment of subsequently executed commands.  If
          the -f option is given, the names refer  to  functions.
          If no names are given, or if the -p option is supplied,
          a list of all names that are exported in this shell  is
          printed.   The  -n option causes the export property to
          be removed from the named variables.  export returns an
          exit  status  of  0 unless an invalid option is encoun-
          tered, one of the names is not a valid  shell  variable
          name, or -f is supplied with a name that is not a func-
          tion.

Sorry, I only extracted relevant parts of my script, I forgot to keep the first line, I just modified my code above to keep it clear here ...

As for the use of export command, I still do not understand. I never used it before and I could not understand what the man actually says. I usually use setenv directly from the shell, but in scripts I was told here that I should use export to be able to call my executable on the next line... The problem is that export is not updating to the new values at every iteration of the loop (the value of $DIR is changing, I checked by echoing the whole export line, so that means the problem is in the way export behaves). The executable is called again and again with the very first value $DIR which is the only thing that changes in my QUERY_STRING. What am I doing wrong?

export var=value

This isn't PHP code, why call it that?

Not only is ls unnecessary, but it will break your script if any filenames contain spaces or other pathological characters.

for DIR in $WorkDir/*/$subdirName/

That is unnecessary if you use the line I suggested above.

export QUERY_STRING="workdir=$DIR&nb1=$NB1&nb2=$NB2"

I just noticed the = in red, sorry! I changed my line into

export QUERY_STRING="workdir=$DIR&nb1=$NB1&nb2=$NB2"

and now everything seems fine.

Strange that before I didn't get any errors (strange and dangerous too!!)

Thanks guys for the help, I greatly appreciate it.

Is there a better way to strip out <b> and </b> symbols from the piped output besides calling perl?

$Executable | tail -$(( $NBCLUSTERS*2+6 )) | head -$(( $NBCLUSTERS*2+4 )) | perl -pi -e 's/<b>|<\/b>//g' > $OutFile

(By the way, I just click on the PHP button where I'm typing the message to capsulate my code, not because my code is PHP, now I just wrote [ code ] and [ /code ] manually)

As for using

for ID in `ls $WorkDir`; do

my reason is that I want to echo $ID on the screen after every iteration...

for ID in $WorkDir/*; do 

gives the full directory name where $ID exists, so I will need to strip everything before the last /

echo 'foo <b>bar</b> <b>fred</b>' | sed -e 's#<b>##g' -e  's#</b>##g'