bash - 计算传递给 bash 脚本的最大参数

标签 bash unix terminal

我正在尝试计算传递给 bash 脚本的最大参数。代码如下:

#!/bin/sh

max=$1

for var in "$@"
do
    if ($var>$max)
    then
        max=$var
    fi
done

echo $max

这是我得到的:

$ /bin/sh my_script 1 2 3
rgz: 11: 1: not found
rgz: 11: 2: not found
rgz: 11: 3: not found
1

我做错了什么?

最佳答案

这可能对你有用 -

#!/bin/bash

max="$1"
for var in "$@"
do
    if [ "$var" -gt "$max" ] # Using the test condition
    then
        max="$var"
    fi
done
echo "$max"

关于bash - 计算传递给 bash 脚本的最大参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8626512/

相关文章:

bash - “docker run”命令来评估容器内的bash $ variable

bash - "-ne"在 bash 中是什么意思?

linux - 使用批处理脚本从Windows检查Linux服务器中的服务

linux - 使用 gnome 终端获取新选项卡

linux - 使用多个命令管道

linux - BASH 脚本如何在不使用 sudoers nopasswd 选项的情况下自动提升到远程服务器上的根目录?

linux - 在redhat中安装namp时出错

linux - 从文件名中删除前导和尾随空格

mysql - 坚持拒绝用户访问 'root' @'localhost' - 终端,Mac

bash - 如何在 mac 上删除文件名第一个点之后的所有部分