命令/别名可以变量? [英] Can a command/alias be variable?
问题描述
我需要不时查看日志文件的特定行时间:
$头-10的log.txt |尾-1#查看log.txt中的第10行
然后我写了一个函数 v
我的的.bashrc
来使生活更轻松:
$ V 10
好吧,也许我在这里我有点分裂的头发:我想忽略空间太:
$ V10
我知道的唯一方法是定义许多别名:
别名V1 ='V 1'
别名V2 ='V 2'
别名V3 ='V 3'
别名V4 ='V 4'
...
对此有什么好的办法?
感谢@Chirlo和@anishsane为理念。
下面是我的最终版本的基础上,@ anishsane与一些修正:
的eval`声明-f command_not_found_handle | sed的S / command_not_found_handle / command_not_found_handle_orig /`
command_not_found_handle(){
如果expr匹配$ 1V [0-9] [0-9] *>的/ dev / null的;然后
V $ {1:1}
返回$?
科幻
command_not_found_handle_orig$ @
}
这是不是一个答案,而是一个提示:
当你给Ubuntu的一些命令,&安培;如果未安装该程序,然后外壳调用自己的处理程序,并称
程序'东西'是当前未安装。您可以通过键入安装:
sudo易于得到安装的东西
您可以尝试找到那片程序,它这样做了。然后勾你的code,指令是否匹配的正则表达式这看起来 v [0-9] *
&安培;然后执行 V $ LINENUMBER
基于命令的一些解析...
基于chepner的评论
,这里是更新:
下面添加在你的.bashrc:
的eval`声明-f command_not_found_handle | sed的S / command_not_found_handle / command_not_found_handle_orig /`command_not_found_handle(){
如果expr匹配$ 1V [0-9] *>的/ dev / null的;然后
V $ {1:1}
返回$?
科幻
command_not_found_handle_orig$ [@]
}
I need to view a specific line of a log file from time to time:
$ head -10 log.txt|tail -1 # to view line 10 of log.txt
Then I wrote a function v
in my bashrc
to make life easier:
$ v 10
Ok, maybe I'm a little splitting hair here: I'd like to ignore the space too:
$ v10
The only way I know is to define lots of alias:
alias v1='v 1'
alias v2='v 2'
alias v3='v 3'
alias v4='v 4'
...
Is there any good way for this?
Thanks @Chirlo and @anishsane for the idea.
Here is my final version, based on @anishsane's with some fixes:
eval "`declare -f command_not_found_handle | sed s/command_not_found_handle/command_not_found_handle_orig/`"
command_not_found_handle(){
if expr match "$1" "v[0-9][0-9]*" >/dev/null ; then
v ${1:1}
return $?
fi
command_not_found_handle_orig "$@"
}
This is not an answer, but a hint:
When you give some command on ubuntu, & if that program is not installed, then the shell calls its own handler, saying
The program 'something' is currently not installed. You can install it by typing:
sudo apt-get install something
You can try finding that piece of program, which does this. Then hook your code, which would look if command is matching regex v[0-9]*
& then execute v $lineNumber
based on some parsing of command...
Based on chepner's comment, here is the update:
Add below in your .bashrc:
eval "`declare -f command_not_found_handle | sed s/command_not_found_handle/command_not_found_handle_orig/`"
command_not_found_handle(){
if expr match "$1" "v[0-9]*" >/dev/null ; then
v ${1:1}
return $?
fi
command_not_found_handle_orig "$[@]"
}
这篇关于命令/别名可以变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!