0

I want to call a php script inside shell script and need to send 4 parameters.I called that called that inside shellscript like this.

php /var/www/php/myscript.php $var1 $var2 $var3 $var4

but php script didn't execute. so what is the correct way to send parameters to php script and execute script inside shellscript?

helplakmal
  • 173
  • 2
  • 12

2 Answers2

0

Suppose you have an executable file file.sh:

#!/usr/bin/php
<?php

include('your/file.php');
exit;

or with the current environment

#!/usr/bin/env php
<?php

include('your/file.php');
exit;

Then execute it on the command line with

$ ./file.sh
marekful
  • 14,986
  • 6
  • 37
  • 59
  • Dont forget to add the right permissions to the file if you want to execute it by issue "./file.sh" for example chmod +x file.sh – Pär Särnö Dec 17 '13 at 11:21
0

This is sample, pay special attention to the quotes in the parameters to wrap any character in the string like spaces or dashes that can affect the args reading.

bash.sh

#!/bin/bash
php test.php "$1" "$2"

test.php

<?php

$a = $argv[1];
$b = $argv[2];

echo "arg1 : $a, arg2: $b";

Some Outputs

$bash bash.sh Hello World
arg1 : Hello, arg2: World

$bash bash.sh Hello
arg1 : Hello, arg2:

$bash bash.sh "Hello World"
arg1 : Hello World, arg2:

Hope this helps

casivaagustin
  • 558
  • 5
  • 7