bash 比较shell脚本中的两个数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33194108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare two numbers in shell script
提问by MO.
Write a Bash shell script “order.sh” that takes two integer arguments “a” and “b”, and prints out the appropriate relationship “a < b”, “a == b” or “a > b” (with “a” and “b” replaced by their values).
编写一个 Bash shell 脚本“order.sh”,它接受两个整数参数“a”和“b”,并打印出适当的关系“a < b”、“a == b”或“a > b”(带有“ a”和“b”替换为它们的值)。
code:
代码:
#!/bin/bash
echo -n "enter the first number:"; read x
echo -n " enter the second number:"; read y
if ["$x " -lt "$y"]
then
echo "$x < $y"
else
echo"$y < $x"
if [ "$x" -eq "$y"]
then
echo " $x == $y "
fi
i am not being able to compile his code as it is failing and says "/bin/sh: make command not found" can someone tell me what this means? i am new to shell scripting and i have no idea what is the problem...
我无法编译他的代码,因为它失败并说“/bin/sh: make command not found”有人能告诉我这是什么意思吗?我是 shell 脚本的新手,我不知道有什么问题...
回答by janos
i am not being able to compile his code as it is failing and says "/bin/sh: make command not found" can someone tell me what this means? i am new to shell scripting and i have no idea what is the problem...
我无法编译他的代码,因为它失败并说“/bin/sh: make command not found”有人能告诉我这是什么意思吗?我是 shell 脚本的新手,我不知道有什么问题...
Several problems in that statement:
该声明中有几个问题:
- "compile this code" ... a Bash script doesn't need to be compiled. Bash is an interpreted language
- "/bin/sh: make command not found" means exactly what it looks like: the
make
command is not found. You don't have amake
command on yourPATH
. But it doesn't matter, because you don't needmake
here
- “编译此代码”……不需要编译 Bash 脚本。Bash 是一种解释型语言
- “/bin/sh: make command not found”的意思就是它的样子:
make
找不到命令。您make
的PATH
. 不过没关系,因为你不需要make
这里
Your script has syntax errors, for example:
您的脚本有语法错误,例如:
if ["$x " -lt "$y"]
if ["$x " -lt "$y"]
You need to put a space after [
and before ]
, like this:
您需要在[
前后放置一个空格]
,如下所示:
if [ "$x " -lt "$y" ]
Other problems:
其他问题:
- Not using
if-elif-else
for the 3 cases - Broken conditions: there are 2
if
but only 1 closingfi
- 不
if-elif-else
用于 3 种情况 - 破坏条件:有 2
if
但只有 1 个关闭fi
A few other tips:
其他一些提示:
- For doing arithmetic in Bash, use
((...))
instead of[...]
. - Instead of
echo -n; read
, useread -p
: it's one command instead of two, and the flags ofecho
are not portable, so it's better to avoid using them - Indent the content of
if-elif-else
to make the script easier to read
- 要在 Bash 中进行算术运算,请使用
((...))
代替[...]
。 - 而不是
echo -n; read
,使用read -p
:它是一个命令而不是两个命令,并且 的标志echo
不可移植,因此最好避免使用它们 - 缩进 的内容
if-elif-else
以使脚本更易于阅读
With the corrections and improvements applied:
应用更正和改进后:
#!/usr/bin/env bash
read -p "enter the first number: "
read -p "enter the second number: "
if ((x < y)); then
echo "$x < $y"
elif ((x > y)); then
echo "$y < $x"
else
echo "$x == $y"
fi
回答by Elliott Frisch
You should probably use /usr/bin/env
to find bash
. I think you also wanted an elif
(and an else
- your current one is missing a fi
and won't test for greater than), and you should be using [[
and ]]
(also you missed a space with echo"$y < $x"
). Something like,
您可能应该使用/usr/bin/env
find bash
。我认为您还想要一个elif
(并且一个else
- 您当前的一个缺少一个fi
并且不会测试大于),并且您应该使用[[
and ]]
(您也错过了一个带有 的空格echo"$y < $x"
)。就像是,
#!/usr/bin/env bash
echo -n "enter the first number:"; read x
echo -n "enter the second number:"; read y
if [[ "$x" -lt "$y" ]]; then
echo "$x < $y"
elif [[ "$x" -gt "$y" ]]; then
echo "$y < $x"
else
echo "$x == $y"
fi
Which I tested, and does as you would expect. I suggest you see 7.02. More advanced if usage - Bash Beginner's Guide
我测试过,并且按您的预期执行。我建议你看7.02。更高级的 if 用法 - Bash 初学者指南