Project56.Cool Shell Tips


Project 56. Cool Shell Tips

"How do I override the shell's command-line processing order?"

This project gives some miscellaneous tips for the Bash and Tcsh shells.

Create Your Own Message of the Day

The message (of the day) you see at the top of every new Terminal windowWelcome to Darwin!is read from the file /etc/motd. It's easy to change this message; simply edit the file. Because of the permissions set on the file, you must edit it as the root user, using the sudo command. Type the following.

$ sudo nano /etc/motd Password: ...


If you want the message to truly be a Message of the Day, you'll have to change it every day!

Format Output with printf

The printf command is much more powerful than echo. It takes a format string and a set of arguments to print within the context of the format string.

Let's define a function that takes two arguments and displays them in aligned columns, with the first number in red. The function is written purely to illustrate some tricks we can employ in printf.

Type the following.

$ function display () { > norm="\033[00m" > red="\033[31m" > indent=${3:-4} > spacer=$((20-$indent)) > printf "%${indent}s%-${spacer}s${red}%7s,${norm}%7s\n" ¬    " " "Values:" $1 $2 > }


Learn More

The projects in Chapter 4 look at several Unix text editors, including nano.


Let's examine the function line by line.

First, we define two variables called norm and red. They each hold an ANSI code that changes the color of the text written to Terminal. The sequence \033 is interpreted by printf to be an escape character. We didn't have to define variables; instead, we embedded the sequences in the printf statement. The variable route is neater, especially in real-world scenarios where a function or script contains numerous printf statements.

Variable indent is set to the value of the third parameter unless it is undefined, in which case a default value of 4 is assigned. Variable spacer is set to 20-$indent. The sequence $((...)) tells Bash to perform integer arithmetic when evaluating the expression.

Finally, we form a printf statement. The first argument is a format string, which, for a default value of 4 for indent, evaluates to

printf "%4s%-16s\033[31m%7s,\033[00m%7s\n" "" "Values:" aaa bbb


The format string is a sequence of characters that will be displayed onscreen, with special placeholder sequences like %ns and %-ns interspersed. The sequence %ns says to display a string right aligned in an n-character field, and %-ns says to display a string left aligned in an n -character field. After the format string, we must provide a value for each placeholder sequence. In this example, the values are "", "Values:", "aaa", and "bbb".

Try the function, and play with some examples yourself to understand what is happening.

$ display 3141 2178 6       Values:         3141,  2178


Learn More

Project 52 covers Bash functions in detail.


Learn More

To learn more about printf, refer to its man page.

$ man 3 printf



Prevent Execution of Initialization Scripts

To launch a login shell from the command line, specify the option --login.

$ bash --login


To prevent Bash from running /etc/profile and ~/.bash_profile when it is invoked as a login shell, specify the option --noprofile.

$ bash --login --noprofile


To prevent a non-login shell from running /etc/bashrc and ~/.bashrc, specify option --norc.

$ bash --norc


Script vs. source vs. .

When you execute a script from the command line by typing

$ ./name-of-script


a new shell instance is launched to execute the script. If you want the script to be executed by the current shellwhen it changes the shell environment, for exampleyou must source it by typing

$ source name-of-script


or

$ . name-of-script


Argument Recall

Recall the last argument of the last command by typing Escape period (.).

Learn More

Refer to Project 47 for more information on Bash initialization scripts.


Auto Options

If you always use a particular command with a specific set of its options turned on, an alias can save typing: Just make the generic command name an alias for the option-enabled version. An alias can include its own name in the definition, and Bash understands not to attempt expansion during the definition.

$ alias ls='ls -lt'


Override Command-Line Processing Order

A very handy trick, should you fall afoul of Bash's command-line processing order, uses the eval command to tell the shell to take a second sweep at processing and expanding a command line.

Here's an example in which we are tripped up by the fact that the shell evaluates tilde expansion before variable expansion.

$ d="~/Documents" $ ls $d ls: ~/Documents: No such file or directory


We solve this problem by telling the shell to evaluate the command line a second time. Now, after the first sweep has expanded $d to ~/Documents, the second sweep will expand tilde to be your home directory.

$ eval ls $d AppleWorks User Data   Microsoft User Data   iChats Downloads              Sophie.ichat          text


Tcsh

Try the following Tcsh built-in command. (There's no space around the dash.)

% ls-F


If you never want to hear another beep from Tcsh, ever, simply type

% set nobeep


Learn More

Project 51 covers Bash aliases in detail.


Learn More

Project 54 covers Bash command-line parsing.





Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net