Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write to stdout instead of file

Tags:

bash

How can I write data to stdout instead of writing file?

I'm using GPG and want to print encrypted text to stdout, without saving files.

However, with gpg command, encrypted text is written to file in ordinary way:

$> gpg --recipient [email protected] --armor --output encrypted.txt --encrypt example.pdf

(With above command, encrypted file is saved in encrypted.txt)

What I want to do is like following:

$> gpg --recipient [email protected] --armor --output <STDOUT> --encrypt example.pdf

and encrypted messages are shown in console.

I don't want to save hard disk to avoid loss of performance.

like image 510
Jumpei Ogawa Avatar asked Dec 21 '22 19:12

Jumpei Ogawa


2 Answers

Usually you use "-" to specify stdout, but not all commands accept this and I don't about gpg.

For example:

tar -cvzf - foo/ ¦ split -b 50k foobar_

will pipe the "tar-file" to stdout, split it and save to "foobar_<123...>".

like image 51
bos Avatar answered Dec 30 '22 16:12

bos


If your application does not support '-' as a filename (and it should) then an alternative is to use a named pipe, although I'll admit this looks like using a sledgehammer to crack a nut:

pipe='/tmp/pipe'
mkfifo "$pipe"

gpg --recipient [email protected] --armor --output "$pipe" --encrypt example.pdf &

while read 
do
    echo "$REPLY"
done < $pipe

rm "$pipe"
like image 22
cdarke Avatar answered Dec 30 '22 14:12

cdarke