2

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.

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

Here is the code:

#!/bin/bash

if [ ! -z $1 ]; then
    for i in `seq 1 $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 $1 ]
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
2
  • @[A. Mesult Konuklar] I edited your question to rollback to previous edit: you may not remove the error from the initial question, otherwise you'll make the overall post unusable to others. Hope you got it by now, but just to explain. Commented Apr 30, 2020 at 5:50
  • @J.Chomel, I have no idea what I was doing at that time. :) Commented May 21, 2020 at 8:53

3 Answers 3

6

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

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
Sign up to request clarification or add additional context in comments.

11 Comments

Thank you, I just leanred [ meant test in shell. I put space between them normally , but tried without space also :)
You say variables should not be prefixed within $((...)), but both $(($i%3)) and $((i%3)) seem to return the same.
In bash, [ is a builtin, as shown by help [, and not as written here a link to the test command.
@gniourf_gniourf help [ prints This is a synonym for the "test" builtin, at least in bash.
Yes, you read properly: builtin !!! It's not the same as the command [ and test. which looks for commands in your path...
|
3

You should probably use something like :

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

instead of

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

2 Comments

Thank you, I though I've tried this method also, but I did it like $((i%3)) silly me. :)
Actually, as @sampson-chen pointed out in his solution, $((i%3)) works.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.