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:
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?
#!/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?
(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 /