将 PHP 变量传递给 Bash 脚本,然后启动它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13903506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:14:10  来源:igfitidea点击:

Pass PHP variables to a Bash script and then launch it

phpbash

提问by Axiol

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

所以,基本上,我想做的是执行这个 Bash 脚本:

#!/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.

在 PHP 函数中。问题是,我真的不知道如何将一些变量传递给它,然后在恢复 PHP 函数之前等待它结束。

Any help ?

有什么帮助吗?

回答by Levi Morrison

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

注意:我没有做所有的设置,就足够了,我希望你能明白。

Option 1: Passing parameters

选项 1:传递参数

PHP:

PHP:

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

Bash:

重击:

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

...


Option 2: Using environment variables

选项 2:使用环境变量

PHP:

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

...