Starting a process will output sensitive information on the bash. I want to extract this value of an occuring pattern and assign it to bash variables. In concrete, starting the process vault (from hashicorp)
$ vault server -dev
will result in output like this
[...] You may need to set the following environment variable:
$ export VAULT_ADDR='http://127.0.0.1:8200'
The unseal key and root token are displayed below in case you want to seal/unseal the Vault or re-authenticate.
Unseal Key: n1yYaOhsKnE1mM+P1ezNTD1yHlhglmEM/F/+gzX0Zl0=
Root Token: s.k9C3agdaneOw809EnTqqErAM
Development mode should NOT be used in production installations!
==> Vault server started! Log data will stream in below:
So I'm interest in extracting the values from 'Unseal Key: ' and 'Root Token: '. Optionally all other output can be stored in a log file. However, the both values (unseal key and root token) should never be (directly) be visible on the commandline nor be stored in a file.
I tried to solve this problem by using sed and redirect the output to the read command. It looks like this
$ read -r unseal_key root_token <<< $(vault server -dev | sed -n -e 's/Unseal Key: //p' -e 's/Root Token: //p' a.txt); echo "$unseal_key /\ $root_token"
a.txt should be a logfile where the rest of the output is stored. As said before, it is not necessary. The output for this command is empty. By splitting up the command and only executing
$ vault server -dev | sed -n -e 's/Unseal Key: //p' -e 's/Root Token: //p'
I observed the (for me) odd behaviour, that the replaced string occured in different positions (but together) in the output.
Since I'm not really sure if sed fits the best for this case or if this even possible, the way I thought it, let me hear your thoughts about it.
So in summary
- Starting a process results in commandline output
- Two of these lines should not be displayed or temporarily stored in a file
- These two values should be accessible after executing the command in variables