Bourne Shell 条件
if [`read -n1 -s`='y']
正在引起
./bzfsctl.sh:第 17 行:[=y]:找不到命令
Even
if [1=1]
产生:
./bzfsctl.sh:第 17 行:[1=1]:找不到命令
编辑正确添加空格后得到
./bzfsctl.sh:第 16 行:[:-eq:需要一元运算符
符
if [ `read -n1 -s` = 'y' ]
then
echo 'killing process ...'
else
echo 'Aborted'
fi
if [`read -n1 -s`='y']
is causing
./bzfsctl.sh: line 17: [=y]: command not found
Even
if [1=1]
produces:
./bzfsctl.sh: line 17: [1=1]: command not found
EDIT After properly adding in the spaces get
./bzfsctl.sh: line 16: [: -eq: unary operator expected
with
if [ `read -n1 -s` = 'y' ]
then
echo 'killing process ...'
else
echo 'Aborted'
fi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要小心命令中的空格。
所有这四个空间都是必要的。
如果您想读取单个字符并测试它:
当您编写
if Something ;然后...
shell 执行某事
,然后根据该命令的返回代码进行操作。[
不是“语法”,它是一个程序(或内置的 shell),也称为test
。因此:
实际运行可执行文件(或内置)
[
,参数为$a
、=
、$b
和]
。如果不加括号,则需要
if
和;
之间的内容成为常规可执行命令,成功时返回 0。You need to be careful with the spaces in your commands.
All four of these spaces are necessary.
If you want to read a single char and test it:
When you write
if something ; then ...
the shell executessomething
and then acts depending on the return code of that command.[
isn't "syntax", it's a program (or shell built-in), that is also calledtest
.So:
actually runs the executable (or built-in)
[
with the arguments$a
,=
,$b
and]
.If you don't put the brackets, you need the thing between the
if
and;
to be a regular executable command that returns 0 on success.应该是这样的
如果你写成
if[1=1]
,那么shell解释器
就会将1
视为命令
, code> 所以你必须在 [ 之后和 ] 之前给空格
就像
if [ 1 = 1 ]
希望这会对你有所帮助。
It should be like this
if you write as
if[1=1]
then theshell interpreter
will consider1
ascommand
so you must givespace after [ and before ]
like
if [ 1 = 1 ]
hope the will help you.
如果您搜索 bash 手册页,则 read 命令会显示以下有关其返回值的内容。
所以它没有给你任何可以测试的东西。您可以传递这样的名称:
或者您可以使用内置的 REPLY 变量。
If you search the bash man page the read command says the folowing about its return value.
So its not giving you anything to test against. You could pass a name like this:
Or you could use the built-in REPLY variable.