0

I coded code that print decimals in 1 to 100. and I executed this code, but Nothing happens... I want to know what's the problem in my code

Please understand with my not good English

enter image description here

#!/bin/sh
i=2

while [ $i -le 100]
do
    c=0
    j=1

    while [ $j -lt $i ]
    do
            if [ $(( $i % $j )) -eq 0 ]
            then
                    c=$(( $c+1 ))
            fi
            j=$(( $j+1 ))

    if [ $c -eq 0 ]
    then
            echo i
    fi
    done

    i=$(( $i+1 ))
done
rohan
  • 1
  • 1
  • 1
    Welcome to Stack Overflow. Please read the [About](http://stackoverflow.com/tour) page soon and also visit the links describing [How to Ask a Question](http://stackoverflow.com/questions/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Further, posting code with obscenities may seem like a cute thing to do.... until your prospective employer is holding your code example in their folder at the time of your interview... Use good judgment. – David C. Rankin May 14 '20 at 07:36
  • Just to be sure and also help you about your algorithm, what exactly are you trying to do? If I would guess, I'd say you are trying to get all dividers of all numbers between 2 and 100. Is that it? – Stéphane Veyret May 14 '20 at 09:44
  • @rohan : What did you expect to happen? You have exactly one statement, which would show an outcome (`echo i`). This would print the letter `i` iff `c` equals 0. But since the loop runs from 2 to 100, for each iteration `$i % $j` would be zero at least once. Hence `c` will always be larger than zero, and nothing will be printed. – user1934428 May 14 '20 at 10:20

3 Answers3

1

As shown by StephaneVeyret, the shebang shouldn't have spaces .

The purpose of the script seems to be printing numbers sequentially. If that is the case, you might want to take a look at a simple code like this :

#!/bin/bash
for i in {1..100}
do
    printf "$i\n"
done
schegu
  • 31
  • 8
0

There should not be space in your shebang, between ! and /bin/sh. The first line of your script should be:

#!/bin/sh

(instead of #! /bin/sh)

Also, please note that if you really want to use Bourne shell, it should actually be:

#!/bin/bash

EDIT1: more problems:

As you want to use bash, you'd better use double brackets in your conditional statement, i.e.:

if [[ $j -le $i ]]

because they are more “powerful” in bash.

For this particular expression, you could also do:

if (( j <= i ))

Now, be careful to put spaces after [[ and (( and before ]] and )), and around operators:

$j=$(( $i + 1 ))

There seem to also be algo problems, but I don't have time to re-create your script and will check them only if I can copy-paste on my computer. Can you please paste an updated version of your script in your question? That would be easier for us than working on an image.

EDIT2: re your last post:

Line 4: there is a missing space before the closing bracket:

while [ $i -le 100 ]

Now, if you want to know what the script is doing, you can add the following command, for example, just after the shebang:

set -x
Stéphane Veyret
  • 1,791
  • 1
  • 8
  • 15
  • Oh Thanks for that! but the problem still going on. – rohan May 14 '20 at 08:11
  • I have to work on /bin/sh. and I updated post. I appreciate about your support. – rohan May 14 '20 at 09:23
  • Then you should update your post title, because you are talking about “bourne shell”, which is `bash`. – Stéphane Veyret May 14 '20 at 09:27
  • I'm confused now. I've only been studying Linux for a few days. The book I study reads 'Bourne shell(/bin/sh) Bash shell(/bin/bash). My assignment description says I should write it in Bourne shell. Now I don't know what to do. – rohan May 14 '20 at 09:35
  • Well, actually `/bin/sh` will use the default shell in your computer, which nowadays is `bash` most of the cases. But you should not rely on that, and because you are using bash-specific things (like the double parenthesis, if I am not mistaken), you should specify `/bin/bash` in your shebang. – Stéphane Veyret May 14 '20 at 09:40
  • (just to be precise, Bourne shell is `bsh`, but it is almost never used today in favor of Bourne Again shell, which is `bash`) – Stéphane Veyret May 14 '20 at 09:42
  • For more information, see this post: https://stackoverflow.com/questions/5725296/difference-between-sh-and-bash – Stéphane Veyret May 14 '20 at 09:49
0

There are a few technical problems with your code, and also (if I understand what you are trying to do correctly) some logical errors in the design.

I'll share a few comments about how I looked at this. First thing to note is that the code you show is inconsistently indented, which means it is hard to see the logical structure of the code by simply looking at it. I loaded this into my usual editor (VSCode) which has a handy "Format Document" feature (many other code editors have similar auto-format features), and that produced the following:

#!/bin/sh
i=2

while [ $i -le 100]; do
        c=0
        j=1

        while [ $j -lt $i ]; do
                if [ $(($i % $j)) -eq 0 ]; then
                        c=$(($c + 1))
                fi
                j=$(($j + 1))

                if [ $c -eq 0 ]; then
                        echo i
                fi
        done

        i=$(($i + 1))
done

Note how this properly shows the nesting of statements within the inner while loop, in contrast to the original code. Even if you don't have access to a fancy editor, it is worthwhile spending the time to ensure that your code is manually indented correctly. It will save you a lot of time later in understanding and debugging the code.

Note also that the echo i line towards the end of the code should be echo $i if you want to print the value of the i variable.

When I try to run this code, it actually gives me an error telling me there's a missing ] in line 4. You need a space after the 100 here (as also pointed out by Stéphane Veyret):

while [ $i -le 100 ]; do

Next up, I added extra echo lines at various points in the code just to debug it. For example, the first thing I tried was to add the following line just before the final done line (the last line of the code)

echo "i=" $i 

When I run the code I see this message printed multiple times, which tells me that the loop is running and that i is getting incremented from 2 to 100 as expected. I then added further echo lines at places within the while loop to check the values of other variables too.

These investigations highlighted a problem in your logic in the line

if [ $(($i % $j)) -eq 0 ]; then

The variable j is set to 1 at the start of each pass of the outer while loop. Any number modulus 1 is equal to zero, so this test will always be true when j is 1. This means that c will always be incremented the first time this code is encountered in each pass of the outer while loop (c=$(($c + 1))). And that means that the following test (if [ $c -eq 0 ]; then) will never be true.

I hope that gives you some ideas of what the problems are and how to go about further testing and debugging the code to make it do what you want.

Tim T
  • 1
  • 2