Não, você não pode usar o comando regex in alias.
Para conseguir o que você deseja, no Bash, você pode usar o command_not_found_handle
. Do Manual de referência :
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.
Você pode fazer algo assim:
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
}
Agora, se você estiver em um ambiente em que command_not_found_handle
já esteja definido, por exemplo, no Ubuntu, você sabe, a mensagem legal como
The program 'whatever' can be found in the following packages:
* whatever
* whatever-ever
Try: sudo apt-get install <selected package>
é muito provável que você não queira substituir isso. Você pode corrigir a função como segue (no seu .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