简体   繁体   中英

Calling a bash script from PHP to generate another script

I am writing an application which takes text input from the user to generate a text file. A bash script, which takes the generated text file as input should generate another script as output.

I tried using exec command but I am not sure if it works. I want something like this:

exec('generate.sh input.txt generated.sh');

generate.sh takes two inputs:

  1. input.txt file - has user input from PHP page(text separated by '\\n')
  2. generated.sh - name of the generated file(to be generated by 'generate.sh').

How can I execute the bash script from PHP to get the above output?

Thanks in advance.

exec的整个参数必须是这样的字符串:

exec('generate.sh input.txt generated.sh');
$generate = '/path/to/generate.sh';
$input = '/path/to/input.txt';
$generated = '/path/to/generated.sh';

if(file_exists($generate))
{
    chmod($generate, 0755);
    if(file_exists($input))
    {
        exec($generate.' '.$input.' '.$generated);
    }
    else
    {
        echo "Input file not found: ".$input."<br />";
    }
}
else
{
    echo "Generate file not found: ".$generate."<br />";
}

Furthermore you can also consider system(), shell_exec(), passthru() or proc_open() to execute external commands.

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