开发者

Can I execute nested or chained commands in UNIX shell?

Can I execute command within another command in UNIX shells?

If impossible, can I use the output of the previous command as the input of next command, as in:

command x then command y,

where开发者_开发百科 in command y I want use the output of command x?


You can use the backquotes for this.

For example this will cat the file.txt

cat `echo file.txt`

And this will print the date

echo the date is `date`

The code between back-quotes will be executed and be replaced by its result.


You can do something like;

x=$(grep $(dirname "$path") file)

here dirname "$path" will run first and its result will be substituted and then grep will run, searching for the result of dirname in the file


What exactly are you trying to do? It's not clear from the commands you are executing. Perhaps if you describe what you're looking for we can point you in the right direction. If you want to execute a command over a range of file (or directory) names returned by the "find" command, Colin is correct, you need to look at the "-exec" option of "find". If you're looking to execute a command over a bunch of arguments listed in a file or coming from stdin, you need to check out the "xargs" commands. If you want to put the output of a single command on to the command line of another command, then using "$(command)" (or 'command' [replace the ' with a backquote]) will do the job. There's a lot of ways to do this, but without knowing what it is you're trying it's hard to be more helpful.


Here is an example where I have used nested system commands. I had run "ls -ltr" on top of find command. And it executes it serially on the find output.

ls -ltr $(find . -name "srvm.jar")

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜