Passing shell variables to a webpage

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Passing shell variables to a webpage
# 1  
Old 12-11-2011
Passing shell variables to a webpage

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

This is my assignment as a whole - Use SVG to present certain dynamic (the raw data should change at least once a day) data visually. Consider using any of the conventional ways of that illustrating data (charts, figures, graphs, maps. etc.) or consider more experimental ways of visualizing data such as shown in the work of Edward Tufte or in some of the illustrations of similarity using OWL. Hopefully, your illustration will help guide insights about the data that might not be apparent to someone just looking at the raw textual or numeric data.




Here's the issue. I have both the client and the server side parts done but with place holder values on the client page. I'm having issues figuring out how to get them to talk or rather pass variables to each other.

For simplicity lets work with a simple example and I can then apply that to my project. Say I have a php page or even a simple html/javascript(if possible) page that a user accesses via their web browser. I want to print or alert the variable $A. I have a server side shell script that simply assigns the variable $a to "hello." I want to somehow send that variable to my php/javascript page so that $A is also hello and the alert or echo/print is no longer blank.

Any ideas? I've used POST and GET to send data to a shell script before but its been a one way street with the shell script dynamically building the output page. Can I call the url of my cgi script and have that script then open the url of my webpage with parameters such as 'testpage.domain.com/?A=hello'


2. Relevant commands, code, scripts, algorithms:

Since I don't really want anyone to do my project for me I've outlined a simple example above so there isn't really any code to post.



3. The attempts at a solution (include all code and scripts):

I've not tried any actual coding yet because I'm not sure where to start with this but I've Google'd quite a few variations of my question to no avail.

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Slippery Rock University, Slippery Rock, PA, United States of America, Dr. David Dailey, CPSC 317

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
# 2  
Old 12-12-2011
Quote:
Originally Posted by ChedWick
Here's the issue. I have both the client and the server side parts done but with place holder values on the client page. I'm having issues figuring out how to get them to talk or rather pass variables to each other.

For simplicity lets work with a simple example and I can then apply that to my project. Say I have a php page or even a simple html/javascript(if possible) page that a user accesses via their web browser. I want to print or alert the variable $A.
Does this variable actually exist anywhere, or is it just sitting in some file?

What do you need to be able to do with this variable?
Quote:
I have a server side shell script that simply assigns the variable $a to "hello."
Shell variables only make sense in a shell...

You could export the variable, then have your PHP executed like this:

Code:
# variable file

export A="asdf"

Code:
#!/bin/sh

. variablefile

/usr/lib/php5.3/bin/php <<EOF
<?php
        printf("variable A is %s\n", getenv("A")); ?>
EOF

exit

But I get the feeling you want to have more control over the variable than that, you need to set and retrieve it when you please, not just when first executed, and do so in a manner safe across several connections.

This is the sort of thing they usually use a database with. The combination of Linux, Apache, Mysql and PHP is so common they usually call it LAMP...

Last edited by Corona688; 12-12-2011 at 12:37 PM..
# 3  
Old 12-12-2011
Quote:
Originally Posted by Corona688
Does this variable actually exist anywhere, or is it just sitting in some file?
Firstly, thanks for posting I really appreciate any help.

Now at the moment in my simple example the variable is just sitting within that script in my cgi directory. I have another file (at this point its simply a html file) in my public web directory.

Quote:
What do you need to be able to do with this variable? Shell variables only make sense in a shell...
Well with my project I need to retrieve a variable from my server script and apply it to the webpage that the user is viewing so that it can dynamically create an svg graph (a rather ghetto graph with simple shapes). But for my simple example I just want to be able to load my html file and have a variable get assigned to the value of a simple variable in my shell script and then either print it or alert it.


Quote:
You could export the variable, then have your PHP executed like this:

Code:
# variable file

export A="asdf"

Code:
#!/bin/sh

. variablefile

/usr/lib/php5.3/bin/php <<EOF
<?php
        printf("variable A is %s\n", getenv("A")); ?>
EOF

exit

But I get the feeling you want to have more control over the variable than that, you need to set and retrieve it when you please, not just when first executed, and do so in a manner safe across several connections.
Thanks for that advice. I'll look into some of those techniques. Ideally I'm just looking to have the variable set and retrieved when the page is loaded or refreshed. I'm also not too worried about safe transfers with this project but it certainly good to keep that in mind for the future.

Now after talking to some of my classmates I might have found a way that may help. I'm gonna play around with what you've given me here as well as some AJAX and hopefully get somewhere. I'll post back later if I get what I want done.
# 4  
Old 12-12-2011
Quote:
Originally Posted by ChedWick
Ideally I'm just looking to have the variable set and retrieved when the page is loaded or refreshed.
Just thought of a potentially much simpler way. PHP has a built-in facility which lets you carry around variables between PHP calls. You might just be able to cram it into the $SESSION variable, which PHP stores either on the server or on the user machine with cookies.

See session basic examples

Code:
<?php
session_start();
// Use $HTTP_SESSION_VARS with PHP 4.0.6 or less
if (!isset($_SESSION['count'])) {
  $_SESSION['count'] = 0;
} else {
  $_SESSION['count']++;
}

printf("You have accessed this page %d times\n", $_SESSION['count']); ?>

# 5  
Old 12-13-2011
My AJAX lead was helpful. I went to my professor about it and he showed me a few things and I've come up with the code below (with a good bit of help from my professor). I was able to get it to accomplishing what I wanted, then I broke it Smilie. Currently I have it activate as a result of a mouseover and it sorta works but its returning null value now. I must be missing something simple so I would appreciate a second set of eyes.

My javascript code
Code:
function createRequestObject() {
        var ro = new XMLHttpRequest();
        return ro;
}

var http = createRequestObject();

function send() {
        http.open('get',"url place holder");
        http.onreadystatechange = doSomething;
        http.send(null);
}

function doSomething() {
        if(http.readyState == 4){
        q=http.responseText
        // this works atm 
            alert(q)
    }
}

My shell script

Code:
#! /bin/sh
echo "Content-type: text/html"
echo

$a=happy
echo $a


I do realize this way of going about it may be out of scope of Unix help forum but I do appreciate the help.
# 6  
Old 12-16-2011
Issue solved. I've been working with php so much recently, I didn't realize you don't declare shell variables with a $ sign. The above codes work and accomplish what I needed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing information to a file on webpage

ok so I have a file on a website. this file is a plain text file i need to be able to update the contents of this file from any internet enabled unix box. does anyone have ideas on how it can be done, without using scp/ftp? i know wget can be used to download the file: wget... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. UNIX for Dummies Questions & Answers

Passing Global Shell variables to awk

Hi All, Iam trying to pass global shell variables and is not working Main script is like below CYEAR=`date +"%y"` CFYEAR=`date +"%Y"` CMONTH=`date +"%m"` if then PMONTH=12 PYEAR=`expr $CYEAR - 1` PFYEAR=`expr $CFYEAR - 1` else PMONTH=`expr... (6 Replies)
Discussion started by: baanprog
6 Replies

3. Shell Programming and Scripting

Passing awk variables to shell

Hi. I need to parse file and assign some values to variables, right now i do like below MYHOMEDIR=`awk '/Home/ {print $NF}' output.txt` MYSHELL=`awk '/Shell/ {print $NF}' output.txt` PRGRP=`awk '/Primary/ {print $NF}' output.txt` SECGRP=`awk '/Second/ {print $NF}' output.txt` In this... (10 Replies)
Discussion started by: urello
10 Replies

4. UNIX for Dummies Questions & Answers

Passing Shell Variables to an awk command

Hello, I have two files File1 & File2. File1 76 135 136 200 250 345 .... File2 1 24 1 35 1 36 1 72 .... I want to get all the values form File2 corresponding to the range in File 1 and feed it to a program. Is the code below right? Can I pass shell variables to awk in this... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

5. Shell Programming and Scripting

Passing gnuplot variables to shell script

Hi, I need to pass a gnuplot value to a shell script. I have a main shell script (Main.sh) that has a bunch of gnuplot commands. Main.sh calls another gnuplot script (Child.gnu). A part of the code in Child.gnu is as follows: sp '</data/src/scripts/results/plot_data.sh $col' u (A):2:3 w pm3d... (8 Replies)
Discussion started by: annazpereira
8 Replies

6. Shell Programming and Scripting

Passing shell variables to a rsh command

I noticed my script is not passing the value of variable alert to the rsh command. I need some assistance, please. This is a solaris environement. Thanks! :confused: #!/bin/sh echo -n "Alert number:" read alert rsh rhost_name 'egrep $alert /opt/var/log/*.logs' (2 Replies)
Discussion started by: lopus
2 Replies

7. Shell Programming and Scripting

Passing the nawk variables to the shell

nawk '($1 ~ "1000") && ($1 ~ "5665" ) { sub ($6,"89");flag =1;print }' old.txt >> new.txt I want to set a flag in awk , if the both conditions are met. I want to pass this flag to shell Can anyone please help me on this (1 Reply)
Discussion started by: prav076
1 Replies

8. Shell Programming and Scripting

Passing Shell Variables in ISQL

Hi.. I am passing a variable in my shell function. I need to access it for an isql comand in the shell script. However the isql o/p gives no results if i pass a variable in the command. The isql command works perfectly fine if i hardcore the table name. My script is : ... (0 Replies)
Discussion started by: dikki
0 Replies

9. Shell Programming and Scripting

passing two variables into a shell script?

Hello all, i have a infile.txt text file which contains such variables: aaa 123 asds 1323 asdsd 13434 lkjlkj 324324 23432 lkjlkj 24324 ljlkj 3j4lk 234kj3 and i want to pass them to my script such as: ./myscript $1 $2 where $1 is the first value in the first row and $2 is the second... (2 Replies)
Discussion started by: Bashar
2 Replies

10. Shell Programming and Scripting

Passing shell variables to awk program..

Hello, Can we pass shell variables like $PATH etc. to a awk program part for example, awk ' { fieldValue=$PATH .... }' file (1 Reply)
Discussion started by: Vishnu
1 Replies
Login or Register to Ask a Question