0

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?

2
  • How are you setting $term in the PHP script? Also, it looks like you're trying to both put single-quotes around the value of $term (in "script.sh '".$term."'"), and also add quotes/escapes with escapeshellarg (although you do that after using it, so it has no effect). You want to do one or the other, not both. Commented Feb 11, 2021 at 20:03
  • Hi @GordonDavisson I'm not setting $term in the PHP script, I'm attempting to carry that argument/variable over from the bash script to the PHP script. So for example, when I run the the script I can do ./script.sh foo and it queries the API and returns an answer to my query. I basically want to do that same thing where I run php php_file.php foo by utilizing the bash script. The use case is so when someone goes to site.com/file.php they get the output of the script (which I already have working). But wanting them to be able to do something like site.com/file.php?term=foo Commented Feb 11, 2021 at 20:40

1 Answer 1

1

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

$term = $argv[1];

And run:

php file.php argument
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Kerby82 This looks like what I needed (in addition to the issue with my escaping the argument twice) in order to achieve the wanted output. I'll go ahead and mark this as accepted as it answers my original question. However I would like to ask one more thing, and that is how I can turn that $argv term into something that a user could query (i.e. site.com/file.php?term=foo and foo being $argv. If that is a little more in depth, then no worries. I can work with what you provided thus far.
I guess you should consider all the implications of running a shell command using an argument passed from an URL. That can be a serious security issue. Anyway in that case you should use $term=$_GET["term"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.