I have a PHP script that takes to command line arguments. I want the user to type the name of the program with no arguments:
$ ./foo.php
and I want it to output something like:
usage: $ ./foo arg1 arg2 where arg1 is something and arg2 is something else
Is there a standard way of doing this?
Many thanks :).
Just output it.
if (count($_SERVER['argv']) <= 1) {
echo 'Usage: $ ' . $_SERVER['argv'][0] . ' arg1 arg2 where arg1 is something and arg2 is something else' . PHP_EOL;
}
$_SERVER['argv'] contains all arguments from the command line and especially the first item is always the scriptname.
See "reserved variables: $argv" and "reserved variables: $_SERVER". Note, that $argv is no available in any case, thus I recommend using $_SERVER['argv']
You have to use ARGV to get these values from command line input.
http://jm2.php.net/manual/en/reserved.variables.argv.php
Related
I hope you can help me.
I am dealing with a problem that I cannot solve. This is my issue. I am trying to exec a bash script through PHP. I tried with the method
exec()
with 3 arguments, arg1, arg2, and arg3.
php code
<?php exec("./randomScript.sh arg1 arg2 arg3"); ?>
randomScript.sh
.... # random code which exploits the three arguments -> executed normally..
.... # random code which exploits the three arguments -> executed normally..
./secondScript.sh $arg1 $arg2 $arg3 #<- this is the script that is not running (is not even started).
I have tried to change the permission (I've got full permission), change the way I call the randomScript.sh (through absolute path), but nothing occurred. Besides, I tried with:
shell_exec()
method, but nothing changed. Of course, if I run the secondScript.sh via terminal everything works fine.
I cannot understand which is the problem. Could you please help me?
Thanks in advance.
In the inner shell, the arguments are not called like that (because it is invoked by the shell, not by PHP).
./secondScript.sh "$1" "$2" "$3"
or just
./secondScript.sh $*
At this stage, just remember that spaces, quotes, and dollar signs in the arguments you pass must be avoided at all costs (the solution is escaping them, but how to do it exactly is tricky).
You also might want to do instead:
$ret = shell_exec("./randomScript.sh 'arg1' 'arg2' 'arg3' 2>&1");
so that in $ret you might find error messages and output from the inner shell.
You should escape your string command before passing it in exec function, i think it may help you
Escapeshellcmd()
...
$escaped_command = escapeshellcmd("./randomScript.sh arg1 arg2 arg3");
exec($escaped_command);
...
Escapeshellarg()
For system command, i recommand using system
...
$escaped_command = escapeshellarg("./randomScript.sh arg1 arg2 arg3");
system($escaped_command);
...
You also need to make sure that your PHP code does not change working dir, and both shell scripts have execute permissions, and filesystems allows to exec files on it.
I would avoid exec("./script $arg ...");, I would rather specify the interpreter to use and full path to the script like exec("sh /home/user/project/script.sh $arg ...");
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
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.
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
I want to execute a cron job, but I recieved an e-mail that tells me:
No input file specified.
And I run the following cron command every day at 15:00
/usr/bin/php -q /home/popasur/public_html/analytics/savedata_script.php?paramz=savesmdata
If I remove the "?" I recieve an e-mail with the output, but I also recieve this warning:
Notice: Undefined offset: 1 in /home/popasur/public_html/analytics/savedata_script.php on line 15
$arguments = array();
if (is_array($argv) && !empty($argv)) {
foreach ($argv as $a) {
$a_explode = explode("=", $a);
$arguments[$a_explode[0]] = $a_explode[1]; //line 15
}
}
If you are using $argv, you should run your script like this:
php myScript.php arg1 arg2 arg3 ...
In this situation, you have:
$argv[0] // myScript.php
$argv[1] // arg1
$argv[2] // arg2
and so on.
Thats not usually how you pass parameters on the command line. The reason your script isnt running is that the shell is trying to find a file called /home/popasur/public_html/analytics/savedata_script.php?paramz=savesmdata
and what I think you want is :
/home/popasur/public_html/analytics/savedata_script.php paramz=savesmdata
Then perform whatever exploding you want on $argv[1]
(or use something like getopts to do it properly)
I'm having a problem on passing the parameters from php to python.
By using the $_SERVER['QUERY_STRING'].
http://www.domain.com/path?a_num=123&msg=hello
i will put the a_num=123&msg=hello to a variable
$a = $_SERVER['QUERY_STRING'];
system("python python.py ".$a);
and in python will print it
a = sys.argv[1]
print a
and the result is *a_num=123* only
what is the problem?
I don't think this is a problem with PHP, more how the system command is being executed. Assuming you are using Linux, the '&' character in the command:
python python.py a_num=123&msg=hello
Is being interpreted as a control operator. From the documentation for bash (although this applies equally to other shells such as tcsh):
If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does
not wait for the command to finish, and the return status is 0.
To prevent this, you need to quote the string being passed:
python python.py "a_num=123&msg=hello"
Which in PHP would look like:
system("python python.py \"".$a."\"");
I'd like to run something like (in myProgram.sh):
java -cp whatever.jar com.my.program $1
within PHP and read the output.
So far I have something like:
$processOrderCommand = 'bash -c "exec nohup setsid /myProgram.sh ' . $arg1 . ' > /dev/null 2>&1 &"';
exec($processOrderCommand);
But what I'd really like is to be able to get the output of the java program within the PHP script and not just execute it as another thread.
How can this be done?
You can do this :
exec($processOrderCommand, $output);
From the documentation :
If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().
For a better control on your execution you can take a look at proc_open()
Resources :
php.net - exec()
php.net - proc_open()
The key is that the classpaths need to be absolute within the shell_exec
PHP script.
Or at least that's the only way I could get it to correctly work. Basically it's almost impossible to tell from environment to environment what the relative directory is that the php script is running the JVM.
As well, it helped to put the absolute path location for java, such as usr/.../bin/java