So che questo è vecchio, e TR funziona altrettanto bene, ma mi è capitato tra questa domanda e ho notato OP ha dichiarato: sto eseguendo in sh, non bash. Quindi nidificare in echo -n "$ (grep -c pattern)" non funziona neanche.
Questo non è grep o sh quanto il modo in cui viene utilizzato l'eco. Per i visitatori futuri, l'unica ragione per cui questo non ha funzionato è dovuta alle doppie virgolette attorno al comando sostituito. Quanto segue, in effetti, funziona anche usando sh.
echo -n $(grep -c pattern)
Esempi:
$ ls /dev/sd? #example of formatted output
/dev/sda /dev/sdc /dev/sde /dev/sdg /dev/sdi /dev/sdk
/dev/sdb /dev/sdd /dev/sdf /dev/sdh /dev/sdj
$ echo $(ls /dev/sd?) #without -n, appends \n only at the end
/dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf /dev/sdg /dev/sdh /dev/sdi /dev/sdj /dev/sdk
$ echo -n $(ls /dev/sd?) #with -n, does not append the \n, but still strips the line breaks from the string
/dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf /dev/sdg /dev/sdh /dev/sdi /dev/sdj /dev/sdk
$ echo -n "$(ls /dev/sd?)" #output when double quotes are used
/dev/sda
/dev/sdb
/dev/sdc
/dev/sdd
/dev/sde
/dev/sdf
/dev/sdg
/dev/sdh
/dev/sdi
/dev/sdj
/dev/sdk
fonte
2015-12-02 17:07:37
questo ha funzionato! Grazie! Accetterà quando mi consentirà: P –