使用 XARG 运行命令
我有示例文件: test_file
//--- Test File--
**RUN_THIS
RUN_THIS00
RUN_THIS01
DONT_RUN00
DONT_RUN00
RUN_THIS02**
其中 RUN_THIS*
& DONT_RUN*
是命令。 我想只从 test_file 运行 RUN_THIS 命令而不编辑文件。 我正在寻找类似
cat test_file | grep RUN_THIS | xargs {Some option to be provided to run run_this}
我无法启动新外壳的选项
I have example file: test_file
//--- Test File--
**RUN_THIS
RUN_THIS00
RUN_THIS01
DONT_RUN00
DONT_RUN00
RUN_THIS02**
where RUN_THIS*
& DONT_RUN*
are commands.
I would like to run only RUN_THIS commands from test_file without editing the file.
I am looking for option like
cat test_file | grep RUN_THIS | xargs {Some option to be provided to run run_this}
I cannot start new shell
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许是这样的?
只要
test_file
中的命令中没有空格,就应该可以正常工作。Something like this perhaps?
That should work okay as long as there are no spaces in the commands in
test_file
.另请注意避免无用地使用 Cat。
实际上,您可能需要在
test_file
中每个命令的末尾添加一个分号,或者将grep
更改为添加必要分号的内容。我不确定我是否理解不启动新外壳的要求。在底层,反引号运行一个子 shell,因此这可能违反该要求(但最终每个外部命令都会启动一个新进程,当您运行 shell 脚本时,该进程将作为当前 shell 进程的分支启动)。如果您担心安全隐患,那么无论如何,您一开始就不应该使用 shell 脚本。
Note also the avoidance of a Useless Use of Cat.
Actually, you may have to add a semicolon to the end of each command in
test_file
, or change thegrep
to something which adds the necessary semicolons.I'm not sure I understand the requirement to not start a new shell. Under the hood, the backticks run a subshell, so this might violate that requirement (but then ultimately every external command starts a new process, which starts out as a fork of the current shell process when you run a shell script). If you are scared of security implications, you should not be using a shell script in the first place, anyhow.
要运行新的 shell,您需要在命令中加入“ksh”。
最简单的形式
To run new shells you need to incorparate "ksh" in your command.
In its simplest form