amy
amy

Reputation: 185

Passing Bash Arguments to PHP File with shell_exec

I have a bash script that I am calling via a PHP file. For starters, the content of these files are as follows:

script.sh

#!/bin/bash
term=$1
curl -H "Accept: text/plain" -s "https://some-api.com/search?term=$term&limit=1"

php file

<?php
$message = shell_exec("script.sh '".$term."'");
$term    = escapeshellarg($term);
print_r($message);
?>

In the above example, the script.sh works find by running something like ./script.sh search-term.

But when running the PHP file to pull that same information, I'm getting errors.

Ultimately I want to run a bash script, including arguments, via a PHP file.

I spent most of my time attempting to use the solutions asked in this answer and feel as though my PHP file should work.

I know my scripting and coding is certainly hacky, and obviously no working - does anyone have any input on what I may be doing incorrectly?

Upvotes: 0

Views: 888

Answers (1)

Kerby82
Kerby82

Reputation: 5146

You can use $argv to get the command line parameters:

$term = $argv[1];

And run:

php file.php argument

Upvotes: 1

Related Questions