为什么在 'if' 语句中,'then' 必须在 bash 的下一行? [英] why in an 'if' statement 'then' has to be in the next line in bash?

查看:37
本文介绍了为什么在 'if' 语句中,'then' 必须在 bash 的下一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if 后跟 then 在 bash 但我不明白为什么 then 不能像 一样在同一行中使用if [...] then 它必须在下一行中使用.这是否消除了代码中的一些歧义?或者 bash 就是这样设计的?其根本原因是什么?

if is followed by then in bash but I don't understand why then cannot be used in the same line like if [...] then it has to be used in the next line. Does that remove some ambiguity from the code? or bash is designed like that? what is the underlying reason for it?

我试图在同一行中编写 ifthen 但它给出了以下错误:

I tried to write if and then in the same line but it gave the error below:

./test: line 6: syntax error near unexpected token `fi'
./test: line 6: `fi'

代码是:

#!/bin/bash
if [ $1 -gt 0 ] then
echo "$1 is positive" 
fi

推荐答案

前面必须有一些描述的分隔符,而不是一定在下一行(a).换句话说,要实现你想要的,你可以简单地使用:

It has to be preceded by a separator of some description, not necessarily on the next line(a). In other words, to achieve what you want, you can simply use:

if [[ $1 -gt 0 ]] ; then
    echo "$1 is positive"
fi

顺便说一句,对于这样的单线,我倾向于:

As an aside, for one-liners like that, I tend to prefer:

[[ $1 -gt 0 ]] && echo "$1 is positive"

但这仅仅是因为我更喜欢在屏幕上看到尽可能多的代码.这真的只是一种风格,你可以随意忽略.

But that's simply because I prefer to see as much code on screen as possible. It's really just a style thing which you can freely ignore.

(a) 原因可以在 Bash 手册页中找到(我的重点):

(a) The reason for this can be found in the Bash manpage (my emphasis):

保留字:保留字是对外壳具有特殊含义的字.以下单词在不加引号时被识别为保留字,简单命令的第一个单词(参见下面的 SHELL GRAMMAR)或 case 的第三个单词code> 或 for 命令:

RESERVED WORDS: Reserved words are words that have a special meaning to the shell. The following words are recognized as reserved when unquoted and either the first word of a simple command (see SHELL GRAMMAR below) or the third word of a case or for command:

<代码>!case coproc do done elif else esac fi for function if in select then until while { } time [[ ]]

请注意,尽管该部分声明它是简单命令的第一个单词",但手册页似乎与引用的 SHELL GRAMMAR 部分中的自相矛盾:

Note that, though that section states it's the "first word of a simple command", the manpage seems to contradict itself in the referenced SHELL GRAMMAR section:

一个简单的命令是一系列可选的变量赋值,后跟空格分隔的单词和重定向,并由控制运算符终止.第一个字指定要执行的命令,并作为参数零传递.

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. The first word specifies the command to be executed, and is passed as argument zero.

因此,您是否将其视为下一个命令的一部分或某种分隔符是有争议的.不可 有争议的是,它需要在 then 关键字之前使用某种分隔符(例如换行符或分号).

So, whether you consider it part of the next command or a separator of some sort is arguable. What is not arguable is that it needs a separator of some sort (newline or semicolon, for example) before the then keyword.

联机帮助页没有说明为什么它是这样设计的,但可能是为了使命令的解析更简单.

The manpage doesn't go into why it was designed that way but it's probably to make the parsing of commands a little simpler.

这篇关于为什么在 'if' 语句中,'then' 必须在 bash 的下一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆