0

我正在使用 ash 而不是 bash 开发嵌入式系统,并试图从交叉编译的 C 程序中调用脚本。

它正在工作,但不像我问的那样在后台进程中。事实上,似乎 strcat 命令不起作用,但它打败了我为什么。

我在 C 方面相当新,这一定是显而易见的。

编码:

char mycall[230] = "/home/root/myscript.sh ";
char inbackground[3]= " &";


// buf is initiated and will be used as a parameter, string length is about 20 characters
strcat(buf,inbackground);
strcat(mycall,buf);

printf("%s", mycall);   // this will display the command and parameter that was stored with 
   // in buf correctly, but without the ampersand at the end
system(mycall); // executes correctly

屏幕显示:

sh: syntax error: "&" unexpected

为什么要这样做?

4

1 回答 1

1

我的猜测是最后buf包含一个换行符 ( '\n') 字符。也许其他一些不可显示的字符。

你需要过滤掉那些不需要的东西。

由于我们没有确切地显示如何buf设置(或 ti 的类型是什么),因此 1buf1 指向的任何内容也可能没有足够的空间容纳其他字符。您可能希望像这样重新调整您的连接以避免修改buf

strcat(mycall,buf);
strcat(mycall,inbackground);
于 2010-12-06T15:57:10.773 回答