Posting a PHP variable into a bash script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












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?







share|improve this question





















  • Well, the variable is within single quotes, so the shell won't be able to expand it.
    – Kusalananda
    May 3 at 9:14










  • @Kiwy - There's nothing sensitive in my question.
    – Ross
    May 3 at 9:15










  • @Kusalananda - Can you give me an example of what I should be doing please?
    – Ross
    May 3 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 at 9:22














up vote
0
down vote

favorite












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?







share|improve this question





















  • Well, the variable is within single quotes, so the shell won't be able to expand it.
    – Kusalananda
    May 3 at 9:14










  • @Kiwy - There's nothing sensitive in my question.
    – Ross
    May 3 at 9:15










  • @Kusalananda - Can you give me an example of what I should be doing please?
    – Ross
    May 3 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 at 9:22












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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?







share|improve this question













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?









share|improve this question












share|improve this question




share|improve this question








edited May 3 at 9:18









Kusalananda

102k13199316




102k13199316









asked May 3 at 9:11









Ross

63




63











  • Well, the variable is within single quotes, so the shell won't be able to expand it.
    – Kusalananda
    May 3 at 9:14










  • @Kiwy - There's nothing sensitive in my question.
    – Ross
    May 3 at 9:15










  • @Kusalananda - Can you give me an example of what I should be doing please?
    – Ross
    May 3 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 at 9:22
















  • Well, the variable is within single quotes, so the shell won't be able to expand it.
    – Kusalananda
    May 3 at 9:14










  • @Kiwy - There's nothing sensitive in my question.
    – Ross
    May 3 at 9:15










  • @Kusalananda - Can you give me an example of what I should be doing please?
    – Ross
    May 3 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 at 9:22















Well, the variable is within single quotes, so the shell won't be able to expand it.
– Kusalananda
May 3 at 9:14




Well, the variable is within single quotes, so the shell won't be able to expand it.
– Kusalananda
May 3 at 9:14












@Kiwy - There's nothing sensitive in my question.
– Ross
May 3 at 9:15




@Kiwy - There's nothing sensitive in my question.
– Ross
May 3 at 9:15












@Kusalananda - Can you give me an example of what I should be doing please?
– Ross
May 3 at 9:16




@Kusalananda - Can you give me an example of what I should be doing please?
– Ross
May 3 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 at 9:22




@Kusalanada - SafetyCulture is not my company, anyone can use their API. I removed the authorization code and replaced with dashes already.
– Ross
May 3 at 9:22










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










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.






share|improve this answer























  • This doesn't work, the value i'm posting still doesn't become the name of the audit.
    – Ross
    May 3 at 9:27










  • Ok, so there's something I missing then. How are you setting the $title shell variable?
    – Kusalananda
    May 3 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 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 at 9:30











  • This still isn't working?
    – Ross
    May 3 at 9:33










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f441495%2fposting-a-php-variable-into-a-bash-script%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










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.






share|improve this answer























  • This doesn't work, the value i'm posting still doesn't become the name of the audit.
    – Ross
    May 3 at 9:27










  • Ok, so there's something I missing then. How are you setting the $title shell variable?
    – Kusalananda
    May 3 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 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 at 9:30











  • This still isn't working?
    – Ross
    May 3 at 9:33














up vote
3
down vote



accepted










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.






share|improve this answer























  • This doesn't work, the value i'm posting still doesn't become the name of the audit.
    – Ross
    May 3 at 9:27










  • Ok, so there's something I missing then. How are you setting the $title shell variable?
    – Kusalananda
    May 3 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 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 at 9:30











  • This still isn't working?
    – Ross
    May 3 at 9:33












up vote
3
down vote



accepted







up vote
3
down vote



accepted






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.






share|improve this answer















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.







share|improve this answer















share|improve this answer



share|improve this answer








edited May 3 at 10:33


























answered May 3 at 9:21









Kusalananda

102k13199316




102k13199316











  • This doesn't work, the value i'm posting still doesn't become the name of the audit.
    – Ross
    May 3 at 9:27










  • Ok, so there's something I missing then. How are you setting the $title shell variable?
    – Kusalananda
    May 3 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 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 at 9:30











  • This still isn't working?
    – Ross
    May 3 at 9:33
















  • This doesn't work, the value i'm posting still doesn't become the name of the audit.
    – Ross
    May 3 at 9:27










  • Ok, so there's something I missing then. How are you setting the $title shell variable?
    – Kusalananda
    May 3 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 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 at 9:30











  • This still isn't working?
    – Ross
    May 3 at 9:33















This doesn't work, the value i'm posting still doesn't become the name of the audit.
– Ross
May 3 at 9:27




This doesn't work, the value i'm posting still doesn't become the name of the audit.
– Ross
May 3 at 9:27












Ok, so there's something I missing then. How are you setting the $title shell variable?
– Kusalananda
May 3 at 9:28




Ok, so there's something I missing then. How are you setting the $title shell variable?
– Kusalananda
May 3 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 at 9:30





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 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 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 at 9:30













This still isn't working?
– Ross
May 3 at 9:33




This still isn't working?
– Ross
May 3 at 9:33












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f441495%2fposting-a-php-variable-into-a-bash-script%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?