Sommario: Nello script Perl, -n
viene trattato come un argomento da includere nell'output di echo
, non una bandiera di sopprimere la nuova riga. (Prova
$hash = `echo -n 'abcd'`;
confermare). Utilizzare invece printf
.
Perl utilizza /bin/sh
per eseguire codice in tic schiena. Anche se /bin/sh
è un collegamento a bash
, si comporterà diversamente quando invocato tramite simile. In modalità POSIX,
echo -n 'abcd'
uscita volontà
-n abcd
che è, l'opzione -n
non è riconosciuto come una bandiera per sopprimere un ritorno a capo, ma viene trattato come un argomento normale per la stampa. Sostituire echo -n
con printf
in ogni script e si dovrebbe ottenere lo stesso hash SHA da ogni script.
(AGGIORNATO: bash
3.2, quando invocato come sh
, visualizza questo comportamento.Le versioni più recenti di bash
sembrano continuare a trattare -n
come un flag quando invocato come sh
.)
Ancora meglio, non sborsare per fare le cose che si possono fare in Perl.
use Digest::SHA;
$hash = Digest::SHA::sha256('abcd');
Per i curiosi, ecco cosa POSIX spec has to say su echo
. Non sono sicuro su cosa fare della conformità XSI; bash
echo
richiede l'opzione -e
per trattare i caratteri di escape specialmente, ma quasi tutti i serbatoi — tranne vecchie versioni di bash
, e quindi solo in casi particolari — trattano le -n
come flag, non una stringa. Oh bene.
The following operands shall be supported:
string
A string to be written to standard output. If the first operand is -n, or
if any of the operands contain a <backslash> character, the results are
implementation-defined.
On XSI-conformant systems, if the first operand is -n, it shall be treated
as a string, not an option. The following character sequences shall be
recognized on XSI-conformant systems within any of the arguments:
\a
Write an <alert>.
\b
Write a <backspace>.
\c
Suppress the <newline> that otherwise follows the final argument in the output. All characters following the '\c' in the arguments shall be ignored.
\f
Write a <form-feed>.
\n
Write a <newline>.
\r
Write a <carriage-return>.
\t
Write a <tab>.
\v
Write a <vertical-tab>.
\\
Write a <backslash> character.
\0num
Write an 8-bit value that is the zero, one, two, or three-digit octal number num.
Assegnare il percorso completo a 'echo'. Altrimenti userete il bash built-in in un caso, e possibilmente qualcos'altro nell'altro caso. – ThisSuitIsBlackNot
Sei pignolo: i 2 hanno in sé più o meno gli stessi 16 caratteri ;-) –
Sì. Usato '/ bin/echo' - questo genere di cose mi tiene sveglio la notte. – wcochran