Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
100 Linux Tips And Tricks.pdf
Скачиваний:
48
Добавлен:
17.08.2013
Размер:
1.25 Mб
Скачать

100 Linux Tips and Tricks

Tip 4: Parsing the command line in BASH

Bash is a shell, but it's also a scripting language. You can make bash scripts, and you can pass parameters to it. You can access the parameters with $1, $2, ... But what if you need the whole command line?

Bash uses special variables for all kind of purposes. One of them is $* which contains the whole command line. You can use it rather than the $1, $2 if all you need is a string of text, passed on the command line.

101

100 Linux Tips and Tricks

Tip 5: Don't grep grep

A useful tool in scripting is the "grep" function. This program will find a string in a file. It is often used also to find if a process is running:

ps ax | grep sendmail

That command will find if sendmail is running. The problem is that you might end up with an other entry for this command. Because you grep for "sendmail", you may well end up with this command showing because the "sendmail" string is in the command line. To avoid that, you can use the -v flag to grep:

ps ax | grep sendmail | grep -v grep

That command will find all the lines in the current process list that have the "sendmail" string in it, and will remove the lines containing the string "grep".

102

100 Linux Tips and Tricks

Tip 6: Move a text into upper case letters

When you want to do text manipulation, you can use Sed and Awk. These 2 tools which come on most Linux distributions, will allow you to modify text files in many ways.

To move a text file into upper case letters, you can use Awk in the following way:

awk '{ print toupper($0) }' old_file > new_file

Sed and Awk are useful for a lot of other uses, and are integrated in several products.

103

100 Linux Tips and Tricks

Tip 7: Using PASCAL on Linux

Linux comes with a lot of compilers and interpreters. These include programs for C, C++, Perl, TCL/TK and more. Unfortunately most Linux distributions don't come with a Pascal compiler. Is it possible to compile Pascal programs? It sure is.

Several projects were started to make a Pascal compiler for Linux. One is called GNU Pascal and is available from http://agnes.dida.physik.uni-essen.de/~gnu-pascal. That program will also run on any operating system that supports the GNU C compiler.

104