0

我试图了解 IFS 如何使用我编写的这个简单代码。但这不起作用,有人能解释为什么这不起作用吗?

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

根据上面的代码,命令 '/bin/date' 应该被分成 2 个命令,如 'bin' 和 'date',但这并没有发生。

4

1 回答 1

0

IFS 用于扩展后的分词。这里没有发生扩展,所以 IFS 不相关。通过“系统”执行此操作毫无意义,因为这只是混淆了问题。从 shell 中考虑以下内容:

$ 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"
于 2013-10-13T13:13:25.943 回答