使用 PHP shell_exec() 调用 sh 脚本时未设置变量
我目前有一个 sh 脚本:
location=$1
imageNumber=$2
keyword=$3
page=$4
imagesInPage=$5
imagesToGet=$6
imageUrls=`curl -s "http://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=[MYAPIKEY]&text=$keyword&per_page=$imagesInPage&media=photos&license=1,2,4,5,7&page=$page" | sed '1,3d' | sed 'N;$!P;$!D;$d' | ./xml_to_urls 2> /dev/nul`
currentImage=0
for url in $imageUrls
do
if [ $currentImage -ne $imagesToGet ]
then
curl -s "$url" > "$location/$imageNumber.jpg"
else
break
fi
currentImage=`expr $currentImage + 1`
imageNumber=`expr $imageNumber + 1`
done
...它基本上请求 Flickr API 提供与参数对应的图像列表。然后,我循环遍历图像 URL,将每个图像保存在指定目录中。
调用它的 PHP 脚本相当长,但这是调用它的行:
shell_exec("./get_images \"" . $dir . "\" " . $currentImageNumber . " \"" . $keyword . "\" " . $page . " 500 500");
当我在命令行上运行命令(例如:./get_images "temp/0.75456300 1297381201" 0 "moose" 1 500 500)时,没有问题发生。但是,当由 PHP 脚本运行时,变量 $imageUrls 永远不会设置。我可以回显我设置 imageUrls 的命令,并在 PHP 创建的页面上显示 URL,但据我所知,变量 self 是空白的。
我能得到的任何帮助都会很棒!请告诉我是否应该添加其他内容或者我的问题是否具有误导性,这是我的第一篇文章:D!谢谢!
I currently have a sh script:
location=$1
imageNumber=$2
keyword=$3
page=$4
imagesInPage=$5
imagesToGet=$6
imageUrls=`curl -s "http://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=[MYAPIKEY]&text=$keyword&per_page=$imagesInPage&media=photos&license=1,2,4,5,7&page=$page" | sed '1,3d' | sed 'N;$!P;$!D;$d' | ./xml_to_urls 2> /dev/nul`
currentImage=0
for url in $imageUrls
do
if [ $currentImage -ne $imagesToGet ]
then
curl -s "$url" > "$location/$imageNumber.jpg"
else
break
fi
currentImage=`expr $currentImage + 1`
imageNumber=`expr $imageNumber + 1`
done
... which basically requests the Flickr API for a list of images corresponding to the arguments. Then, I loop through the image URLs, saving each one in a specified directory.
The PHP script that calls this is quite long, but here's the line that calls it:
shell_exec("./get_images \"" . $dir . "\" " . $currentImageNumber . " \"" . $keyword . "\" " . $page . " 500 500");
When I run the command (ex: ./get_images "temp/0.75456300 1297381201" 0 "moose" 1 500 500) on the command line, no problems occur. However, when run by the PHP script, the variable $imageUrls is never set. I can echo out the command I am setting imageUrls to, and that displays the URLs on the page PHP creates, but as far as I can tell, the variable its self is blank.
Any help I could get on this would be great! Please tell me if I should add anything else or if my question is misleading, this is my first post :D! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在 imageList 的变量赋值的输出周围添加双引号,
我希望这会有所帮助。
PS,由于您似乎是新用户,如果您得到的答案对您有帮助,请记住将其标记为已接受,或给它一个+(或-)作为有用的答案
try adding dbl-quotes around the output of your variable assignment for imageList
I hope this helps.
P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, or give it a + (or -) as a useful answer