2

I want to pass the string from my php like

<?php
str1="string to pass"
#not sure about passthru
?>

And my tcl script

set new [exec $str1]#str1 from php
puts $new

Is this Possible? Please let me know I'm stuck with this

sam
  • 53
  • 5

2 Answers2

1

It is possible.

test.php

<?php
$str1="Stackoverflow!!!";
$cmd = "tclsh mycode.tcl $str1";
$output = shell_exec($cmd);
echo $output;
?>

mycode.tcl

set command_line_arg [lindex $argv 0]
puts $command_line_arg 
Dinesh
  • 16,014
  • 23
  • 80
  • 122
  • can you explain what is $argv? – Aroon Apr 03 '18 at 09:47
  • and one more thing if i run the php code in my localhost it can able to trigger the tcl script in my server? – Aroon Apr 03 '18 at 09:54
  • @ArunBaskar : Yes, it can. Refer [here](https://www.tcl.tk/man/tcl/TclCmd/tclvars.htm#M46) for `argv` usage. – Dinesh Apr 03 '18 at 09:58
  • I run your php code in localhost but getting parse error @ line 2. – Aroon Apr 03 '18 at 10:01
  • @ArunBaskar : Fixed it. – Dinesh Apr 03 '18 at 10:04
  • Seems like I have already edit like this but the problem is the output shows blank page. then i run my tcl script it is also blank. do i need to give file path somewhere in php? – Aroon Apr 03 '18 at 10:08
  • @ArunBaskar : Ensure the file present in the same directory. Else, give full path. – Dinesh Apr 03 '18 at 10:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168105/discussion-between-arun-baskar-and-dinesh). – Aroon Apr 03 '18 at 10:12
  • You'll have to be careful with strings with whitespace or file globbing chars. You may need to do some quoting tricks in the $cmd strings, or find a PHP shell exec function where you can pass a list of words instead of a single string. – glenn jackman Apr 03 '18 at 14:57
1

The simplest mechanism is to run the Tcl script as a subprocess that runs a receiving script (that you'd probably put in the same directory as your PHP code, or put in some other location) which decodes the arguments it is passed and which does what you require with them.

So, on the PHP side you might do (note the important use of escapeshellarg here! I advise using strings with spaces in as test cases for whether your code is quoting things right):

<?php
$str1 = "Stack Overflow!!!";
$cmd = "tclsh mycode.tcl " . escapeshellarg($str1);
$output = shell_exec($cmd);
echo $output;
echo $output;
?>

On the Tcl side, arguments (after the script name) are put in a list in the global argv variable. The script can pull them out with any number of list operations. Here's one way, with lindex:

set msg [lindex $argv 0]
# do something with the value from the argument
puts "Hello to '$msg' from a Tcl script running inside PHP."

Another way would be to use lassign:

lassign $argv msg
puts "Hello to '$msg' from a Tcl script running inside PHP."

Note however (if you're using Tcl's exec to call subprograms) that Tcl effectively automatically quotes arguments for you. (Indeed it does that literally on Windows for technical reasons.) Tcl doesn't need anything like escapeshellarg because it takes arguments as a sequence of strings, not a single string, and so knows more about what is going on.


The other options for passing values across are by environment variables, by pipeline, by file contents, and by socket. (Or by something more exotic.) The general topic of inter-process communication can get very complex in both languages and there are a great many trade-offs involved; you need to be very sure about what you're trying to do overall to pick an option wisely.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215