11

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?

Kheldar
  • 5,361
  • 3
  • 34
  • 63
Osama Ahmad
  • 805
  • 4
  • 12
  • 18
  • 1
    @Mohammad: I removed the svn tag since this has nothing to do with svn, it is a general command piping question. – Albin Sunnanbo Sep 19 '10 at 14:48
  • 1
    I think you need to describe the bigger problem that you're having. What *exactly* are you trying to accomplish? – Cristian Ciupitu Sep 19 '10 at 15:51
  • 1. i want find all repository .... 2. i want find entire directory for all repository .... note : the result of execute step one more than 500 repositoy so i need method to execute step two by easy method,,,, – Osama Ahmad Sep 19 '10 at 20:10
  • i execute the following command :-find / -type f -name fs-type -exec svnlook tree {} \; |egrep "/$" ... the result is svnlook: Can't open file '/var/lib/svn/repos/b1me/products/payone/generic/code/core/db/fs-type/format': Not a directory svnlook: Can't open file '/var/lib/svn/repos/b1me/products/payone/generic/code/fees/db/fs-type/format': Not a directory ..... maybe we must make find command give us path without "db/fs-type/format" but how i can do this ??? – Osama Ahmad Sep 20 '10 at 06:40

4 Answers4

21

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.

Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
  • please you can give me example in more detail – Osama Ahmad Sep 19 '10 at 14:43
  • but please in the following case what is the solution : i execute "find / -type f -name fs-type -print " this commad found all repository ... now i need to find all directory of repository by this command "svnlook tree /var/lib/lib.org.15-01-2008/svn/repos/pstest | egrep "/$ – Osama Ahmad Sep 19 '10 at 14:57
  • Then you need to look at the `-exec` option of find – Colin Hebert Sep 19 '10 at 15:21
  • is it possible to nest backquotes? I'm trying to use the result of this expression in a command: realpath --relative-to=`pwd` `git top pwd` (where git top is an alias that executes at the top of the repository. the idea is to put that in a find command, which will give me relative paths to the results, for use in a selecta fuzzy selection vim binding. – TamaMcGlinn Oct 23 '16 at 05:15
  • googlefoo evidently failed me: here it is http://www.linuxmisc.com/12-unix-shell/d9c594a376ffed37.htm - $ FOO=` echo \\` echo hello \\` ` - i.e. escape the inner ones – TamaMcGlinn Oct 23 '16 at 05:16
15

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

codaddict
  • 445,704
  • 82
  • 492
  • 529
  • but please in the following case what is the solution : i execute "find / -type f -name fs-type -print " this commad found all repository ... now i need to find all directory of repository by this command "svnlook tree /var/lib/lib.org.15-01-2008/svn/repos/pstest | egrep "/$ ..... how i can solve it – Osama Ahmad Sep 19 '10 at 14:58
  • 2
    @Mohammed: Add that to your main question. – Donal Fellows Sep 19 '10 at 15:17
  • i want to find all my repository and then find its entire content – Osama Ahmad Sep 20 '10 at 05:52
4

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.

Jim Nutt
  • 1,736
  • 12
  • 10
-1

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")