This is an iEntry.com Website
Search iEntry News

Submit Your Site For Free!

Email Address:
* URL:
*
*Indicates Mandatory Field

Terms & Conditions

LinuxProNews
SecurityProNews
ITmanagement








Releasing The Potential Of GNU Core Utilities, Part 2

By Joe Purcell
Expert Author
Article Date: 2011-07-13

Previously we began looking at the core utilities available to linux. Continuing looking at file utilities we will look at two in particular that are very useful for producing output in writing bash scripts: echo and printf.

File Utilities



3. echo



Echo prints a string to standard output. Most who have written bash scripts know the basic use of this, so let's look at a few examples that are a little more exotic, but highly useful.

version=`uname -r`
echo "My username is "`whoami`" and am running kernel version "${version/-generic/}'.'


Any programmer would find this first example not intuitive. First, note that there are no concatenating operators like '.', each segment is automatically combined. Second, notice that the use of a tick marks ` denote that what is inside should be executed as a command. Third, note that replacement operations can be placed on variables, which is not specific to the echo command, but very useful in this situation. We can remove or replace text on the fly without having to store an additional variable.

Two additional things to note about echo: first, by default an end of line character is printed, and second, special characters are not interpreted. To overcome both of these, use the 'n' flag which prevents printing the trailing newline and use the 'e' flag which enables interpretation of blackslash escapes for special characters. Compare the two outputs in the following example:

echo "\tExample #"; echo "1"
echo -ne "\tExample #"; echo "2"


And, one quick final note, each command in bash can be ended by a semicolon to allow multiple commands be run concurrently.

4. printf



Printf is where printing to the command line gets fancy. For the sake of brevity we will look at just one example that is someone complex and explain what is going on.

GREEN=`echo -e "\033[32m"`
RED=`echo -e "\033[31m"`
NORMAL=`echo -e "\033[m"`
echo -n "Loading..."; sleep 1; printf "%95s\n" "[ $GREEN DONE $NORMAL ]"
echo -n "Halting..."; sleep 1; printf "%95s\n" "[ $RED FAIL $NORMAL ]"


With more familiarity with bash scripting, these types of print statements can be extremely useful. The first three lines are setting colors, which are unnecessary, but handy if writing a large script where the colors will be reused. The first line of output prints "Loading...", then the system pauses for 1 second from the sleep command, and then prints a statement with green color, and the one after it with red color. For another example of printing with color, see the article on Wikipedia.

About the Author:
Joe Purcell is a technology virtuoso, cyberspace frontiersman, and connoisseur of Linux, Mac, and Windows alike.



Newsletter Archive | Article Archive | Submit Article | Advertising Information | About Us | Contact