Emulate reading from file on application argument

bash shell arguments command-line-interface sh

33 просмотра

1 ответ

54760 Репутация автора

I'm working with a command line application (geth) that accepts the argument --password which expects a valid path of a file containing a password, i.e.:

geth --password /some/path/to/password/file

I'm trying to specify the password on the command line (I'm aware of the security implications) instead of keeping it on a file, for example:

geth --password `echo P@ssWordHer3`

The above doesn't work because geth is searching for a file named P@ssWordHer3, which doesn't exist:

Fatal: Failed to read password file: open P@ssWordHer3: no such file or directory

I was thinking about creating a shell script that would accept a password as argument and output it, but I guess it also doesn't work because geth will try read the file content instead of the output.

Any idea how to solve this?

Автор: Pedro Lobito Источник Размещён: 08.11.2017 11:42

Ответы (1)

2 плюса

194249 Репутация автора


This is what the ksh and bash feature known as process substitution is for:

#!/usr/bin/env bash
#              ^^^^- also works on ksh, but not on baseline-POSIX /bin/sh

geth --password <(echo 'P@sswordHer3')

The <(...) expansion will be replaced with a filename which, when read from, will return the output of the code contained therein.

Note that this is a FIFO (either a named pipe or a /dev/fd/...-style name, depending on the operating system), so it can only be read once, front-to-back, and does not allow seeking or rereading.

If you need a real file, as opposed to a FIFO, zsh (which I personally don't recommend) has an expansion for that too:

#!/usr/bin/env zsh
geth --password =(echo 'P@sswordHer3')
Автор: Charles Duffy Размещён: 08.11.2017 11:55
Вопросы из категории :