在bash脚本中使用条件来检查字符串参数

时间:2021-09-02 00:08:08

I am trying to write my shell script thing.sh so that upon making it an executable and running it with the single letter ``A" like so:

我在试着写我的shell脚本。在使它成为可执行文件并使用单字母“A”运行时:

$ ./thing.sh A

I get the output

我得到的输出

A 

If argument 1 is not A, I want the output

如果参数1不是A,我想要输出

Not A 

Here is my code so far :

这是我目前的代码:

#!/bin/bash

if [ "$1" -eq "A"]
then echo "A"
else echo "Not A"
fi 

which returns, no matter what I enter,

不管我输入什么,

./thing.sh: line 3: [:missing `]'
Not A

I am trying what I hoped would check something with one or several letters and compare it against the letter A; could someone tell me what I am missing to get this to work? Thank you

我正在尝试用一个或几个字母来检查某样东西,并将它与字母A进行比较;谁能告诉我我缺少什么才能让这个工作?谢谢你!

4 个解决方案

#1


17  

What about the shorter :

那矮的呢?

#!/bin/bash

[[ $1 == A ]] && echo "A" || echo "not A"

?

吗?

And a beginner version (identical logic) :

和初学者版本(相同逻辑):

#!/bin/bash

if [[ $1 == A ]]; then
    echo "A"
else
    echo "not A"
fi

Like Scott said, you have a syntax error (missing space).

正如Scott所说,您有一个语法错误(缺少空格)。

explanations

  • I use boolean logic here. [[ $1 == A ]] is executed, and then if its true, echo "A" is executed, and if it's false, echo "not A" is executed, See http://mywiki.wooledge.org/BashGuide/TestsAndConditionals
  • 这里我用布尔逻辑。[[$1 == A]]被执行,如果是true,则执行echo“A”,如果是false,则执行echo“not A”,请参见http://mywiki.wooledge.org/BashGuide/TestsAndConditionals。
  • [[ is a bash keyword similar to (but more powerful than) the [ command. See http://mywiki.wooledge.org/BashFAQ/031 and http://mywiki.wooledge.org/BashGuide/TestsAndConditionals Unless you're writing for POSIX sh, I recommend [[.
  • [[是与[命令]相似(但比[命令更强大)的bash关键字。请参阅http://mywiki.wooledge.org/BashFAQ/031和http://mywiki.wooledge.org/BashGuide/TestsAndConditionals,除非您为POSIX sh编写,否则我推荐[[[]。

#2


4  

Change the first line to:

将第一行更改为:

if [ "$1" == "A" ]

The -eq operator is for integers. And as someone else mentioned, the space does matter before the ']'.

-eq算符是整数。正如有人提到的,空间在"]"之前确实很重要。

See here: http://tldp.org/LDP/abs/html/comparison-ops.html

在这里看到的:http://tldp.org/LDP/abs/html/comparison-ops.html

#3


3  

You need a space after "A"

" a "之后需要一个空格

if [ "$1" -eq "A" ]

#4


2  

Try putting a space between "A" and ].

试着在“a”和“]”之间留个空格。

#1


17  

What about the shorter :

那矮的呢?

#!/bin/bash

[[ $1 == A ]] && echo "A" || echo "not A"

?

吗?

And a beginner version (identical logic) :

和初学者版本(相同逻辑):

#!/bin/bash

if [[ $1 == A ]]; then
    echo "A"
else
    echo "not A"
fi

Like Scott said, you have a syntax error (missing space).

正如Scott所说,您有一个语法错误(缺少空格)。

explanations

  • I use boolean logic here. [[ $1 == A ]] is executed, and then if its true, echo "A" is executed, and if it's false, echo "not A" is executed, See http://mywiki.wooledge.org/BashGuide/TestsAndConditionals
  • 这里我用布尔逻辑。[[$1 == A]]被执行,如果是true,则执行echo“A”,如果是false,则执行echo“not A”,请参见http://mywiki.wooledge.org/BashGuide/TestsAndConditionals。
  • [[ is a bash keyword similar to (but more powerful than) the [ command. See http://mywiki.wooledge.org/BashFAQ/031 and http://mywiki.wooledge.org/BashGuide/TestsAndConditionals Unless you're writing for POSIX sh, I recommend [[.
  • [[是与[命令]相似(但比[命令更强大)的bash关键字。请参阅http://mywiki.wooledge.org/BashFAQ/031和http://mywiki.wooledge.org/BashGuide/TestsAndConditionals,除非您为POSIX sh编写,否则我推荐[[[]。

#2


4  

Change the first line to:

将第一行更改为:

if [ "$1" == "A" ]

The -eq operator is for integers. And as someone else mentioned, the space does matter before the ']'.

-eq算符是整数。正如有人提到的,空间在"]"之前确实很重要。

See here: http://tldp.org/LDP/abs/html/comparison-ops.html

在这里看到的:http://tldp.org/LDP/abs/html/comparison-ops.html

#3


3  

You need a space after "A"

" a "之后需要一个空格

if [ "$1" -eq "A" ]

#4


2  

Try putting a space between "A" and ].

试着在“a”和“]”之间留个空格。