简体   繁体   中英

Pass PHP variables to a Bash script and then launch it

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?

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

...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM