Answers:
touch:while read line; do touch "$line.txt"; done <in
while read line; [...]; done <in:这会read一直运行直到read自身返回1,这在到达文件末尾时发生;输入read是从in当前工作目录中命名的文件而不是由于<in重定向而从终端读取的;touch "$line.txt":这touch基于的扩展值$line.txt,line其后是.txt; 的内容;touch如果不存在则创建文件,如果存在则更新访问时间;xargs+ touch:xargs -a in -I name touch name.txt
-a in:xargs从in当前工作目录中命名的文件中读取其输入;-I name:在以下命令中用当前输入行xargs替换每次name出现的;touch name:touch以的替换值运行name; 如果不存在,它将创建文件,如果存在,它将更新其访问时间;% ls
in
% cat in
john
george
james
stewert
% while read line; do touch "$line.txt"; done <in
% ls
george.txt in james.txt john.txt stewert.txt
% rm *.txt
% xargs -a in -I name touch name.txt
% ls
george.txt in james.txt john.txt stewert.txt
read line实际将如何读取该行?@kos
<in后done品牌read在while循环条件读取和存储从一条线in中line,在每一次迭代; 这样,touch "$line.txt"循环内部将扩展到最后的读取行.txt。
while read line; do touch "$line.txt"; done <in做吗?源文件在哪里?
read。请参阅help read。
在这种特殊情况下,每行只有一个单词,您还可以执行以下操作:
xargs touch < file
请注意,如果您的文件名可以包含空格,则此操作将中断。在这种情况下,请改用以下方法:
xargs -I {} touch {} < file
只是为了好玩,这里还有其他几种方法(这两种方法都可以处理任意文件名,包括带有空格的行):
佩尔
perl -ne '`touch "$_"`' fileAwk
awk '{printf "" > $0}' file 请注意,在Linux和类似系统上,该扩展名对于绝大多数文件是可选的。没有理由在.txt文本文件中添加扩展名。您可以这样做,但是完全没有区别。因此,如果仍要扩展,请使用以下方法之一:
xargs -I {} touch {}.txt < file
perl -ne '`touch "$_.txt"`' file
awk '{printf "" > $0".txt"}' file
AWK也适合此任务:
testerdir:$ awk '{system("touch "$0)}' filelist
testerdir:$ ls
filelist george james john stewert
testerdir:$ awk '{system("touch "$0".txt")}' filelist
testerdir:$ ls
filelist george.txt james.txt john.txt stewert.txt
george james john stewert
另一种方式tee。请注意,如果一行在文件列表中包含多个字符串,则此方法将中断。
testerdir:$ echo "" | tee $(cat filelist)
testerdir:$ ls
filelist george james john stewert
或者,</dev/null tee $(cat filelist)如果您要避免管道运输,也可以完成
cp /dev/null 方法(正如我演示的那样,它确实适用于包含空格的文件名):
testerdir:$ cat filelist | xargs -I {} cp /dev/null "{}"
testerdir:$ ls
filelist FILE WITH SPACES george james john stewert
testerdir:$ ls FILE\ WITH\ SPACES
FILE WITH SPACES
echo "" | tee file1 file2 file2。但是,如果文件名包含空格,则确实会中断。