Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Linux Timesaving Techniques For Dummies.pdf
15.98 Mб

58 Technique 10: Keeping Your Life Simple with Aliases and Functions

Automating Tedious Tasks with Functions

A bash function is like an alias on steroids. A function has none of the restrictions of an alias. You can execute many commands within a bash function, and you can pass arguments to a function and use those arguments wherever you need them.

Filtering file searches by file type

Here’s another version of gf, this time written as a function instead of an alias:

function gfn ()


find . -name “$2” -print0 | xargs -0 -e grep -n -e $1


This function, which we’ve called gfn to distinguish it from the gf alias, expects two arguments. The first argument is a filename pattern, such as “*.c”, that specifies which files you want to search. The second argument to gfn is the text that you want to search for. If you want to search through all the files in your current directory, just use the pattern “*” (the double quotes are important). Now you can search for text in .txt files like this:

$ gfn Martini “*.txt”

./recipes/drinks.txt:200: the perfect Martini

The $1 variable holds the first command line argument (Martini), and $2 holds the second (“*.txt”). With a function, you can use the command line arguments wherever you need them. (With an alias, the arguments get tacked onto the end of the command line.)

Automatic downloading

In its most basic form, a function is a name that you give to a sequence of one or more commands. Functions are perfect for automating tasks that you

find yourself doing over and over again. If you often download, configure, and build software from the Web, you can save time by creating a simple function to automate that task:

function loadcode ()


wget -q -O - $1 | tar -zxvf -

cd $(basename $1 .tar.gz)




The loadcode function expects a single argument, the URL for a tarball that you want to download and install. To use this function, open your favorite editor, type the text for the loadcode function as shown, and save your changes to ~/.funcs.sh (which is just a random filename we’re using for this example). Now, use the source command to install loadcode into your shell:

$ source ~/funcs.sh

Next, find a package that you want to install and then run the loadcode function like this:

$loadcode ftp://ftp.gnu.org/gnu/barcode/barcode- 0.98.tar.gz

barcode-0.98/ barcode-0.98/CVS/ barcode-0.98/ChangeLog barcode-0.98/COPYING barcode-0.98/Makefile.in barcode-0.98/INSTALL barcode-0.98/barcode.h


The loadcode function has four commands inside it. The first command uses wget to download the

tarball and feeds the download to tar for unpacking. When you unpack a tarball like barcode-0.98.tar.gz, the content is stored in a subdirectory named barcode-0.98; the second command moves into that directory. The last two commands do the GNU-install

Соседние файлы в предмете Операционные системы