Come estrarre il contenuto del file e passare come parametri della riga di comando in Linux?


0

Devo eseguire un comando:

python test.py command --option 1 value1 value2 value3 value4 value5

(fino a valore100)

Ho un file di testo con valori separati da spazio, come:

valore1 valore2 valore3 valore4 valore5 ..

Come posso inserirlo nel comando senza copiare e incollare l'intero contenuto del file nel terminale?

Risposte:


4

È possibile utilizzare la funzione di sostituzione del comando Bash:

python test.py command --option 1 $(<file.txt)

A partire dal man bash:

Command substitution allows the output of a command to replace
the command name. There are two forms:

$(command)

or

`command`

Bash performs the expansion by executing command and replacing
the command substitution with the standard output of the command,
with any trailing newlines deleted. Embedded newlines are not
deleted, but they may be removed during word splitting. The
command substitution $(cat file) can be replaced by the
equivalent but faster $(< file).

1

Ecco un altro modo:

xargs -a file.txt python test.py command --option 1

0

Tu puoi fare:

python test.py command --option 1 `cat file.txt`
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.