I can run a php file in Bash script but I can't run with variables
Non-variables with Working:
#!/bin/bash
data=$(/usr/bin/php -q /home/PATH/contest.php);
Variables with Not Working:
#!/bin/bash
data=$(/usr/bin/php -q /home/PATH/contest.php data=Nov14&day=3);
What is the reason of this?
I solved.
You can easily parse command line arguments into the $_GET variable by using the parse_str() function.
I added my PHP script top
parse_str(implode('&', array_slice($argv, 1)), $_GET);
PHP Documents
Related
I need help in passing the Linux bash-script variable to execute the PHP CLI command.
I am working on the bash-script that executes PHP CLI command that gets input variable from the bash-script like folder-path to include a file for accessing a class of the PHP file.
But what happens while executing the bash-script is that the variable passed to the PHP CLI-command act as a variable for PHP.
Below is the sample-code my script
file="$folderpath"somefile.php
mage_version=$(php -r 'include "$file";print_r(Mage::getVersionInfo());')
Below is the error I am getting
PHP Notice: Undefined variable: file in Command line code on line 1
PHP Warning: include(): Filename cannot be empty in Command line code on line 1
Bash needs double (") quotes to parse variable, otherwise they will stay $var and so php will read them;
file="$folderpath"somefile.php
mage_version=$(php -r "include \"${file}\";print_r(Mage::getVersionInfo());")
More inline bash info
If you need a multi line solution, you can do something like: Run PHP function inside Bash (and keep the return in a bash variable)
php_cwd=`/usr/bin/php << 'EOF'
<?php echo getcwd(); ?>
EOF`
echo "$php_cwd" # Or do something else with it
When i'm writing the command ./test.sh testvalue in the Terminal
it is writing "testvalue" in a script with sed.
But when it runs with php shell_exec('./test.sh testvalue')
the php script is writing hello on the page (because in bash, I used echo)
but the bash script isn't writing testvalue in the test.txt data.
Why?
I used chmod a+x+r for php and bash?
Please help me!
Thank you!
My PHP:
<?php
session_start();
$out = shell_exec('./test_sed.sh $test');
echo "$out";
?>
My Bash:
#!/bin
new_pw=$1
sudo sed -i '/^wpa_passphrase/d' test.txt
sudo sed -i '$awpa_passphrase='$new_pw test.txt
echo 'hello'
I suggest you to check:
on the PHP script you should change the string into double quotes(so it can interpret variables inside)
define $test variable (it is undefined)
on the PHP script you are trying to execute test_sed.sh but on the description you have wrote test.sh. Check if is the what you mean
and last if you have problems with executing the shell script from CLI too then you should check the shebang. If you have this problem you should try #!/bin/bash
I hope it helps
I'm trying to terminate a shell script using PHP code.
I have created a shell script foo.sh which calls a PHP file
#! /bin/bash
cd /var/www/html/test
php test.php
Following is test.php
<?php
exec('exit 0');
?>
For some reason the shell script is not exiting.
Your exit(0) in PHP will terminate process with PHP itself, and not parent process (if any).
To terminate your bash script, you will need to find it's pid via ps and grep, or, alternatively, use killall:
system('killall foo.sh');
-in PHP
I need to write a bash script that wraps a php script,
I have some variables that needs to be forwarded to the php script and some variables that I need internally for the bash script it self.
The call for the shell script should look like that but the php file can have more params so it needs to be generic :
bash /tmp/test.sh -c -l /tmp/aaa -php aaa.php -d -p 3 -f 2012-10-23
the -php option is mandatory because it contains the php file that needs to be called the -c and -l are optional flag and needs to be used internally for the bash script, everything after the aaa.php are params for the php file.
bash /tmp/test.sh -c -l /tmp/aaa -php "aaa.php -d -p 3 -f 2012-10-23" ?
Don't know how you are passing anythin inside bash script, bu basically everything wrapped in apostrophes is treated as one argument in bash script, passed further to php interpreter is seen as separated parameters, unless of course you don't wrap it again.
How we run php script using Linux bash?
php file test.php
test.php contains:
<?php echo "hello\n" ?>
From the command line, enter this:
php -f filename.php
Make sure that filename.php both includes and executes the function you want to test. Anything you echo out will appear in the console, including errors.
Be wary that often the php.ini for Apache PHP is different from CLI PHP (command line interface).
Reference: https://secure.php.net/manual/en/features.commandline.usage.php
First of all check to see if your PHP installation supports CLI. Type: php -v. You can execute PHP from the command line in 2 ways:
php yourfile.php
php -r 'print("Hello world");'
There are two ways you can do this. One is the one already mentioned, i.e.:
php -f filename.php
The second option is making the script executable (chmod +x filename.php) and adding the following line to the top of your .php file:
#!/path/to/php
I'm not sure though if a webserver likes this, so if you also want to use the .php file in a website, that might not be the best idea. Still, if you're just writing some kind of script, it is easier to type ./path/to/phpfile.php than having to type php -f /path/to/phpfile.php every time.
Simply this should do:
php test.php
just run in linux terminal to get phpinfo .
php -r 'phpinfo();'
and to run file like index.php
php -f index.php
php -f test.php
See the manual for full details of running PHP from the command line
php test.php
should do it, or
php -f test.php
to be explicit.
I was in need to decode URL in a Bash script. So I decide to use PHP in this way:
$ cat url-decode.sh
#!/bin/bash
URL='url=https%3a%2f%2f1%2fecp%2f'
/usr/bin/php -r '$arg1 = $argv[1];echo rawurldecode($arg1);' "$URL"
Sample output:
$ ./url-decode.sh
url=https://1/ecp/