开发者

Running command using XARG

I have example开发者_StackOverflow中文版 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


Something like this perhaps?

for cmd in $(grep RUN_THIS < test_file); do
    $cmd --some-option-to-be-provided-to-run-this
done

That should work okay as long as there are no spaces in the commands in test_file.


eval `grep RUN_THIS test_file`

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 the grep to something which adds the necessary semicolons.

eval `awk '/RUN_THIS/ { print; print ";" }'`

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.


To run new shells you need to incorparate "ksh" in your command.

In its simplest form

RUN_THIS00='ls'
echo $RUN_THIS00 | ksh
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜