Posting a PHP variable into a bash script
Clash 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?
bash scripting php
add a comment |Â
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?
bash scripting php
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
add a comment |Â
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?
bash scripting php
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?
bash scripting php
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
add a comment |Â
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
add a comment |Â
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.
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
 |Â
show 4 more comments
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.
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
 |Â
show 4 more comments
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.
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
 |Â
show 4 more comments
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.
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.
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
 |Â
show 4 more comments
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
 |Â
show 4 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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