简体   繁体   English

在 PHP 中执行多行 shell 命令

[英]execute a multi line shell comand in PHP

I'm trying to get user email of current user using command line.我正在尝试使用命令行获取当前用户的用户 email。 In Powershell i paste these lines and it works在 Powershell 我粘贴这些行并且它工作

>> $searcher = [ADSISearcher] "(sAMAccountName=$env:USERNAME)"
>> $searcher.PropertiesToLoad.AddRange(@("mail"))
>> $searcher.FindOne().Properties["mail"][0]
test@gmail.com

but when i try to do this in php with exec or shell_exec但是当我尝试在 php 中使用 exec 或 shell_exec 执行此操作时

$cmd=<<<CMD
$searcher = [ADSISearcher] "(sAMAccountName=$env:USERNAME)" \
$searcher.PropertiesToLoad.AddRange(@("mail")) \
$searcher.FindOne().Properties["mail"][0]
CMD;

print_r(shell_exec($cmd));

it shows me errors undefined variable searcher它向我显示错误未定义的变量搜索器

You want to use nowdoc instead of heredoc .您想使用nowdoc而不是heredoc Just enclose CMD in single quotes and PHP will not try to expand your $searcher as a PHP variable.只需将CMD括在单引号中, PHP 就不会尝试将您的$searcher扩展为 PHP 变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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