linux 命令两个冒号,为什么两个冒号的错误消息作为bash中的命令(::)有三个冒号,但是一个冒号没有输出?...

问题描述

如果我输入

::

进入bash shell,我得到:

-bash: ::: command not found

但是,只有一个:没有输出。为什么是这样?

最佳解决方案

:壳内置与non-existent ::

存在: shell 内置 command(注意difference between external and 内置 commands),它什么都不做;它只是返回成功,就像true命令一样。 : 内置是标准的defined by the POSIX standard,它也被称为”null utility”。它经常用于测试或运行无限循环,如while : ; do ...;done中所示

bash-4.3$ type :

: is a shell builtin

但是,:: – 两个冒号字符在一起 – 被解释为shell的一个”word”,它假定是用户输入的命令。 shell将经历检查内置s的过程,然后检查PATH变量中是否存在该命令。但既没有内置 ::也没有外部命令::。因此,这会产生错误。

那么,错误的典型格式是什么?

: : error message

因此,您看到的不是3个冒号,而是您键入的内容粘贴到标准错误格式中。

另请注意,:可以采用命令行参数,即执行以下操作是合法的:

: :

在这种情况下,shell会将其视为两个”words”,其中一个是命令而另一个是位置参数。这也不会产生错误! (另请参阅有关使用:和位置参数的历史记录(本回答后面)。)

在bash以外的shell中

请注意,格式也可能因不同的shell而异。对于bash,ksh和mksh,行为是一致的。例如,Ubuntu的默认/bin/sh shell(实际上是/bin/dash):

$ dash

$ ::

dash: 1: ::: not found

其中1是命令编号(相当于脚本中的行号)。

相比之下,csh根本不会产生任何错误消息:

$ csh

% ::

%

实际上,如果运行strace -o csh.trace csh -c ::,则csh.trace文件中的跟踪输出显示csh退出且退出状态为0(无错误)。但是tcsh会输出错误(但不输出其名称):

$ tcsh

localhost:~> ::

::: Command not found.

错误消息

通常,错误消息中的第一项应该是执行进程或函数(您的shell尝试执行::,因此错误消息来自shell)。例如,这里的执行过程是stat:

$ stat noexist

stat: cannot stat 'noexist': No such file or directory

实际上,POSIX定义了perror()函数,该函数根据文档采用字符串参数,然后在冒号后输出错误消息,然后输出换行符。引用:

The perror() function shall map the error number accessed through the symbol errno to a language-dependent error message, which shall be written to the standard error stream as follows:

First (if s is not a null pointer and the character pointed to by s is not the null byte), the string pointed to by s followed by a colon and a .

Then an error message string followed by a .

perror()的字符串参数在技术上可以是任何东西,但当然为了清楚起见,它通常是函数名称或argv[0]。

fprintf (stderr, "%s: Couldn't open file %s; %s\n",

program_invocation_short_name, name, strerror (errno));

历史记录

在旧的Unix和Thompson shell中,:与goto语句一起使用(根据用户名为Perderabo的this thread不是shell 内置)。从手册中引用:

The entire command file is searched for a line beginning with a : as the first non-blank character, followed by one or more blanks, and then the label. If such a line is found, goto repositions the command-file offset to the line after the label and exits. This causes the shell to transfer to the labelled line.

所以你可以做这样的事情来制作一个无限循环脚本:

: repeat

echo "Hello World"

goto repeat

次佳解决方案

最后一个冒号只是默认”not found”消息的一部分:

$ x

x: command not found

$ ::

::: command not found

单个冒号没有产生任何东西的原因是:是一个有效的命令 – 虽然它什么都不做(除了返回TRUE)。来自man bash的SHELL BUILTIN COMMANDS部分:

: [arguments]

No effect; the command does nothing beyond expanding arguments

and performing any specified redirections. A zero exit code is

returned.

你有时会在像这样的结构中看到它

while :

do

something

done

第三种解决方案

尝试任何其他non-existent命令,您将看到:以英语服务于其正常用途:

$ ---

---: command not found

第四种方案

添加的冒号是错误消息本身的一部分。如果一个类型cd ow它导致bash: cd: ow: No such file or directory,这表明错误是放入额外结肠: No such file or directory

第五种方案

$ ::

bash: ::: command not found

$ kkkk

bash: kkkk: command not found

第三个是格式化的间隔

在bash中,:是一个空行void指令

第六种方案

你得到3个冒号,因为错误格式包含冒号:

bash: : command not found

参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值