0

I'm working on interfacing a microcontroller with a lamp server. I am trying to run the command echo -e -n "data \r" > /dev/ttyUSB0 using shell_exec in php but with no results. It works just fine from the command line. Doing a little experimenting, I discovered that echo -e -n "1 \r" actually echoes -e -n 1. Is there a reason it won't take the -e or -n options? Here's my code:

    <?php
    shell_exec('echo -e -n "1 \r" > /dev/ttyUSB0');
    ?>
2
  • Why not use the PHP file functions? Commented Mar 16, 2012 at 3:53
  • I did not think of that. Could you give an example? Commented Mar 16, 2012 at 3:56

5 Answers 5

1

Instead of using shell_exec and echo, why not use PHP's filesystem functions?

file_put_contents('/dev/ttyUSB0', "1 \r");
Sign up to request clarification or add additional context in comments.

Comments

1

There are some other functions too, try this function maybe you get your answer.

exec(command, $output);

This function takes a command and assigns to $output an array where each element is a line of the generated output.

1 Comment

I tried this and I still get the same -e -n 1. I need echo to use the -e and -n options
0

I ran into similar problem, calling from php

php > echo shell_exec("echo -e aaa\tbbb");
-e aaa bbb

note, that output contains "-e", while I have expected that 'echo' command will interpret -e as flag and would not send it to output.

After doing some investigation I came to following conclusion:

  • when exec or shell_exec are called from php - new shell interpreter is launched.
    • php launches "sh".
    • When I was running on CentOS sh was a symlink to bash, and exec("echo ...") worked as I would have expected.
    • Now I am running Ubuntu. sh is a symlink to dash, not bash!
    • And final root cause - builtin echo command in dash does not have/understand '-e' flag, so it just forwards it to output

Comments

0

This is an old question but it goes what worked for me in case someone else comes across with this as well.

Yesterday night I've messing around and struggling with this myself. For this to work, the command you need to use should start by calling the echo bin directly /bin/echo... instead of only echo.

Also don't forget to use single quote /bin/echo... instead of double quote to avoid PHP null byte detection error (this part you did correctly).

Comments

0

I had a similar problem running with -e, I was trying to change the password from a php

running this didnt work

exec("echo -e \"$pass\\n$pass\" | passwd $user");

It said passwords dont match. Checking the echo i saw that -e was includes as a part of the echoing so i changed it to

exec("echo \"$pass\\n$pass\" | passwd $user");

And then it worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.