📜 ⬆️ ⬇️

Bash naval battle

Recently, in order to study bash, he wrote on it a game of "sea battle" for playing over the network. The game was called "Sink 'em all".

image

Of the external programs, only nc (for organizing interaction over TCP), stty (for turning off the echo when the key is pressed) and clear (for clearing the terminal) are used.

The source code can be viewed here , but it is better to download it here or here , because PasteBay inserts line breaks in the Windows style, which in turn causes the interpreter to stop using hashbang ( #! ).
')

How to play


Download the source code, rename the file to sink.sh and put the executable bit on it. After this, the game can be safely launched (or not boldly - is there a malicious code in it?). To start the server, use the -serve :

./sink.sh -serve

To join a server, pass its IP address or domain name as a parameter, for example:

./sink.sh localhost

You will receive further management tips during the game. In what place of the code to enter the lines to deceive the enemy, I propose to find out yourself :)

What have I learned?


In the process of writing, I discovered some previously unknown features and features of bash. Among them, for example, coprocesses. Using the coproc built-in command, coproc can create a coprocess and communicate with it after a couple of pipes, which I used to communicate with nc . If you are interested in details, type help coproc , or read the section “Coprocesses” in man bash .

Bash has built-in tools for working with strings. It is not necessary to use sed , etc., to replace the substring with another string, calculate the length of the string, etc. This also became a useful discovery for me. If you are interested in the details, I can suggest the name of the corresponding section in man bash : “Parameter Expansion”.

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


All Articles