简体   繁体   中英

Calling Bash script from PHP not outputting correct values

I am attempting to call a bash script from PHP. When I run this bash script on my text file from the command line, I get the desired output. However, through PHP, it simply outputs nothing.
The following is my PHP code.

$output = shell_exec("path/to/script.sh tmp/file.txt");
echo $output;

Where path/to/script is, as it sounds, the path to the bash script, and tmp/file.txt is the argument for the script. The output of this is blank, yet when I run the bash script with the same input in the command line, it executes properly. Why is this?

This is hard to answer without seeing script.sh however I can see that shell_exec outputs null when it fails.

You could try using the exec() function to review the exit code of the script. php manual exec()

so the code would be:

exec("path/to/script.sh tmp/file.txt", $output);
echo $output;

You might find that it's a permission issue stopping the process from making changes to it.

try changing the file permissions with chmod 777 tmp/file.txt

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