Using bash completion on the command line, native scripts and applications. Part 1

    Part 2 The

    system administrator is a lazy person. Usually he tries to do the maximum with minimal effort, and this often requires automation of many routine procedures. For example, a set of full hostnames when creating an ssh connection, or the long arguments of some commands :)

    Today I will try to talk about using such a shell functionality as bash completion.

    So, almost any shell by default can add paths to files and directories: bash (in my case) after pressing Tab will append the name of the directory (if it can be determined uniquely by the first characters typed), or show options: But at the same time, some shells can complement not only the paths, but also the arguments for a number of teams. For instance:

    root@mould:~# mkdir very_long_dir_name
    root@mould:~# cd ve[Tab]
    root@mould:~# cd very_long_dir_name/




    root@mould:~# ls .s[Tab]
    .ssh/ .subversion/




    root@mould:~# apt-get up[Tab]
    update upgrade


    Or even more complex constructions: The contents of the bash-completion package are responsible for this functionality in debain-based distributions (I can’t say anything about the rest). In order to activate completion capabilities, it is enough to do the following: Or add such a call to your .bashrc file, and then log in: Most likely, this is already written to you, but commented out. Let's try to start using one of the most frequently required compliments - adding hostnames when accessing them via ssh. First you need to disable hashing host names in ~ / .ssh / known_hosts. When the "box" configuration line in this file looks like this: , it does not suit us. After setting the value

    root@mould:~# apt-get install bash-[Tab]
    bash-builtins bash-completion bash-doc bash-minimal bash-static





    root@mould:~# . /etc/bash_completion



    if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
    fi






    |1|yVV33HmBny6RPYWkUB5aW+TksqQ=|f11ZL/FI9/Krfw2bqN0tBJeeq4w= ssh-rsa AAAAB3Nz__много-много-символов__2bYw==


    HashKnownHosts No in the ssh client config file (/ etc / ssh / ssh_config or ~ / .ssh / config), and cleaning up .ssh / known_hosts (otherwise, the correct lines will be added only for new hosts) we will get a readable version of the entry in known_hosts after the first attempt to login to the host:

    mould,11.22.33.44 ssh-rsa AAAAB3NzaC1y__много-много-символов__c2EAANq6/Ww==.

    And this, in turn, will allow you to use the compliant of names when setting up an ssh connection: If you have more than 5-10 machines in your “submission” it will be very convenient. And by the way, the add-on will work not only for ssh, but also for a number of other programs: ping, traceroute, telnet, etc. And not only by hostname, but also by ip-address. If this topic turns out to be interesting, in the next part I can tell you how to configure the addition of commands and arguments for your own scripts. For example, like this:

    veshij@dhcp250-203:~ $ ssh mould[Tab]
    mould mould01e







    root@mould:~# my_test_script --[Tab]
    --help --kill-all-humans --conquer-the-world



    Also popular now: