简体   繁体   中英

How to push text into newly created file by shell script from PHP?

i used PHP site to run a shell script and it created my desired name file "result.txt" inside /opt/xampp/htdocs. However, the result.txt is totally empty, when it is supposed to have output values from the shell script. Tried on terminal, result.txt is created on Desktop and outputs are there. Tried on PHP, result.txt is created but totally no outputs are inside.

It's a long script so here's the first few lines:

# creates result.txt
touch result.txt

##makes the .txt read only
chmod 444 result.txt 


clear


###################################################
# This option audits the password and             #
# shadow files on the system                      #
###################################################

##option 2.1.1
echo -e "=========================================================================================" >> result.txt
echo "                                  SHADOWED PASSWORD CHECK                                   " >> result.txt
echo "=========================================================================================" >> result.txt

Anyone?

Thanks.

Firstly, it is completely unnecessary to redirect the output of every echo call - use this at the start of your shell script to have STDOUT redirected to your output file.

exec &> results.txt

Secondly - your chmod to 444 will not leave the file writeable by the Apache/PHP user - I'd perform this at the very end of your script, if even needed at all.

Try changing working directory first, using chdir($folder).

<?php
chdir("/testfolder/");
exec("./script.sh");
?>

But, why not write the script as php? Things like touch, chmod and every other command available in the unix shell is easily available in php.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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