24.07.2018 Views

Bash-Beginners-Guide

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />

121script.sed<br />

120temp_file<br />

126test<br />

120twolines<br />

441txt2html.sh<br />

kelly@octarine ~/test><br />

This command printed the fifth column of a long file listing, which contains the file size, and the last column,<br />

the name of the file. This output is not very readable unless you use the official way of referring to columns,<br />

which is to separate the ones that you want to print with a comma. In that case, the default output separater<br />

character, usually a space, will be put in between each output field.<br />

Local configuration<br />

Note that the configuration of the output of the ls -l command might be different on your system.<br />

Display of time and date is dependent on your locale setting.<br />

6.2.2. Formatting fields<br />

Without formatting, using only the output separator, the output looks rather poor. Inserting a couple of tabs<br />

and a string to indicate what output this is will make it look a lot better:<br />

kelly@octarine ~/test> ls -ldh * | grep -v total | \<br />

awk '{ print "Size is " $5 " bytes for " $9 }'<br />

Size is 160 bytes for orig<br />

Size is 121 bytes for script.sed<br />

Size is 120 bytes for temp_file<br />

Size is 126 bytes for test<br />

Size is 120 bytes for twolines<br />

Size is 441 bytes for txt2html.sh<br />

kelly@octarine ~/test><br />

Note the use of the backslash, which makes long input continue on the next line without the shell interpreting<br />

this as a separate command. While your command line input can be of virtually unlimited length, your<br />

monitor is not, and printed paper certainly isn't. Using the backslash also allows for copying and pasting of the<br />

above lines into a terminal window.<br />

The -h option to ls is used for supplying humanly readable size formats for bigger files. The output of a long<br />

listing displaying the total amount of blocks in the directory is given when a directory is the argument. This<br />

line is useless to us, so we add an asterisk. We also add the -d option for the same reason, in case asterisk<br />

expands to a directory.<br />

The backslash in this example marks the continuation of a line. See Section 3.3.2.<br />

You can take out any number of columns and even reverse the order. In the example below this is<br />

demonstrated for showing the most critical partitions:<br />

kelly@octarine ~> df -h | sort -rnk 5 | head -3 | \<br />

awk '{ print "Partition " $6 "\t: " $5 " full!" }'<br />

Partition /var : 86% full!<br />

Partition /usr : 85% full!<br />

Partition /home : 70% full!<br />

kelly@octarine ~><br />

Chapter 6. The GNU awk programming language 71

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!