清空一个文件的常用方法是
cat /dev/null > file
但是可否用 cat file < /dev/null 清空文件呢?
事实上不能,为什么呢?因为当cat命令找到文件名后会忽略后面的标准输入。只有当找不到文件名的时候,cat 命令才会去标准输入里面去找
Standard input (stdin) normally comes from your keyboard. Many programs ignore stdin; you name files directly on their command line -- for instance, the command cat file1 file2 never reads its standard input; it reads the files directly. But, without filenames on the command line, UNIX commands that need input will usually read stdin. Standard input normally comes from your keyboard, but the shell can redirect stdin from a file.
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/13633513/viewspace-706405/ ,如需转载,请注明出处,否则将追究法律责任。