Can we use regex in alias command?

1

I want to create the alias for this command. ssh user@ip as

 alias [0-9][0-9][0-9].[0-9][0-9][0-9].[0-9][0-9][0-9].[0-9][0-9][0-9] ="ssh user@$0"

I have created it ,but it is not working. Is there any syntax error in this command.

KALAI SELVAN

Posted 2014-11-02T04:57:10.620

Reputation: 119

Answers

1

No you can't use regex in alias command.

To achieve what you want, in Bash, you can use the command_not_found_handle. From the Reference Manual:

If the name is neither a shell function nor a builtin, and contains no slashes, Bash searches each element of $PATH for a directory containing an executable file by that name. Bash uses a hash table to remember the full pathnames of executable files to avoid multiple PATH searches (see the description of hash in Bourne Shell Builtins). A full search of the directories in $PATH is performed only if the command is not found in the hash table. If the search is unsuccessful, the shell searches for a defined shell function named command_not_found_handle. If that function exists, it is invoked with the original command and the original command’s arguments as its arguments, and the function’s exit status becomes the exit status of the shell. If that function is not defined, the shell prints an error message and returns an exit status of 127.

You can then do something like this:

command_not_found_handle() {
    local i ip ip_ok=0
    if [[ $1 = +([[:digit:]]).+([[:digit:]]).+([[:digit:]]).+([[:digit:]]) ]]; then
        IFS=. read -a ip <<< "$1"
        ip_ok=1
        for i in "${ip[@]}"; do
            [[ $i = $((10#$i)) ]] && (( i>=0 && i<=255 )) || { ip_ok=0; break; }
        done
    fi
    if ((ip_ok)); then
        ssh "user@$1"
    else
        ( unset -f command_not_found_handle; "$@" )
    fi
}

Now if you are in an environment where command_not_found_handle is already defined, e.g., on Ubuntu, you know, the cool message like

The program 'whatever' can be found in the following packages:
 * whatever
 * whatever-ever
Try: sudo apt-get install <selected package>

you very likely don't want to override this. You can monkey-patch the function as follows (in your .bashrc):

# Guard to not monkey-patch twice, in case this file is sourced several times
if ! declare -f kalai_command_not_found_existent &>/dev/null; then
    mapfile -s1 -t kalai_command_not_found_existent_ary < <(declare -f command_not_found_handle 2>/dev/null)
    if ((${#kalai_command_not_found_existent_ary[@]})); then
        eval "$(printf '%s\n' "kalai_command_not_found_existent ()" "${kalai_command_not_found_existent_ary[@]}")"
    else
        kalai_command_not_found_existent() { ( unset -f command_not_found_handle; "$@" ) }
    fi
    command_not_found_handle() {
        local i ip ip_ok=0
        if [[ $1 = +([[:digit:]]).+([[:digit:]]).+([[:digit:]]).+([[:digit:]]) ]]; then
            IFS=. read -a ip <<< "$1"
            ip_ok=1
            for i in "${ip[@]}"; do
                [[ $i = $((10#$i)) ]] && (( i>=0 && i<=255 )) || { ip_ok=0; break; }
            done
        fi
        if ((ip_ok)); then
            ssh "user@$1"
        else
            kalai_command_not_found_existent "$@"
        fi
    }
    unset command_not_found_existent_ary
fi

gniourf_gniourf

Posted 2014-11-02T04:57:10.620

Reputation: 1 882

0

No, you can't use that way.

With an alias you could sort a command when you type it. For example:

alias ls="ls -lh"

When you type ls, the system knows that you are saying you want to do ls -lh.

In your case, if you want to maintain ssh user@ to only type the server name, a better approach is to declare something like this:

alias myssh="ssh -l user"

Then when you do myssh server what you are really doing is ssh -l user server.

jherran

Posted 2014-11-02T04:57:10.620

Reputation: 1 693