📜 ⬆️ ⬇️

A little about Bash and related sciences. Part 1

If you actively use Linux for administrator tasks, then you probably look in from time to time to the console (or live in it). Despite the active ousting of the text interface by the graphical one, it’s still intuitive to poke checkmarks and press buttons, which gave rise to a whole generation of enikeyschikov, the console was, is and will be an effective means of communicating with the computer. This article is intended for those who are already familiar with Bash (Bourne-again Shell) , the most popular implementation of the command shell. This terminal has been used by default in almost every Linux distribution for many years, so novices do not even realize that there are other shells. Bash is permeated with the wisdom of our UNIX ancestors and is strongly recommended for mastering. Now you will see that the console is useful not only for commands like “sudo /etc/rc.d/network restart” :)

Aliases


Three-story teams look very cool, but typing them every time is tiring. So Bash allows you to create short arbitrary aliases for any set of commands. For example, you can create a command-alias "la", which will run "ls -a".

alias la='ls -a'

And so that when you restart your new settings are not lost, write down the aliases in the file ~ / .bash_aliases .
')

Conditional statements


Everyone who knows at least the basics of programming is familiar with the concept of conditional operators such as if then else . Although bash is not a programming language, it allows the use of such operators, which is incredibly convenient and allows you to avoid using languages ​​like Perl or Python in simple tasks. For example:

if uname -a | grep "GNU/Linux" > /dev/null; then echo " Linux"; fi

It works as follows: first, the “uname -a” command is executed, which receives information about your system. The information obtained is sifted through the grep filter, which looks for the GNU / Linux string in it. The output is sent to “dev / null” (Linux black hole), because we are interested in the fact of the presence of a string, and not its output to the screen. If , then and fi , as you may have guessed, is the conditional operator Bash. “If” checks the condition, “then” describes an action that matches the condition, and “fi” simply means the end of the logical structure. The condition can be of any complexity.

Let's look at a more useful example and check whether Firefox is running somewhere in the system. To do this, use the “ps aux” command, and redirect the output to the grep filter. Grep itself is a program, so it is in the process list. Since “firefox” is contained in “grep firefox”, it turns out that Firefox is supposedly in the processes, although we just started searching for this line. To get around the problem, use grep twice to filter yourself :) Run it with the -v option to invert the condition:

if ps aux | grep firefox | grep -v grep > /dev/null; then echo "Firefox "; fi

Constantly recruiting such a large team is inconvenient, so we’ll make a short alias out of it:

alias isff='if ps aux | grep firefox | grep -v grep > /dev/null; then echo "Firefox "; fi'

Now for verification you just need to execute the isff command.

Cycles


The true power of Bash is not even in conditional statements, but in cycles. Cycles are used to repeat an operation several times. Consider an example:

while (true); do sleep 1; date; done

The command every second displays the date and time on the screen until you press Ctrl + C. But this is somehow stupid, let's do something more useful.

How, for example, unpack several tar.bz2 archives with one command? And so: with the command "ls" we get a list of bz2-files in the directory and with the help of a cycle we process each file separately. Of course, in automatic mode.

for i in `ls *.tar.bz2`; do tar xjf "$i"; done

Pay attention to the reverse apostrophe symbol (it is located on the keyboard to the left of the number 1). This is an operator that substitutes the result of the “ls * .tar.bz2” command at the appropriate place in the loop.

Underestimated grep


Grep is usually used for solving moronic tasks, such as:

somecommand | grep "fuuu"

That is, for coarse filtering of a string of information (sounds like light amplification by stimulated emission of radiation). In general, the grep command has a huge number of parameters, but here are the most basic ones and they should be remembered:

-c Count the number of occurrences of the search string.
-i Search is not case sensitive.
-l Displays the names of the matched files (this is a lowercase L).
-n Displays the number of the corresponding query string.
-r Performs a recursive directory search.
-v Inverts the search condition (strings that do not contain the search target).


Parameters can be combined, and grep can be sent to itself. For example, if you need to count the number of files that contain the word "paul" (or "PAUL", "PaUl", etc.), except for those that have the extension .txt, you should do something like this:

grep -ilr paul * | grep -cv "\.txt$"

Here we use the most important grep properties. The combination of the -ilr parameters means that the search is performed for files of the words “PAUL”, “PaUl”, etc., the search is recursive in your file system, starting from the current directory and returns the names of the matching files. All this arrives at another instance of grep, which will be executed with the -cv parameters, which, in turn, include a counting and searching mode for files that do not correspond to the regular expression "\ .txt $". The backslash here escapes the point, without it we would get a special character. A dollar sign means that .txt must be at the end of the file name. That is, "fuuu.txt.boo" will not match the expression.

Not obvious find


The find command does not obey the laws of common sense. That is, do not think that you can use it in the form of "find <needle> <stack_sen>." In contrast, find works like a filter.

find .

A dot at the end means that a list of all files in the current directory will be returned. But the "ls -a" command does, in principle, the same thing. Therefore, we complicate the task. Find the files in the directory with the name “arr”.

find . -name "*arr*"

And you can add the parameter "-size" and find only those files that are larger than 1 megabyte.

find . -name "*bar*" -size +1M

The "-user" parameter returns files belonging to the specified user. For example:

find . -name "*bar*" -user veles

This is also useful with the "-not" parameter, which, you guessed it, inverts the search:

# , myfile.txt , veles
find . -not -name "myfile.txt" -not -user veles


Another cool filter is "-newer". It returns all files that are newer than the specified file. This is very convenient for backup scripts: when you make a copy, just specify an arbitrary file, and it will serve as a timestamp that can be used in the “find -newer” command. This will give you a list of all the files that have changed since the creation of this file.

find . -newer /path/to/myfile

For now enough. Of course, tons of parts are missed and remain for you to study on your own. “Mana” has not been canceled yet, and the purpose of this article is to draw your attention to the great and mighty UNIX way.

Next time we will get acquainted with multitasking management (have you forgotten that the Linux console is multitasking?), And also make friends with the cron scheduler, which can be your favorite means of automating routine work.

PS Unicsoids with the experience, most likely, for a long time all know it. But this article is written more for good appetite.

Source: https://habr.com/ru/post/89468/


All Articles