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

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

问题描述

if后跟bash中的then,但我不明白为什么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) sup>.换句话说,要实现您想要的目标,只需使用:

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具有特殊含义的字.以下单词在不加引号的情况下被识别为保留,并且是 简单命令的第一个单词 (请参阅下面的SHELL GRAMMAR)或case命令:

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天全站免登陆