Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How could I capture the console output into a variable when running a utility so I can filter the text?


Example: I am trying to capture the console output when I type bitcoind so that I can filter out the rpcpassword and write it to a config file.

ubuntu@ip-172-31-3-49:~$ bitcoind
Error: To use bitcoind, you must set a rpcpassword in the configuration file:
/home/ubuntu/.bitcoin/bitcoin.conf
It is recommended you use the following random password:
rpcuser=bitcoinrpc
rpcpassword=Eb5WDgzKqt77U4LhvvfnYxaNvw2rpztSvM5XKsonXBWC
(you do not need to remember this password)
The username and password MUST NOT be the same.
If the file does not exist, create it with owner-readable-only file permissions.
It is also recommended to set alertnotify so you are notified of problems;
for example: alertnotify=echo %s | mail -s "Bitcoin Alert" admin@foo.com
share|improve this question

2 Answers 2

up vote 4 down vote accepted

Redirect the output to a file e.g.

bitcoind >~/bitcoind_output.txt 2>&1

This will put the output in a file in your home directory called bitcoind_output.txt

share|improve this answer
    
Cheers Glenn - well spotted with the typo :) –  NGRhodes Feb 21 at 20:43
2  
You'll want the 2>&1 after the >~/bitcoind_output.txt. –  minerz029 Feb 21 at 20:56
1  
@minerz029 That was the trick bitcoind >~/bitcoind_output.txt 2>&1 –  Brandon Clark Feb 21 at 22:41

To capture the output into a variable:

output=$(bitcoind 2>&1)
share|improve this answer
    
I would love this to work but it wasn't. –  Brandon Clark Feb 21 at 22:43
    
curious: what does the variable hold, and what goes to the screen? –  glenn jackman Feb 21 at 22:50
    
Nothing with: bitOut=$(bitcoind 2>&1) echo ${bitOut} –  Brandon Clark Feb 21 at 23:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.