Invoking CGI executable after setenv (in bash)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Invoking CGI executable after setenv (in bash)
# 1  
Old 02-26-2009
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:

PHP Code:
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
# 2  
Old 02-26-2009
Quote:
Originally Posted by aplaydoc
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:

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

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

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

It shouldn't, though it could depend on the contents of the variable.
# 3  
Old 02-26-2009
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
# 4  
Old 02-26-2009
Quote:
Originally Posted by aplaydoc
Any quick fix?
My crystal ball is being repaired, so please post your code.
# 5  
Old 02-26-2009
Sorry, here it is:

PHP Code:
#!/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+)) | head -$(( $NBCLUSTERS*2+)) | perl -pi -'s/<b>|<\/b>//g' $OutFile
        
        
echo "Done!"
done 

Last edited by aplaydoc; 02-26-2009 at 08:17 PM..
# 6  
Old 02-26-2009
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):
Code:
#!/bin/bash

Secondly, 'man bash' yields the following:
Code:
     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.

# 7  
Old 02-26-2009
Quote:
Originally Posted by vgersh99
Code:
#!/bin/bash

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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script make itself executable

Is there a way to make this make itself executable? Thanks. :-) cat > somescript.sh << \EOF #!/bin/bash block_count=$(sudo tune2fs -l /dev/sda1 | awk '/^Block count:/ {print $NF}') reserved_block_count=$(sudo tune2fs -l /dev/sda1 | awk '/^Reserved block count:/ {print $NF}') perl -e... (4 Replies)
Discussion started by: drew77
4 Replies

2. Programming

Calling bash script from CGI

Hi, I am having two individual scripts Script 1): CGI script - is a simple script which trigger bash script Script 2): Bash script - is a script which execute which collects file system utilization information from all the Unix servers If I am executing CGI script manually from command... (2 Replies)
Discussion started by: Naveen.6025
2 Replies

3. Shell Programming and Scripting

Bash scripts - CGI and ssh

Hi Everyone, I started looking at the possibility of making some of our bash scripts available through a web server using CGI and the simple ones works just fine. Now I need to execute remote commands using ssh but can't really get it to work. I got private keys all sorted. Must be ssh... (1 Reply)
Discussion started by: arizah
1 Replies

4. Shell Programming and Scripting

Invoking a bash shell with an export var

Hello all, How can I invoke the bash shell (via command line) to execute another command by setting an exported environmental variable on the fly (as this env var would be used by the command -another script, the bash would execute). This needs to be done in one single line as the same would... (4 Replies)
Discussion started by: Praveen_218
4 Replies

5. Shell Programming and Scripting

Running an executable from bash prompt

Hi, I'm trying to run a program from the bash prompt and I don't understand why it is returning with an error. Dig is my C program, and it takes in parameters J4, detect, 3 and 0182F98E var1="cygdrive/c/2i/test fixture/software/mccdaqtest/debug/Dig J4 detect 3 0182F98E" when I do ... (6 Replies)
Discussion started by: oahmad
6 Replies

6. UNIX for Dummies Questions & Answers

Running Executable in Bash Script

Hey guys, so I've been trying to write a bash script called runSorter.sh that runs an executable that also takes in some parameters and outputs the results to a text file. The executable, sorter, takes in a number parameter. I want to make it so that you can input as many number parameters into... (4 Replies)
Discussion started by: Duo11
4 Replies

7. Shell Programming and Scripting

Delete Files with CGI Bash Script

Hi. I could use some help with my problem. I am creating a website. One option on this website is to delete a specified file, say an image, when the user clicks on it. I want to do this with CGI. I believe bash will be the easiest since I will just type the command "rm file". I also do not know... (4 Replies)
Discussion started by: JMooney5115
4 Replies

8. Shell Programming and Scripting

invoking a shell script inside cgi shell script

Hi, I have an HTML form through which I get some text as input. i need to run a shell script say script.sh inside a perl-cgi script named main_cgi.sh on the form input. I want to write the contents of the form in a file and then perform some command line operations like grep, cat on the text... (2 Replies)
Discussion started by: smriti_shridhar
2 Replies

9. Shell Programming and Scripting

Wrapping a bash script for CGI

I hope its ok to start a new thread, I was going to use my existing one but thought a new one would better clarify things and provide better search for future reference. Well I have my bash program working now, all nice, user input validated, output formatted, everything is looking sexy. Now... (2 Replies)
Discussion started by: andyj
2 Replies

10. UNIX for Dummies Questions & Answers

How do I make a cgi script World-executable

I'm trying to set up a form mail script on a website that when Submitted, a cgi script is executed and a perl translator (located in a secured and inaccessible folder) translates the script. After speaking to my hosting provider, I was told to "Telnet into the system and make the script... (2 Replies)
Discussion started by: tylerl
2 Replies
Login or Register to Ask a Question