Javascript -> Shell Script | Unix Linux Forums | Web Programming

  Go Back    


Web Programming Discuss Web Programming and Web Server Administration, including LAMP, Apache, MySQL, HTML, SEO, and other Web APIs and topics.

Javascript -> Shell Script

Web Programming


Tags
cgi, shell

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-20-2008
mark007 mark007 is offline
Registered User
 
Join Date: Jun 2008
Last Activity: 3 December 2009, 5:02 AM EST
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Javascript -> Shell Script

Hi all,

I am trying to call a shell script from a javascript function. This works fine and the shell script returns everything I expected BUT I cannot figure out how to pass command line arguments to this shell script.

Using GET, I assume the url being called needs to be one string with no spaces (hence i cannot do url="myScript.sh arg1 arg2 arg3"

Using POST, I am sending the post variables like this

var args="a=b";
req.open("POST", url, true);
req.send(args);

but do not know how to read those from within my shell script.

Anyone know how to do it? TYVM!
Sponsored Links
    #2  
Old 08-20-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
The CGI spec defines how arguments are made available to the CGI script. You can pass data using either GET, in $QUERY_STRING, or POST, as standard input. Transforming these to the moral equivalent of command-line arguments needs to be performed inside the script. If you cannot modify it, write a wrapper which does the transformation and then invokes the real script with the command-line arguments it expects.
Sponsored Links
    #3  
Old 08-24-2008
redoubtable redoubtable is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 7 December 2010, 12:28 PM EST
Location: Portugal
Posts: 244
Thanks: 0
Thanked 0 Times in 0 Posts
Side note: not all browsers allow you to execute shell commands through javascript (and enabling this is really dangerous). The only way javascript can ask for safe execution is on server-side using ajax -- this is used to make really dynamic pages which don't need page reload to retrieve information.
    #4  
Old 08-24-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
If the conduit is CGI, I would imagine that we are talking server-side in any event here.
Sponsored Links
    #5  
Old 08-24-2008
mark007 mark007 is offline
Registered User
 
Join Date: Jun 2008
Last Activity: 3 December 2009, 5:02 AM EST
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Ya thanks all

Simple javascript "ajax" call to the script using "GET" worked. Command line arguments come in in the env variable $QUERY_STRING. I find it hard to write a command line input parser so I might have to go back to my old way of doing things.

javascript -> calls php script (and easily parses parameters) -> calls shell script

as opposed to

javascript -> directly calls shell script (bit annoying to parse the parameters which might be in form "a=1&b=2&bla=5")

Anyone know of an existing lump of code to let me parse these, like its easy to get them in php using $_GET['a'] and $_GET['b] etc
Sponsored Links
    #6  
Old 08-24-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
Shell script is kind of brittle when it comes to proper quoting of user-specified arguments etc so you need to be really careful here. Perhaps wrapping the call in PHP is not such a bad idea (although PHP too has a bit of a track record when it comes to security problems .... /me ducks) and make really really sure you use proper quoting everywhere in the script and in everything which invokes it. And keep in mind that security checks in JavaScript are ineffective; somebody could simply be connecting directly to the CGI script, without going through your form (or with JavaScript disabled).

As such, it's not very hard to split on & with IFS='&'. IFS=& query_string - Google Search brings up some matches but I would regard all of them with extreme suspicion. If you see a variable interpolation without double quotes around it, run away.

Last edited by era; 08-24-2008 at 01:13 PM.. Reason: Note that JavaScript input checking is ineffective
Sponsored Links
    #7  
Old 08-24-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
Parse query function - IT Community looks like it's rather competently coded, although it's Bash only. Actually seems like it's probably one of our members who submitted it -- cfajohnson
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to use javascript code in unix shell? l_gshankar24 Shell Programming and Scripting 4 02-20-2012 02:52 AM
want to use javascript as shell script anuajay1988 Shell Programming and Scripting 4 09-27-2011 07:30 AM
Why using this kind of format in web programming <script type="text/javascript"><!-- ...//--></scrip Anna Hussie Web Programming 2 05-23-2011 11:17 PM
Calling Shell script in javascript DeepakXavier Shell Programming and Scripting 1 11-21-2005 08:26 PM
Javascript: Edit a script ? perleo Shell Programming and Scripting 1 05-23-2003 06:45 PM



All times are GMT -4. The time now is 02:11 PM.