-8

I need to fix my code?

i=0; bil_genap=0;  
bil_ganjil=0;  
echo -n "Batas loop : ";  
read batas;  

if [ -z $batas ] | [ $batas -lt 0 ]; then  
    echo "Ops, tidak boleh kosong atau Batas loop harus >= 0";  
    exit 0;  
fi   
while [ $i -le $batas ]; do   
    echo -n "$i,";  
    if [ `expr $i % 2` -eq 0 ]; then   
        let bil_genap=$bil_genap+1;  
    else  
        let bil_ganjil=$bil_ganjil+1;  
        let i=$i+1;   
    fi  
done  

closed as unclear what you're asking by Michael Homer, Kusalananda, αғsнιη, Jeff Schaller, A.B Mar 16 at 12:14

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 6
    Welcome to U&L. There are a couple of problems with your question right now: 1) The variables and messages are not in English, which makes it difficult to understand what your script does. 2) You haven't described what the problem exactly is. – Haxiel Mar 16 at 5:44
  • There is no question here. – ctrl-alt-delor Mar 16 at 12:48
  • @ctrl-alt-delor: Actually, there was a question, but Rui F Ribeiro edited it out. – G-Man Mar 16 at 16:15
  • The code works as specified: That is it does everything that you say it does, and more. Can you tell us what it should do? – ctrl-alt-delor Mar 16 at 18:43
2

Beside small problems you should make line:

if [ -z $batas ] | [ $batas -lt 0 ]; then  

to be

if [ -z $batas ] || [ $batas -lt 0 ]; then  

And instead of this:

if [ `expr $i % 2` -eq 0 ]; then  

you can use

if (( $i % 2 == 0 )); then  
1

The lack of information in this post made this a weirdly appealing challenge for me. First, off to a robot translator who told me that the language is Malay, and divined that "batas" means "limit" or "boundary", genap means "even", ganjil means "odd" and the warning message roughly reads "Ops, can not be empty or Limit loop must be >= 0".

So the code requests an input value, checks to make sure the value is not empty and is zero or greater, and then, when you run it -- it loops infinitely and barfs zeros to the terminal. Since I'm not sure what the coder's ultimate goals are, I can't be sure I've "fixed" it just the right way, but by moving the line "let i=i+1;" out of the "else" statement, instead of looping infinitely, it counts from zero to the entered loop limit and outputs the count. It also counts the number of even and odd numbers it encounters along the way. The code, as is, does nothing with these counts. Maybe it's a snippet of a larger piece of code. Maybe it's a toy program written by someone experimenting for the fun of it. Anyway, here's a "translated" version that adds lines outputting the even and odd counts:

i=0;
even_num=0;
odd_num=0;
echo -n "Limit of loop : ";
read loop_lim;

if [ -z $loop_lim ] | [ $loop_lim -lt 0 ]; then
    echo "Ops, Limit of loop cannot be empty and must be  >= 0";
    exit 0;
fi
while [ $i -le $loop_lim ]; do
    echo -n "$i,";
    if [ `expr $i % 2` -eq 0 ]; then
        let even_num=$even_num+1;
    else
        let odd_num=$odd_num+1;
    fi
    let i=$i+1;
done
echo
echo $even_num
echo $odd_num

Not the answer you're looking for? Browse other questions tagged or ask your own question.