0

I have a PHP project that runs a bash code under php file.

When I try to run this command under bash, everything works ok. But in my PHP code, it gives me a syntax error for some reason.

PHP Code I am having issue with;

exec('comm -13 <(sort ' . $path_d_raw.$least_recent_raw_file . ') <(sort ' . $path_d_raw.$most_recent_raw_file . ') > test.txt 2>&1', $output, $return);

Error output

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `comm -13 <(sort /path/to/file/file_01.ext) <(sort /path/to/file/file_02.ext) > test.txt 2>&1'

But when I try to run this command under terminal, it works;

comm -13 <(sort /path/to/file/file_01.ext) <(sort /path/to/file/file_02.ext) > test.txt 2>&1

I think there are problems with ` or ' but couldn't get rid of them.

Thanks in advance for any suggestion.

Pelin
  • 467
  • 4
  • 17
  • @MasivuyeCokile Hi Masivuye, thanks for link. I checked it out but I wasn't able to find a topic related to my issue. PHP code works ok, but I still couldnt find out why `exec` troubles me. When I tried `shell_exec` with adding `bash` command, error is gone but I dont get any output. :/ – Pelin Sep 30 '19 at 14:19
  • Try: save the command to a variable. Echo/var_dump/log the variable to make sure it all looks correct, then try passing the variable into the exec command. This will allow you to see exactly what is being passed, and copy/paste the exact command into the command line to check for syntax errors. – aynber Sep 30 '19 at 14:19
  • Hi @aynber I just tried it a minute ago but unfortunately I am getting same error. `sh: -c: line 0: syntax error near unexpected token `('` – Pelin Sep 30 '19 at 14:20
  • 1
    The error message is telling you that **sh** is having a syntax error: you are trying to use bash-specific syntax with plain sh. – glenn jackman Sep 30 '19 at 14:21
  • I usually put the command in a file, and call the file with exec (with bash). You could give that a try. – Erwin Moller Sep 30 '19 at 14:21
  • @glennjackman Thank you Glenn, Yes looks like this is the issue. What would you suggest me to overcome this issue using the easiest way? I tried creating a different file that contains the command but then I wasnt able to pass arguments into this command – Pelin Sep 30 '19 at 14:26

1 Answers1

1

I don't really know PHP (we're in quoting hell territory here: can a single quoted string contain escaped single quotes? Is there another quoting mechanism, like triple quotes?), but something like:

exec('bash -c \'comm -13 <(sort "' . $path_d_raw.$least_recent_raw_file . '") <(sort "' . $path_d_raw.$most_recent_raw_file . '") > test.txt 2>&1\'', $output, $return);
# ....^^^^.^^.^^................^..........................................^.........^.........................................^.................^^

I've also added double quotes around the filenames, to protect you from filenames that may contain whitespace.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Thank you very much for detailed answer! I don't get any error now, but interestingly, it doesn't compare the files even with the paths are ok. When I hardcode paths, it works aswell. I'll check whats wrong and mark this one as answer. Thanks! – Pelin Sep 30 '19 at 15:07
  • It will be helpful for future readers of your question if you provide your own answer to show exactly what you did to solve it. Mark your own answer as accepted if you want. – glenn jackman Sep 30 '19 at 21:19
  • The files were same, pity me, that was the issue of empty files :) Other than that your answer is the exact solution of mine. `bash -c` solved my issue since I think some part of this command didn't work well with `sh` – Pelin Oct 01 '19 at 07:22