moreutils completes the shell's toolbox

Slashdot it! Delicious Share on Facebook Tweet! Digg!

sponge

The sponge command is also suitable for use in pipes. The shell does offer the possibility of redirecting data at any time to a file for transfer. The data is continuously deposited in pieces (usually line by line) in a file throughout the entire run of the command line, or pipeline.

sponge does this in a different way. It first collects all the data internally and then transfers it to a file all in one go at the very end of a run. Among other things, this makes it possible to use the same file for input and output.

Take for example file.txt shown in Listing 13. When no output file has been designated, the program uses the standard output channel. If an output file exists, it preserves its permissions. The -a option, shown in the second example of Listing 14, appends the output to the contents of the file.

Listing 13

file.txt

7
5
3

Listing 14

Sorting with sponge

$ sort -u file.txt | sponge file.txt
$ cat file
3
5
7
$ sort -r file.txt | sponge -a file.txt
$ cat file.txt
3
5
7
7
5
3

Listing 15

file.txt

$ errno eio
EIO 5 Input-/Output error
$ errno 5
EIO 5 Input-/Output error
$ errno -s Output error
EIO 5 Input-/Output error
EREMOTEIO 121 Input-/Output error of the remote
$ errno -l | grep 121
EREMOTEIO 121 Input/Output error of the remote

Handling Errors

There are a number of Linux tools like strace that output very cryptic messages concerning supposed errors. Normally you would look up the symbolic name by means of:

man 3 <error number>

in the hope that the description offers some help.

It is much easier to match up names and suitable numbers by using errno (Listing 15). The -s option (--search ) lets you search among the list of error messages for a particular term independently of its spelling. In addition, the program looks via -S for all man pages that have been installed in other languages. Occasionally, you get additional hits this way. Searching by error number does not work like this. Instead you should use the parameter -l to have errno output all of the error messages and then search through the list with grep .

Buy this article as PDF

Express-Checkout as PDF

Pages: 7

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content