bash 用于 if 条件的 Shell 脚本过多参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13405893/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:48:25  来源:igfitidea点击:

Shell Script Too Many Arguments for if condition

bashshellterminal

提问by A. Mesut Konuklar

My current script does the following;

我当前的脚本执行以下操作;

It takes integer as a command line argument and starts from 1 to N , it checks whether the numbers are divisible by 3, 5 or both of them. It simply prints out Uc for 3, Bes for 5 and UcBes for 3,5. If the command line argument is empty, it does the same operation but the loop goes to 1 to 20.

它接受 integer 作为命令行参数并从 1 到 N 开始,它检查数字是否可以被 3、5 或两者整除。它只是打印出 3 的 Uc、5 的 Bes 和 3,5 的 UcBes。如果命令行参数为空,它会执行相同的操作,但循环会转到 1 到 20。

I am having this error "Too many arguments at line 11,15 and 19".

我有这个错误“第 11,15 和 19 行参数太多”。

Here is the code:

这是代码:

#!/bin/bash

if [ ! -z  ]; then
    for i in `seq 1 `
    do
        if [ [$i % 3] -eq 0 ]; then
            echo "Uc"
        elif [ i % 5 -eq 0 ]; then
            echo "Bes"
        elif [ i % 3 -eq 0 ] && [ i % 5 -eq 0?]
        then
            echo "UcBes"
        else
            echo "$i"
        fi
    done
elif [ -z  ]
then
    for i in {1..20}
    do
        if [ i % 3 -eq 0 ]
        then
            echo "Uc"
        elif [ i % 5 -eq 0 ]
        then
            echo "Bes"
        elif [ i % 3 -eq 0 ] && [ i % 5 -eq 0?]
        then
            echo "UcBes"
        else
            echo "$i"
        fi
    done
else
    echo "heheheh"
fi

回答by sampson-chen

Note that [is actually synonym for the testbuiltin in shell (try which [in your terminal), and not a conditional syntax like other languages, so you cannot do:

请注意,这[实际上是testshell 中内置函数的同义词(which [在您的终端中尝试),而不是像其他语言那样的条件语法,因此您不能这样做:

if [ [$i % 3] -eq 0 ]; then

Moreover, always make sure that there is at least one space between [, ], and the variables that comprise the logical condition check in between them.

此外,始终确保有之间至少有一个空格[]以及包含在它们之间的逻辑条件检查的变量。

The syntax for evaluating an expression such as modulo is enclosure by $((...)), and the variable names inside need not be prefixed by $:

求值表达式(例如模数)的语法是由 包围$((...)),并且其中的变量名称不需要以 为前缀$

remainder=$((i % 3))
if [ $remainder -eq 0 ]; then

回答by Julien Vivenot

You should probably use something like :

你可能应该使用类似的东西:

if [ $(($i % 3)) -eq 0 ]

instead of

代替

if [ $i % 3 -eq 0 ]
if [ [$i % 3] -eq 0 ]

回答by William Pursell

Your script could be greatly simplified. For example:

您的脚本可以大大简化。例如:

#!/bin/sh

n=0
while test $(( ++n )) -le ${1:-20}; do
  t=$n
  expr $n % 3 > /dev/null || { printf Uc; t=; }
  expr $n % 5 > /dev/null || { printf Bes; t=; }
  echo $t
done

gives slightly different error messages if the argument is not an integer, but otherwise behaves the same.

如果参数不是整数,则给出略有不同的错误消息,否则行为相同。