2013-07-25 88 views
0

好了,所以我的繼承人bash命令我想給一個變量發送到如何傳遞從我的PHP腳本變量,並將其發送到我的bash腳本

perl -pi -e 's/ : /:/g' /opt/lampp/htdocs/"variable needs to go here" 

的話,我有我的變量在PHP至極是

$filename 

如何編輯我的bash腳本接受變量 以及如何傳遞變量和執行從PHP bash的任何幫助,將不勝感激。 我只是不知道我會如何讓bash準備好接受變量以及如何讓php發送它並執行腳本

+0

你使用bash或Perl? – jh314

回答

1

在PHP中使用的exec()函數調用bash腳本是這樣的:

$filename = escapeshellarg($filename); 
exec("perl -pi -e 's/ : /:/g' /opt/lampp/htdocs/".$filename); 
+0

我使用了它,它完美地工作謝謝 –

0

嘗試在您的php腳本中使用shell_exec來執行您的shell腳本並傳遞您的變量所以:

$cmd="perl -pi -e 's/ : /:/g' /opt/lampp/htdocs/" . escapeshellarg($variable); 
$r=shell_exec($cmd); 

escapeshellarg用來逃避$變量的任何潛在危險的人物,以防止命令行注入攻擊。

+0

我已經有一個bash腳本中的perl基本上需要將變量傳遞給/opt/lampp/htdocs/bash.sh,然後執行它我將如何做 –

0

還有另一種解決方案,不那麼優雅,但它的工作原理。
您可以從php腳本中回顯值,並將其捕獲到bash腳本中的變量中(使用反引號)。
不要忘記首先禁用php腳本中的錯誤輸出,以消除輸出帶有警告,警告的危險...

例如:

<?php 
ini_set("display_errors", "Off"); 
ini_set("log_errors", "On"); 
$filename = 'someValue'; 
echo $filename; 
?> 


#!/bin/bash 
#execute the php file 
FILENAME=`php file.php` 
perl -pi -e 's/ : /:/g' /opt/lampp/htdocs/$FILENAME