LPI Linux Certification in a Nutshell by Adam Haeder & Stephen Addison Schneiter & Bruno Gomes Pessanha & James Stanger

LPI Linux Certification in a Nutshell by Adam Haeder & Stephen Addison Schneiter & Bruno Gomes Pessanha & James Stanger

Author:Adam Haeder & Stephen Addison Schneiter & Bruno Gomes Pessanha & James Stanger [Adam Haeder]
Language: eng
Format: epub
Tags: COMPUTERS / Operating Systems / Linux
ISBN: 9781449390136
Publisher: O'Reilly Media
Published: 2010-06-15T00:00:00+00:00


Functions

In addition to aliases, bash also offers functions. They work in much the same way as aliases, in that some function name of your choosing is assigned to a more complex construction. However, in this case that construction is a small program rather than a simple command substitution. Functions have a simple syntax:

[ function ] name () { command-list; }

This declaration defines a function called name. function is optional, and the parentheses after name are required if function is omitted. The body of the function is the command-list between the curly brackets ({ and }). This list is a series of commands, separated by semicolons or by newlines. The series of commands is executed whenever name is specified as a command. The simple lsps alias shown earlier could be implemented as a function like this:

$ lsps () { ls -l; ps; }

Using this new function as a command yields exactly the same result the alias did. However, if you implement this command as a function, parameters can be added to the command. Here is a new version of the same function, this time entered on multiple lines (which eliminates the need for semicolons within the function):

$ lsps () { > ls -l $1 > ps aux | grep `/bin/basename $1` > }

The > characters come from bash during interactive entry, indicating that bash is awaiting additional function commands or the } character, which terminates the function definition (this is called the secondary shell prompt). This new function allows us to enter a single argument to the function, which is inserted everywhere $1 is found in the function. These arguments are called positional parameters because each one’s number denotes its position in the argument list. This example uses only one positional parameter, but there can be many, and the number of parameters is stored for your use in a special variable $#.

The command implemented in the previous example function now prints to STDOUT a directory listing and process status for any program given as an argument. For example, if the Apache web server is running, the command:

$ lsps /usr/sbin/httpd

yields a directory listing for /usr/sbin/httpd and also displays all currently running processes that match httpd:

-rwxr-xr-x 1 root root 317072 2010-01-22 14:31 /usr/sbin/httpd root 1882 0.0 1.5 22664 8088 ? Ss Aug10 0:14 /usr/sbin/httpd apache 20869 0.0 0.6 22664 3560 ? S 04:27 0:00 /usr/sbin/httpd apache 20870 0.0 0.6 22664 3560 ? S 04:27 0:00 /usr/sbin/httpd apache 20872 0.0 0.6 22664 3560 ? S 04:27 0:00 /usr/sbin/httpd apache 20874 0.0 0.6 22664 3560 ? S 04:27 0:00 /usr/sbin/httpd apache 20875 0.0 0.6 22664 3560 ? S 04:27 0:00 /usr/sbin/httpd apache 20876 0.0 0.6 22664 3560 ? S 04:27 0:00 /usr/sbin/httpd apache 20877 0.0 0.6 22664 3560 ? S 04:27 0:00 /usr/sbin/httpd apache 20878 0.0 0.6 22664 3560 ? S 04:27 0:00 /usr/sbin/httpd



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.