修复 Less 中的一个错误 通过记下 no 命令。 参数个数
这个问题基于此线程。
代码
function man()
{
man "$1" > /tmp/manual; less /tmp/manual
}
问题:如果我使用一个选项,该命令不知道想要的手册在哪里,
例如,
man -k find
给我一个错误,因为引用是错误的。 该命令读取 -k
作为手册。
我尝试用伪代码解决问题
if no parameters
run the initial code
if one parameter
run: man "$2"
...
换句话说,我们需要在开头添加一个选项检查,以便
伪代码
man $optional-option(s) "$n" > /tmp/manual; less /tmp/manual
where $n
- n=1 if 0 options
- n=2 if 1 option
- n=3 if 2 options
- ....
如何进行这样的“选项检查”以更改 $n 的值?
开发问题:针对从 n=1 到 n=2 的情况创建两个 if 循环
This question is based on this thread.
The code
function man()
{
man "$1" > /tmp/manual; less /tmp/manual
}
Problem: if I use even one option, the command does not know where is the wanted-manual
For instance,
man -k find
gives me an error, since the reference is wrong. The command reads -k
as the manual.
My attempt to solve the problem in pseudo-code
if no parameters
run the initial code
if one parameter
run: man "$2"
...
In other words, we need to add an option-check to the beginning such that
Pseudo-code
man $optional-option(s) "$n" > /tmp/manual; less /tmp/manual
where $n
- n=1 if zero options
- n=2 if 1 option
- n=3 if 2 options
- ....
How can you make such an "option-check" that you can alter the value of $n?
Developed Problem: to make two if loops for the situations from n=1 to n=2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
传递所有参数怎么样
?您在标题中提到的 less 中的错误是什么?
How about passing all the arguments
What is the bug in less which you mention in the title?
首先,您可以使用
$*
或$@
将函数的所有参数传递给man
。 您可以阅读man sh
了解两者之间差异的精确细节; 简而言之,几乎总是使用带有双引号的"$@"
。其次,临时文件是不必要的。 您可以通过将
man
的输出直接通过管道传输到less
来使这更清晰:顺便说一下,如果您只是尝试使用不同的寻呼机(
man
使用more
并且您想要更漂亮的less
),有一个公认的PAGER
环境变量,您可以设置它来覆盖默认值寻呼机。 例如,您可以将其添加到您的~/.bashrc
中,以告诉所有程序在显示多个输出屏幕时使用less
:要回答您的确切问题,您可以检查
$#
的参数数量:您可能还会发现
shift
命令很有帮助。 它将$2
重命名为$1
,将$3
重命名为$2
,依此类推。 它经常用在循环中来一一处理命令行参数:First, you can pass all of your function's arguments to
man
by using$*
or$@
. You can readman sh
for the precise details on the difference between the two; short story is to almost always use"$@"
with double quotes.Second, the temporary file is unnecessary. You could make this a little cleaner by piping the output of
man
directly toless
:By the way, if you're just trying to use a different pager (
man
usesmore
and you want the fancierless
) there's a commonly recognizedPAGER
environment variable that you can set to override the default pager. You could add this to your~/.bashrc
for instance to tell all programs to useless
when displaying multiple screens of output:To answer your precise question, you can check the number of arguments with
$#
:You might also find the
shift
command helpful. It renames$2
to$1
,$3
to$2
, and so on. It is often used in a loop to process command-line arguments one by one: