douhuigan8063 2013-01-10 14:11
浏览 92

将bash换行符转换为HTML换行符; 在BASH中运行良好,但不是通过shell_exec运行

I've created a web page that converts bash line breaks [ ] to HTML line breaks [< br />]. This works by having the web page call

shell_exec("bash /xxx/xxx/script.sh");

Inside the bash script.sh, it runs a series of SQL queries to replace the bash line breaks with html line breaks, allowing me to format the SQL data into an HTML based email.

`mysql -uXXX -pXXX -e "UPDATE tbl SET var = REPLACE(var, '
', '<br />')" db`

When I run this bash script via Linux cli, it runs perfect, and the email it sends out uses HTML line breaks. But when I run the script via shell_exec, it just shows in plain text, the bash line break code [ ].

Here is a sample of the email output when running the bash script with PHP shell_exec.

[ var1 ]
test test test
test test test
 test some more
[ var2 ]
test test
test
test

And a sample when I run the bash script from linux.

[ var1 ]
test test test
test test test
test some more
[ var2 ]
test test
test
test

I know my other SQL queries work such as SELECT MAX(id) [grabbing the appropriate row and data fields]. Any idea as to why the same script runs perfectly fine under linux bash (to run SQL query, replacing line breaks), but not by calling it with PHP shell_exec? Am I missing something here?

* httpd error log states: sudo: sorry, you must have a tty to run sudo

and when I enable: sudo: no tty present and no askpass program specified

Could this be a TTY issue since it's a command/script being called by user apache? If so, any work arounds?

  • 写回答

1条回答 默认 最新

  • douxun4924 2013-01-17 19:13
    关注

    I think what happens is that currently it's replacing the ' ' char, when you would like to escape the '\' char AND 'n' char. Try to change your request like this:

    `mysql -uXXX -pXXX -e "UPDATE tbl SET var = REPLACE(var, '\
    ', '<br />')" db`
    
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题