试图通过我的PHP脚本在我的apache服务器中执行bash命令,但是收到错误
问题描述:
i have just started implemting bash commands into my php scripts the first few works fine now i am trying to do the following bash command in my php script
exec("awk -F: '{printf "www.example.com/2/tks.php?account=%s%%3A%s
" ,$1, $2 }' < /opt/lampp/htdocs/$filename > /opt/lampp/htdocs/2.txt");
the error i am getting from my php script that worked fine before i put this in is
Parse error: syntax error, unexpected 'www' (T_STRING) in /opt/lampp/htdocs/index.php on line 54
答
You have to escape your double quotes in the string:
exec("awk -F: '{printf \"www.example.com/2/tasks.php?account=%s%%3A%s\
\" ,$1, $2 }' < /opt/lampp/htdocs/$filename > /opt/lampp/htdocs/2.txt");
答
The error is because you've not escaped the double quotes for your printf
statement inside the exec
statement.
In it's current state, PHP treats "awk -F: '{printf "
as the first part of the string since they're enclosed in double quotes. To tell PHP to ignore the other quotes, you can use a backslash.
Change it to:
exec("awk -F: '{printf \"www.example.com/2/tks.php?account=%s%%3A%s
\" ,$1, $2 }' < /opt/lampp/htdocs/$filename > /opt/lampp/htdocs/2.txt");