2013-10-11 37 views
0

我想了解IFS如何與我編寫的簡單代碼一起工作。但這不起作用,任何人都可以解釋爲什麼這不起作用?通過unix中的system()設置IFS

#include <stdio.h> 
void main() 
{ 
    system("export IFS='/'; /bin/date"); 
} 

根據上面的代碼,該命令「/ bin中/日期」應該被分成2級的命令,如「倉」和「日期」,但是這不會發生。

回答

0

IFS用於擴展後的分詞。這裏沒有擴張,因此IFS不相關。通過「系統」做這件事毫無意義,因爲這只是混淆了這個問題。考慮下面的外殼:

$ k=/bin/date 
$ /bin/date # execute /bin/date 
$ $k # expand k, perform word-splitting (nothing happens), run /bin/date 
$ IFS=/ 
$ /bin/date # execute /bin/date 
$ $k # expand k, perform word-splitting to get 3 words: "", "bin", "date" 
     # attempt (and fail) to invoke a command with the name "" and arguments 
     # "bin", "date"