Ho un file di testo in cui ogni riga è un elenco di argomenti che voglio passare ad uno script nodejs. Ecco un esempio di file, file.txt:passando gli argomenti citati al nodo tramite script di shell?
"This is the first argument" "This is the second argument"
Per l'amor di dimostrazione, lo script nodo è semplice:
console.log(process.argv.slice(2));
voglio eseguire questo script nodo per ogni riga nel file di testo, in modo da fatto questo script bash, run.sh:
while read line; do
node script.js $line
done < file.txt
Quando ho eseguito questo script bash, questo è quello che ottengo:
$ ./run.sh
[ '"This',
'is',
'the',
'first',
'argument"',
'"This',
'is',
'the',
'second',
'argument"' ]
Ma quando ho appena eseguito direttamente lo script del nodo ottengo i risultati attesi:
$ node script.js "This is the first argument" "This is the second argument"
[ 'This is the first argument',
'This is the second argument' ]
cosa sta succedendo qui? C'è un modo più nodo per farlo?
grazie! quello ha fatto il trucco – Rafael