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

标签 php bash

所以,基本上,我想做的是执行这个 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

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

有什么帮助吗?

最佳答案

注意:我没有做所有的设置,我希望你能理解。

选项 1:传递参数

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

...

选项 2:使用环境变量

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

...

关于php - 将 PHP 变量传递给 Bash 脚本,然后启动它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13903506/

相关文章:

regex - 通过在特定位置添加前缀来编辑文件

linux - 比较两个数字,但如果条件不能正常工作

bash - 如何在 awk 脚本中使用 shell 变量?

php - MySQL DB 字段的多个数据

php - 具有 mySQL 数据库的多用户 PHP 应用程序 - 不同的用户相同的表?

php - count() 发出 E_WARNING

php - 搜索复杂且设计良好的 PHP OOP 应用程序以供学习

php - 自定义帖子类型元框单选按钮输出

linux - 如何更正浮点值范围?

ruby - Chef + rbenv : "rbenv: no such command ' install'"despite ruby-build being installed