Using output of previous commands in bash
Since the amount of output is indeterminate, it doesn't make sense for bash
to store it for you for re-display. But there's an alternate solution to your problem:
The tee
command allows you to duplicate an output stream to a file. So if you're willing to use a file for temporary storage, you can do something like this:
make | tee output.txt
grep "warning" output.txt
This solution avoids running make
twice, which could be (a) expensive and (b) inconsistent: the second make may be doing less work than the first because some targets were already made the first time around.
Note: I haven't tried this. You may need to fiddle with joining the error and output streams, or such.
Automatically capture output of last command into a variable using Bash?
This is a really hacky solution, but it seems to mostly work some of the time. During testing, I noted it sometimes didn't work very well when getting a ^C on the command line, though I did tweak it a bit to behave a bit better.
This hack is an interactive mode hack only, and I am pretty confident that I would not recommend it to anyone. Background commands are likely to cause even less defined behavior than normal. The other answers are a better way of programmatically getting at results.
That being said, here is the "solution":
PROMPT_COMMAND='LAST="`cat /tmp/x`"; exec >/dev/tty; exec > >(tee /tmp/x)'
Set this bash environmental variable and issues commands as desired. $LAST
will usually have the output you are looking for:
startide seth> fortune
Courtship to marriage, as a very witty prologue to a very dull play.
-- William Congreve
startide seth> echo "$LAST"
Courtship to marriage, as a very witty prologue to a very dull play.
-- William Congreve
How to read the output of previous command and then proceed to the next command
try this:
#!/bin/bash
Commands=(
"command1 parameter1 parameter2"
"command2 parameter1"
"command3 parameter1 parameter2 parameter3"
)
tmplog=$(mktemp)
for cmd in "${Commands[@]}"; do
echo "$cmd"
$cmd >"$tmplog"
tail -1 "$tmplog" | grep -v "^Successfully" && break
done
rm "$tmplog"
Execute the output of previous command line
This should do it I believe.
my-first-command | bash
In a bash pipe, take the output of the previous command as a variable to the next command (Eg. if statement)
Following your narrow request looks like:
sha256sum abc.txt |
awk '{print $1}' |
if [ "$(cat)" = "8237491082roieuwr0r9812734iur" ]; then echo "match"; fi
...as cat
with no arguments reads the command's stdin, and in a pipeline, content generated from prior stages are streamed into their successors.
Alternately:
sha256sum abc.txt |
awk '{print $1}' |
if read -r line && [ "$line" = "8237491082roieuwr0r9812734iur" ]; then echo "match"; fi
...wherein we read
only a single line from stdin instead of using cat
. (To instead loop over all lines given on stdin, see BashFAQ #1).
However, I would strongly suggest writing this instead as:
if [ "$(sha256sum abc.txt | awk '{print $1}')" = "8237491082roieuwr0r9812734iur" ]; then
echo "match"
fi
...which, among other things, keeps your logic outside the pipeline, so your if
statement can set variables that remain set after the pipeline exits. See BashFAQ #24 for more details on the problems inherent in running code in pipelines.
Related Topics
Read Serial Data Without High CPU Use
Linux: How to Debug a Sigsegv? How to Trace The Error Source
How to Programmatically Pick and Choose Which Core of a Multi-Core CPU My Thread Should Run On
How to Get The System Home Directory in Cmake on Linux
Customize Tab Completion in Shell
Configure "-Prefix" Option for Cross Compiling
Gdb Complains No Source Available
Getting Function Arguments Using Kprobes
What Is Dma Mapping and Dma Engine in Context of Linux Kernel
Inappropriate Ioctl for Device When Trying to Ssh
Check for Iommu Support on Linux
What Does an Asterisk at The End of a Mv Command Do
Command to Check Status of Message Queue and Shared Memory in Linux
How to Get The Bash Date Script to Return a Day of The Week Relative to a Non-Current Time