Network naval battle on bash

    Recently, for the purpose of studying bash, he wrote on it a game in "sea battle" for playing on the network. The game is called "Sink 'em all."

    image

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

    The source code can be viewed here , but it’s better to download it here or here , because PasteBay inserts line breaks in the style of Windows, which, in turn, stops specifying the interpreter using hashbang ( #!).

    How to play


    Download the source code, rename the file to sink.shand put the executable bit on it. After that, the game can be safely launched (or not boldly - does it suddenly contain malicious code?). To start the server, use the key -serve:

    ./sink.sh -serve

    To connect to the 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 which place in the code to enter lines to deceive the enemy, I propose to find out on your own :)

    What useful have I learned?


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

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

    Also popular now: