Bourne Shell 条件

发布于 2024-11-30 16:30:17 字数 483 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

物价感观 2024-12-07 16:30:17

您需要小心命令中的空格。

if [ 1 = 1 ]; then echo Ok ; fi
    ^ ^ ^ ^

所有这四个空间都是必要的。

如果您想读取单个字符并测试它:

read -n1 input
if [ $input = "y" ] ; then echo Got a Yes ; fi

当您编写 if Something ;然后... shell 执行某事,然后根据该命令的返回代码进行操作。

[ 不是“语法”,它是一个程序(或内置的 shell),也称为 test

因此:

if [ $a = $b ] ; then ...

实际运行可执行文件(或内置)[,参数为$a=$b]

如果不加括号,则需要 if; 之间的内容成为常规可执行命令,成功时返回 0。

You need to be careful with the spaces in your commands.

if [ 1 = 1 ]; then echo Ok ; fi
    ^ ^ ^ ^

All four of these spaces are necessary.

If you want to read a single char and test it:

read -n1 input
if [ $input = "y" ] ; then echo Got a Yes ; fi

When you write if something ; then ... the shell executes something 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 called test.

So:

if [ $a = $b ] ; then ...

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.

痴梦一场 2024-12-07 16:30:17

应该是这样的

if [ 1 = 1 ]; then
echo "equal";
else
echo "not-equal";
fi

如果你写成if[1=1],那么shell解释器就会将1视为命令, code> 所以你必须在 [ 之后和 ] 之前给 空格

就像 if [ 1 = 1 ]

希望这会对你有所帮助。

It should be like this

if [ 1 = 1 ]; then
echo "equal";
else
echo "not-equal";
fi

if you write as if[1=1] then the shell interpreter will consider 1 as command so you must give space after [ and before ]

like if [ 1 = 1 ]

hope the will help you.

回忆那么伤 2024-12-07 16:30:17

如果您搜索 bash 手册页,则 read 命令会显示以下有关其返回值的内容。

read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]


          If  no  names are supplied, the line read is assigned to the variable REPLY.  The return code
          is zero, unless end-of-file is encountered, read times out (in which case the return code  is
          greater than 128), or an invalid file descriptor is supplied as the argument to -u.

所以它没有给你任何可以测试的东西。您可以传递这样的名称:

read -n 1 YesNo
if [ $YesNo = 'Y' ] ; then
   echo 'Yes'
else
   echo 'No'
fi

或者您可以使用内置的 REPLY 变量。

If you search the bash man page the read command says the folowing about its return value.

read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]


          If  no  names are supplied, the line read is assigned to the variable REPLY.  The return code
          is zero, unless end-of-file is encountered, read times out (in which case the return code  is
          greater than 128), or an invalid file descriptor is supplied as the argument to -u.

So its not giving you anything to test against. You could pass a name like this:

read -n 1 YesNo
if [ $YesNo = 'Y' ] ; then
   echo 'Yes'
else
   echo 'No'
fi

Or you could use the built-in REPLY variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文