0

I've connected to an API with a bash script as follows:

#!/bin/bash

curl "https://example.com/templates/search?field=template_id&    field=name" \
-H "Authorization: Bearer ----------------------------------------------------------------"

curl -X POST "https://example.com/audits" \
-d '{ "template_id": "template_28c5b7ec77f34ea7881b6a9ef9c01b91", "header_items": [ { "item_id": "f3245d40-ea77-11e1-aff1-0800200c9a66", "label": "Audit Title", "type": "textsingle", "responses": { "text": "${title}" } }, { "item_id": "f3245d43-ea77-11e1-aff1-0800200c9a66", "label": "Conducted By", "type": "textsingle", "responses": { "text": "John Citizen" } } ] }' \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ----------------------------------------------------------------"

This connects successfully.

I then have a PHP webpage with a form in which you can enter a title for the audit. When I press save on the form the script above runs and creates the audit. However, the title is ${title} rather than the value of the textbox.

Is it even possible to post variable values to a bash script in this way?

  • Well, the variable is within single quotes, so the shell won't be able to expand it. – Kusalananda May 3 '18 at 9:14
  • @Kiwy - There's nothing sensitive in my question. – Ross May 3 '18 at 9:15
  • @Kusalananda - Can you give me an example of what I should be doing please? – Ross May 3 '18 at 9:16
  • @Kusalanada - SafetyCulture is not my company, anyone can use their API. I removed the authorization code and replaced with dashes already. – Ross May 3 '18 at 9:22
3

Updated with comments and chat discussion taken into account.

The PHP code runs the shell script as sh ./curl.sh $title where $title is a PHP variable (it should ideally properly quote the $title data as to not confuse the shell). The shell script therefore needs to pick the title from its command line parameters, $*, rather than from the shell variable $title.

The JSON document that sent is single quoted which means that the variable expansion $title will not be seen by the shell.

You need to break out of the single quoted string temporarily to get the variable expanded:

-d '{ "template_id": "template...{ "text": "'"$*"'" }...

The outer double quotes in "'"$*"'" belong to the JSON document, the contents within single quotes belong to the shell. The inner double quotes quotes the $* value (in case it contains whitespace and/or filename globbing patterns).

There was an additional issue in the PHP code which meant that the PHP $title variable was never set properly. When this was fixed, it worked as expected.

  • This doesn't work, the value i'm posting still doesn't become the name of the audit. – Ross May 3 '18 at 9:27
  • Ok, so there's something I missing then. How are you setting the $title shell variable? – Kusalananda May 3 '18 at 9:28
  • if(isset($_POST['submit'])) { $output=shell_exec('sh ./curl.sh $title'); echo $output; } ?> <?php echo $_GET["title"]; ?> -- This is what i've got in my PHP file. – Ross May 3 '18 at 9:30
  • Ah, then you should use $@ in place of $title. If the title is passed as an command line parameter to the script, it will be available in $@. You would still need to use "'"$@"'" though. – Kusalananda May 3 '18 at 9:30
  • This still isn't working? – Ross May 3 '18 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.