In bash
, you capture the output of a command with $()
(or backticks, but I prefer the former since you can nest them):
pax> xx=$(ls -1 | grep bac)
pax> echo "..${xx}.."
..backup0.sh
backup1.sh..
So, in your particular case you'd be looking at something like:
timestamp=$(db2 backup db $SOURCE online compress include logs 2>&1
| tee /tmp/output
| grep 'image is'
| awk '{print $11}')
Keep in mind I've split that across multiple lines for readability but it should go on a single line. As to how it works:
- The
2>&1
combines standard output and error.
- The
tee
will ensure you have the entire output stored somewhere.
- The
grep
will give you the relevant line.
- The
awk
will print out only 20100906142221
.
- The
xx=$(...)
will take that and assign it to xx
.
You may need to fine-tune some of the command if your output is slightly different to what you've stated but that should be a good start.
Based on comments, here's what you need to start with:
#!/bin/bash
echo what's the source db name?
read SOURCE
echo what's the target db name?
read TARGET
db2 backup db $SOURCE online compress include logs 2>&1 | tee /tmp/db_bkp_$$
DB2TIME=$(cat /tmp/db_bkp_$$ | grep 'image is' | awk '{print $11}')
rm -rf /tmp/db_bkp_$$
db2 restore database $SOURCE taken at $DB2TIME into $TARGET
This will execute the DB2 command sending output and error to both the terminal and the temporary file ($$
gives you the current process ID to avoid file name collisions). You then extract the date/time from the temporary file then delete that file.
Then you use the date/time.