Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass PHP variables to a Bash script and then launch it

Tags:

bash

php

So, basically, what I want to do is execute this Bash script :

#!/bin/bash

path="./"
filename="Ellly.blend"
file=${path}${filename}
description="Test of the api with a simple model"
token_api="ff00ff"
title="Uber Glasses"
tags="test collada glasses"
private=1
password="Tr0b4dor&3"

curl -k -X POST -F "fileModel=@${file}" -F "filenameModel=${filename}" -F "title=${title}" -F "description=${description}" -F "tags=${tags}" -F "private=${private}" -F "password=${password}" -F "token=${token_api}" https://api.sketchfab.com/v1/models

Inside a PHP function. Problem is, I don't really see how I can pass to it some variables and then wait for it to end before resuming the PHP function.

Any help ?

like image 254
Axiol Avatar asked Dec 16 '12 17:12

Axiol


People also ask

How do I run a PHP file in bash?

Show activity on this post. Put that at the top of your script, make it executable ( chmod +x myscript. php ), and make a Cron job to execute that script (same way you'd execute a bash script). You can also use php myscript.

How do you pass variables in a script?

Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth. The variable $0 references to the current script.

Can you pass PHP variable to JavaScript?

We can pass data from PHP to JavaScript in two ways depending on the situation. First, we can pass the data using the simple assignment operator if we want to perform the operation on the same page. Else we can pass data from PHP to JavaScript using Cookies. Cookie work in client-side.


Video Answer


1 Answers

Note: I didn't do all the settings, just enough I hope you get the idea.

Option 1: Passing parameters

PHP:

$file = escapeshellarg($file);
$filename = escapeshellarg($filename);
// escape the others
$output = exec("./bashscript $file $filename $tags $private $password");

Bash:

#!/bin/bash
filename=$1
file=$2
description="Test of the api with a simple model"
token_api="ff00ff"
title="Uber Glasses"
tags=$3
private=$4
password=$5

...

Option 2: Using environment variables

PHP:

putenv("FILENAME=$filename");
putenv("FILE=$file");
putenv("TAGS=$tags");
putenv("PRIVATE=$private");
putenv("PASSWORD=$PASSWORD");

$output = exec('./bash_script');

Bash:

filename=$FILENAME
file=$FILE
description="Test of the api with a simple model"
token_api="ff00ff"
title="Uber Glasses"
tags=$TAGS
private=$PRIVATE
password=$PASSWORD

...
like image 102
Levi Morrison Avatar answered Sep 20 '22 14:09

Levi Morrison