2010-12-06 28 views
0

我正在使用ash而不是bash的嵌入式系統上工作,並試圖從交叉編譯的C程序中調用腳本。用作bash系統調用的strcat結果不喜歡&符號

它正在工作,但不是像我在問它的​​後臺進程。事實上,它似乎是一個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 

爲什麼這樣做呢?

+0

如果`buf`是「大約20個字符」這已經是太短了! – Clifford 2010-12-06 19:52:25

回答

1

我的猜測是buf在最後包含換行符('\n')。也許還有其他一些不可顯示的字符。

您需要將不需要的東西過濾掉。

由於我們沒有精確地顯示如何設置buf(或什麼樣的類型),所以無論1buf1指向什麼都沒有足夠的空間用於附加字符,也是可能的。你可能想rejigger像這樣的級聯,以避免修改buf

strcat(mycall,buf); 
strcat(mycall,inbackground);