Can a script command be used to trigger a particular PHP function in a PHP file via a bash script?

The title of this question is pretty self-explanatory, but I'll provide some more details. There's a PHP file named hello-world.php and a bash script named testBash.sh in my setup.

Inside hello-world.php, there are two methods: helloWorld() and helloName($name).

What I want to achieve is to pass a parameter from my bash script (testBash.sh) and then execute the helloName($name) method using that parameter.

Here's what I've tried so far:

testBash.sh

INPUT="Bobby"

// THIS IS WHERE I AM HAVING TROUBLE
TEST= php -r "require 'hello-world.php'; helloName("$INPUT");"

echo "$TEST"

hello-world.php

function helloWorld() {

    return "Hello, World!";

}

function helloName($name) {

    return "Hello, $name!";

}

In an ideal scenario, when I echo "$TEST", the output of that function should be displayed.

For example: Hello, Bobby!

Is this feasible? I've searched online for solutions, but haven't found one that fits. Any suggestions or advice would be greatly appreciated. Thank you!

Answer №1

Here is the solution:

RESULT=$(php -r "require 'greetings.php'; echo greetName(\"$NAME\");")

You can also try using single quotes for the function parameter:

RESULT=$(php -r "require 'greetings.php'; echo greetName('$NAME');")

To clarify,

  • including echo in PHP ensures that the result of greetName is sent to the output,
  • using $(command) instructs the bash command to treat its output as a value,
  • finally, this value is assigned to the variable named RESULT.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Print values to screen during the execution of a loop in PHP

Is there a method to display content on the screen while executing a lengthy foreach loop? When I run a loop that consumes a lot of time, the output does not show on the screen until the loop has completed - even when using echo statements within the loop ...

How can HTML output be automatically indented?

Seeking a way to automatically indent the HTML generated by my PHP script. I currently use HTML Purifier for validating internal textbox inputs, but I have reservations about using HTML Tidy due to its lack of active development and numerous reported bugs ...

Using $.ajax to fetch a JSON array from a PHP endpoint

Any assistance would be greatly appreciated if I am making a simple mistake somewhere. Associative Array data_form[name] = value; Action $.ajax({ type: "GET", cache: false, url: "../pages/ajax/takeaction.php", data: ...

Include Public in asset directory path for Laravel

Trying to set up Laravel on a shared hosting platform and followed the guide over at , but my asset directory path is missing the public folder. Currently, it looks like this <link href='http://example.com/public/assets/css/style.css' type= ...

Access PUT and DELETE arrays in a PHP controller

Currently, I am attempting to send a request to my controller using a DOJO request. request("/category/", { method: "PUT", data: { original: event.target.getAttribute("data-original"), ...

Filtering nested relations in Laravel

Is there a way to filter query results by translation relation (specifically the name column)? $item = Cart::select('product_id','quantity') ->with(['product.translation:product_id,name','product.manufacturer:id,name&a ...

Using opening and closing curly braces within a PHP foreach loop

I am facing an issue with formatting an array in PHP. The array structure is as follows: Array ( [0] => Array ( [team1_score] => 10 [team2_score] => 5 [round_number] => 1 [teamtitle1] ...

Tips for abbreviating HTML content using PHP

Similar Question: PHP: Truncate HTML, ignoring tags I have some HTML content stored in a database. I need to display this content in a shortened form. I attempted to use the mb_strstr function like so; $str = mb_strstr($this->htmlData, "</p> ...

Customize JSON response formatting based on specific needs

When returning a JSON response to an API, I am utilizing the json_encode() method. The array includes a key for users that can contain multiple users, or be empty, and a key for course that can either be empty or contain a single object as shown below: { ...

Retrieve column names from a table that match a specific pattern by utilizing a query with PDO

Is there a way to retrieve the names of all columns that follow a specific pattern? For instance, retrieving all columns that begin with 'allow'. I prefer to achieve this using only a pure PDO query rather than relying on a PHP array filter. $qu ...

Create eye-catching banners, images, iframes, and more!

I am the owner of a PHP MySQL website and I'm looking to offer users banners and images that they can display on their own websites or forums. Similar to Facebook's feature, I want to allow users to use dynamic banners with links. This means the ...

Connecting a href link to a TAB

Check out this useful CODE example I am using. I have a webpage with links that have href attributes. Now, I want to link these pages to another page and have them automatically open a specific tab when clicked. Is this possible? Here are the links on th ...

Securing the .env file in Laravel: Best practices for safeguarding your

After relocating my project to HOST, I discovered that I am still able to access the .env file by visiting mysite.com/.env. This exposes all the variables and sensitive data within the file. The contents of my .env file are as follows: APP_ENV=local APP_D ...

Ensuring the confidentiality of files stored in string format

I want to discuss the topic of file security with you. Currently, I have a PHP script that retrieves a file from an <input type="file"> tag and then uses file_get_contents() to store the file data in a variable. However, I am concerned about potenti ...

PHP File Permissions Issue When Generating Files

I have come across the following scenario: public_html - 755 => avatar - 777 => poll - 755 When I run the code provided below, I encounter an error (Warning: file_put_contents(../test.php) [function.file-put-contents]: failed to open ...

Issue with the image posting function in Laravel - why isn't it functioning properly?

Looking for Assistance I've been working on a simple web application using Laravel 6.0 and have encountered an issue with the image post function I developed. Despite not receiving any error messages, the functionality doesn't seem to be work ...

What is the process for testing a personalized DQL function?

After following a tutorial, I developed my own custom DQL function. Now I want to write tests for it, but the intricacies of DQL make it challenging. Is there a method available for testing DQL functions? It seems that even the Doctrine project itself do ...

The absence of character encoding is evident at a specific location

I'm attempting to input data into a mysql database. This data often includes German umlauts. This is the method I am using: function insertMenu($content, $date) { $session = $_SESSION['aid']; global $pdo; $pdo->exec('SE ...

Deleting MySQL records with JQuery and PHP

Gradually, I've been transforming a PHP web application that I'm currently developing into Jquery AJAX form submissions. The specific issue I am tackling involves removing table rows. It seems like there is an issue with my JavaScript code becau ...

Tips on extracting only abstract JSON data from an API response?

Description I'm attempting to send a request to an API using PHP cURL $access_token = $tokens['access_token']; $headers = array( "Authorization: Bearer " . $access_token ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,env('USE ...