Linux command output as a parameter of another command
This is what you want:
find . -name myFile -exec cp /home/myuser/myFile {} ';'
A breakdown / explanation of this:
find
: invoking the find command.
: start search from current working directory.- Since no depth flags are specified, this will search recursively for all subfolders
-name myFile
: find files with the explicit namemyFile
-exec
: for the search results, perform additional commands with themcp /home/myuser/myFile {}
: copies/home/myuser/myFile
to overwrite each result returned byfind
to ; think of{}
as where each search result goes.';'
: used to separate different commands to be run afterfind
How to pass command output as multiple arguments to another command
You can use xargs
:
grep 'pattern' input | xargs -I% cp "%" "%.bac"
Use output of a command for another
Shell
Use $(...)
to insert the stdout of one command into another as a string:
setsid "$(find ./.stack-work/install -name nonosolver-exe)" >/dev/null 2>&1 < /dev/null &
This will cause unexpected problems if you end up finding a number of matches other than one. You can store into a variable to do error checking first:
exe_path="$(find ./.stack-work/install -name nonosolver-exe)"
# check non-empty and no newlines
setsid "${exe_path}" >/dev/null 2>&1 < /dev/null &
Makefile
The makefile equivalent to the bash $(...)
construct is $(shell ...)
. And variables are expanded with $(...)
, not ${...}
:
setsid "$(shell find ./.stack-work/install -name nonosolver-exe)" >/dev/null 2>&1 < /dev/null
Or
EXE_PATH = $(shell find ./.stack-work/install -name nonosolver-exe)
setsid "$(EXE_PATH)" >/dev/null 2>&1 < /dev/null
how do I pass the output of a bash command as parameters to another?
To pass the output of,a program to another you can use xargs, for example
find . -name "myfile*" -print | xargs grep "myword" $1
This one searches for files called myfile* and look for a key inside of them.
Use output of bash command (with pipe) as a parameter for another command
You can do this without resorting to sed with the help of Bash variable mangling, although as @ruakh points out this won't work in the single line version (without the semicolon separating the commands). I'm leaving this first approach up because I think it's interesting that it doesn't work in a single line:
TTY=$(tty); who | grep "${TTY#/dev/}"
This first puts the output of tty
into a variable, then erases the leading /dev/
on grep's use of it. But without the semicolon TTY
is not in the environment by the moment bash does the variable expansion/mangling for grep.
Here's a version that does work because it spawns a subshell with the already modified environment (that has TTY
):
TTY=$(tty) WHOLINE=$(who | grep "${TTY#/dev/}")
The result is left in $WHOLINE
.
How to pass output as command line argument in bash?
Use process substitution:
myprogram testfile $(wc -l < testfile) testfile.out
^^^^^^^^^^^^^^^^^^^
This way, wc -l < testfile
is evaluated together with the call of the program and you have both commands combined.
Note wc -l < file
returns you just the number, so you don't have to do cut
or any other thing to clean the output.
Passing output from one command as argument to another
As a sidenote, tail
displays the last 10 lines.
A possible solution would be to grep
this way:
for i in `ls -lf access.log*`; do grep $(tail $i |awk {'print $4'} |cut -d: -f 1| sed 's/\[/\\[/') $i > $i.output; done
How To Use A Command Output As An Argument of Another Command?
You can use a for /f
the command. For each line in the output of the executed command, the code in the do
clause will be executed, with the content of the line stored in the for
replaceable parameter
for /f "delims=" %%a in ('command') do CLITool -Switch %%a
Also, you can execute the command, send the output to a temporary file and retrieve its contents with a input redirection and a set /p
command
command > "tempfile"
<"tempFile" set /p "var="
CLITool -Switch %var%
In both cases, under the assumption that command
will echo only one line. If it generates more lines, the for
will execute CLITool
for each of the lines, while the redirection option will only retrieve the first line from the temp file.
The third way to solve it is to use a pipe. The left side of the pipe generates the content and the right side reads this content.
command | ( set /p "var=" && @call CLITool -Switch %%var%% )
Why the call
and the %%var%%
syntax? We are setting a variable inside a line and in the same line we need to retrieve the variable and for this to work, delayed expansion is needed. But by default delayed expansion is disabled and enabling it does not solve anything as each side of the pipe is executed inside its own cmd
instance that is started without delayed expansion (default behaviour) so, the trick is to escape the reference to the variable (the reason for the doubled percent sign) and force a double parse phase in the line (the reason for the call
)
The alternative could be something as
command | cmd /e /v /q /c "set /p "var=" && CLITool -Switch !var!"
This way we are running in the right side of the pipt a cmd instance with delayed expansion enabled (/v
), extensions enabled (/e
), with echo off
(/q
) that will execute the needed command.
But the pipe code (in both cases) has a drawback: the variable is set in a separate cmd instance, so, your main batch file will no have access to its value.
Related Topics
How to Read the Mouse Button State from /Dev/Input/Mice
Difference Between Bash Pid and $$
Create a Virtual Floppy Image Without Mount
Why Is Pr_Debug of the Linux Kernel Not Giving Any Output
Docker Change Cgroup Driver to Systemd
Docker MACvlan Network, Unable to Access Internet
What Is the Use of File Descriptor 255 in Bash Process
How to Use the Linux Flock Command to Prevent Another Root Process from Deleting a File
How to Search an Image for Subimages Using Linux Console
Finding Contents of One File in Another File
How to Specify Non-Capturing Groups in Sed
How to Return Exit Code 0 from a Failed Command
Use a C Library in Swift on Linux
How to Store One Billion Files on Ext4
How to Determine the Process Memory Limit in Linux