How to pass bash script variable value in PHP command (CLI) - php

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

Related

Bash Script - PHP script with Variables

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

Add current date to PHP command line argument

I'm trying to use PHP command line (from cron tab). I know how to add arguments like this:
cd /home/users/public_html/; php -f script.php some_value
I would like (or need) to add current date dinamically:
cd /home/users/public_html/; php -f script.php current_date
With wget I did this:
wget "https://mysitecom/script.php?currentdate=`date +\%s.\%N`"
But I can not find any way to do something similar wih a php command line.
I've tried:
cd /home/users/public_html/; php -f script.php `date+\%s.\%N`
And I get the error "Command not found".
I've tried also the solution proposed in one answer:
cd /home/users/public_html/; php -f script.php date+\%s.\%N
And I get the literal string "date+\%s.\%N"
With the other proposed solution:
cd /home/users/public_html/; php -f script.php "$(date +"%s.%N")"
I get these errors in the email sent by the cron:
/usr/local/cpanel/bin/jailshell: -c: line 0: unexpected EOF while looking for matching `"'
/usr/local/cpanel/bin/jailshell: -c: line 1: syntax error: unexpected end of file
When using PHP from the command line (CLI) they are not called GET variables they are called arguments
There are 2 parameters passed to every script run from the command line called $argv and $argc
One thing to remember the first argv[0] occurance holds the name of the script that is being run. Other than that the arguments appear in $argv[] in the order thay appear on the command line
argc is a count of how many variables have been passed to the script
arcv is an array of all the variables passed
If its any help they are just like the "C" equivalents if you have ever written any "C" code
ADDITIONAL INFO
To call your script with todays date use something like this
cd /home/users/public_html/; php -f script.php "$(date +"%s.%N")"
adjust the format as required.
Although if you want todays date in the script I am not sure why you would not get that from within the PHP script itself.
Finally I've made it work. It was a syntax error:
This:
cd /home/users/public_html/; php -f script.php `date+\%s.\%N`
Need to be:
cd /home/users/public_html/; php -f script.php `date +\%s.\%N`

Running shell commands in .php file from command-line

I have a series of shell commands I want to put in a program and execute the program from the command line. I decided to use PHP for this so currently I am trying to get the most basic shell commands to run.
Save as build.php
<?php
shell_exec('cd ..');
echo "php executed\n";
?>
From the command-line
php build.php
Output
php executed
Php executes correctly but I'm still in the same directory. How do I get shell_exec( ... ) to successfully call a shell command?
You need to change the cwd (current working directory) in PHP... any cd command you execute via exec() and its sister functions will affect ONLY the shell that the exec() call invokes, and anything you do in the shell.
<?php
$olddir = getcwd();
chdir('/path/to/new/dir'); //change to new dir
exec('somecommand');
will execute somecommand in /path/to/new/dir. If you do
<?php
exec('cd /path/to/new/dir');
exec('somecommand');
somecommand will be executed in whatever directory you started the PHP script from - the cd you exec'd just one line ago will have ceased to exist and is essentially a null operation.
Note that if you did something like:
<?php
exec('cd /path/to/new/dir ; somecommand');
then your command WOULD be executed in that directory... but again, once that shell exits, the directory change ceases to exist.

Can I pass $_GET parameters to a script running locally?

I'm trying to launch from the command line:
$ /usr/bin/php -f "index.php?refresh_words=1"
Could not open input file: index.php?refresh_words=1
Off course, without ?refresh_words=1 it's running ok. Can I pass $_GET parameters from the command line?
You can't do this that way. Use argv and argc. See Command Line Usage section in manual

Running php script (php function) in linux bash

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/

Categories