Shell 找出三个数字中最大的一个

Shell 找出三个数字中最大的一个,shell,unix,sh,Shell,Unix,Sh,编写一个shell脚本,找出三个数字中最大的一个。假设输入作为命令行参数给出,如果没有给出这三个数字,则显示错误消息为“缺少命令行参数” 我的脚本在这里,但是我得到了错误,因为所有的选项都是相等的和不相等的。。。请告诉我 if [ $# -eq 3 ]; then if [ $1 -eq $2 -eq $3 ]; then echo "All numbers are equal" elif [ $1 -eq $2 -ne $3 || $2 -eq $3 -ne $2

编写一个shell脚本,找出三个数字中最大的一个。假设输入作为命令行参数给出,如果没有给出这三个数字,则显示错误消息为“缺少命令行参数”

我的脚本在这里,但是我得到了错误,因为所有的选项都是相等的和不相等的。。。请告诉我

if [ $# -eq 3 ]; then
   if [ $1 -eq $2 -eq $3 ]; then
       echo "All numbers are equal"
   elif [  $1 -eq $2 -ne $3 ||  $2 -eq $3 -ne $2 ||  $1 -eq $3 -ne $2 ]; then
       echo "I can't figure out which number is greater"
   elif [ $1 -gt $3 && $1 -gt $3 ]; then
       echo "$1 is the biggest number"
   elif [ $2 -gt $3 ]; then
       echo "$2 is the biggest number"
   else
       echo "$3 is the biggest number"
   fi
else
   echo "command line args are missing"
fi

用于查找三个数是否相等以及三个数中最大的数

#!/bin/bash
echo ”enter 3 numbers”
read a
read b
read c
if [$a –eq $b –a $a –eq $c]
then
echo “3 number are equal”
elif [$a –gt $b –a $a –gt $c]
then
echo ”$a is the greatest number”
elif [$b –gt $c]
then
echo “$b is the greatest number”
else
echo “$c is the greatest number”
fi
意见:

  • 如果用户输入四个或更多数字,则会显示错误消息“命令” 行参数丢失“将是错误的
  • -eq
    语法不允许
    1-eq 2-eq 3
    。运行它会返回 “
    bash:[:参数太多
    ”错误。对于
    -ne
    ,情况也是如此
  • |
    应替换为
    -o
    ]|【
  • 这个测试不好:
    $1-gt$3&&$1-gt$3
    • 第一个
      $3
      应该是
      $2
    • &&
      应替换为
      ]&[
      -a
  • 的测试和输出“我不知道哪个数字更大”
    是不必要的。如果A=B和B>C,只需打印A

  • 您的两个主要问题是尝试链接
    -eq
    (您需要使用两个单独的
    [
    命令)和尝试在
    [
    内部使用
    |
    &&
    (同样,使用单独的命令)。您的第二种情况没有意义;整数的顺序很好,因此如果三者不相等,则将有一个最大值。您可以在
    $#
    检查中将
    -eq
    替换为
    -lt
    ,以忽略任何其他参数

    和往常一样,您应该引用参数扩展,以避免意外的参数值破坏代码

    if [ $# -lt 3 ]; then
       if [ "$1" -eq "$2" ] && [ "$2" -eq "$3" ]; then
           echo "All numbers are equal"
       elif [ "$1" -gt "$2" ] && [ "$1" -gt "$3" ]; then
           echo "$1 is the biggest number"
       elif [ "$2" -gt "$3" ]; then
           echo "$2 is the biggest number"
       else
           echo "$3 is the biggest number"
       fi
    else
       echo "command line args are missing"
    fi
    

    尝试使用以下脚本:

    #!/bin/bash
    if [ $# -eq 3 ]
    then
             if [ $1 -gt $2 ] && [ $1 -gt $3 ]
             then
             echo "$1 is Biggest number"
    
             elif [ $2 -gt $1 ] &&  [ $2 -gt $3 ]
             then
             echo "$2 is Biggest number"
    
             elif [ $3 -gt $1 ] && [ $3 -gt $1 ]
             then
             echo "$3 is Biggest number"
             fi
    else
    echo "command line arguments are missing"
    fi
    

    以下是正确答案:

    if [ $# -lt 2 ]
    then 
        echo "command line arguments are missing"
    elif [ $1 -eq $2 ]&&[ $2 -eq $3 ]
    then
        echo "All the three are equal"
    elif [ $1 -eq $2 ]||[ $1 -eq $3 ]
    then
        echo "I cannot figure out which number is biggest"
    elif [ $1 -gt $2 ]
    then
        if [ $1 -gt $3 ]
        then
            echo "$1 is biggest number"
        else
            echo "$3 is biggest number"
        fi
    elif [ $2 -gt $3 ]
    then
        echo "$2 is biggest number"
    else
        echo
    
     "$3 is biggest number"
    fi
    

    简单整洁的代码:

    #!/bin/bash
    #find largest among three
    read a
    read b
    read c
    if [ $a -eq $b ] && [ $b -eq $c ]
    then
    echo "All the three numbers are equal"
    elif [ $a -eq $b ] || [ $b -eq $c ] || [ $a -eq $c ]
    then
    echo "I cannot figure out which number is largest"
    else
    
    if [ $a -gt $b ] && [ $a -gt $c ]
    then
    echo "$a is largest number"
    elif [ $b -gt $c ] && [ $b -gt $a ]
    then
    echo "$b is largest number"
    else
    echo "$c is largest number"
    fi
    fi
    

    您听说过缩进吗?要进行调试,请使用-x运行它。还要添加
    echo$#
    echo“$@”
    到您的最后一个else分支。如果您得到这个结果,参数的数量明显不同于3。这方面存在许多问题,其中一些问题是由于从文字处理器复制和粘贴而出现的。请尽量避免将代码作为答案转储,并尝试解释它的作用和原因。您的代码可能对peo来说不明显没有相关编码经验的ple。请编辑您的答案以包括