你如何运行一个文件的每一行命令? [英] How do you run a command for each line of a file?

查看:111
本文介绍了你如何运行一个文件的每一行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,现在我用下面的方法修改几个文件,其路径的Unix我写的一个文件:

For example, right now I'm using the following to change a couple of files whose Unix paths I wrote to a file:

猫file.txt的|而阅读;做搭配chmod$的755;做

有没有更优雅,更安全的方式?

Is there a more elegant, safer way?

推荐答案

如果您的文件不是太大,所有文件都的以及名为的(不含空格或其他特殊字符像引号),你可以简单地说:

If your file is not too big and all files are well named (without spaces or other special chars like quotes), you could simply:

chmod 755 $(<file.txt)

如果您有特殊字符和/或在很多线路 file.txt的

If you have special chars and/or a lot of lines in file.txt.

xargs -0 chmod 755 < <(tr \\n \\0 <file.txt)

如果您的命令需要通过条目刚好运行1次:

if your command need to be run exactly 1 time by entry:

xargs -0 -n 1 chmod 755 < <(tr \\n \\0 <file.txt)

这是不需要这个样本,因为搭配chmod 接受多个文件作为参数,但这场比赛问题的标题。

This is not needed for this sample, as chmod accept multiple files as argument, but this match the title of question.

对于一些特殊情况下,可以甚至的xargs 定义命令generateds文件参数的位置:

For some special case, you could even define location of file argument in commands generateds by xargs:

xargs -0 -I '{}' -n 1 myWrapper -arg1 -file='{}' wrapCmd < <(tr \\n \\0 <file.txt)

这篇关于你如何运行一个文件的每一行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆