[Solved] Why code run not correctly


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Why code run not correctly
# 1  
Old 07-12-2012
[Solved] Why code run not correctly

Hi there can anyone help me
here is my code
Code:
echo "Type in a positive number"
read X
I=2
while [ $(( $X > $I )) ]
do
if [ $(( $X % $I == 0)) ]
then
echo "It is not prime"
break
else
if [ $(($X - $I == 1)) ]
then
echo "It is  prime"
break
else
I=$(( $I + 1))
fi
fi
done

idk why when I am entering there 5 it says that it is not prime any ideas why it is working not cirrectly??? Thanks in advanace!! Smilie
# 2  
Old 07-12-2012
Hi

The closing brackets were placed at the wrong places:


Code:
echo "Type in a positive number"
read X
I=2
while [ $X>$I ]
do
    if [ $(($X%$I)) == 0 ]
    then
    echo "It is not prime"
    break
    else
    if [ $(($X-$I)) == 1 ]
    then
    echo "It is  prime"
    break
    else
    I=$(( $I + 1))
    fi
    fi
done

This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 07-12-2012
Quote:
Originally Posted by guruprasadpr
Hi

The closing brackets were placed at the wrong places:


Code:
echo "Type in a positive number"
read X
I=2
while [ $X>$I ]
do
    if [ $(($X%$I)) == 0 ]
    then
    echo "It is not prime"
    break
    else
    if [ $(($X-$I)) == 1 ]
    then
    echo "It is  prime"
    break
    else
    I=$(( $I + 1))
    fi
    fi
done

Thanks for answering I tried to remove all the spaces in arithmetic expressions but still it generates wrong answer
# 4  
Old 07-12-2012
Hi

Its not just about spaces, its about the brackets as mentioned earlier. Try running the corrected code which i pasted, it works fine.

Guru
# 5  
Old 07-12-2012
Quote:
Originally Posted by guruprasadpr
Hi

Its not just about spaces, its about the brackets as mentioned earlier. Try running the corrected code which i pasted, it works fine.

Guru
Smilie
oh, get my mistake
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

When I run the code yesterday I am getting output,when I run same code today I am getting error?

If run the below code today its creating all directory and getting output files,I f run same code tomorrow I am getting error. can any one give suggestion to sortout this error. OSError: no such file or directory : '062518'My code looks like this import paramiko import sys import os ... (8 Replies)
Discussion started by: haribabu2229
8 Replies

2. Shell Programming and Scripting

[Solved] Help understanding this code!!

Hi guys, I am still learning awk and much apprecated to shed some light on the following: the questions asked is below! { total = i = 0 do { ++i total += $i } while ( total <= 100 ) print i, ":", total } File used: cat test.do 45 25 60 20 10 105 50 40 33 5 9 67 108 3 5 4 (2 Replies)
Discussion started by: Apollo
2 Replies

3. Homework & Coursework Questions

Cannot correctly connect multi-stage C command pipe (among others) (FYI: a lot of code)

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: We are supposed to write a C program that parses a command line, separates it into each command (further... (5 Replies)
Discussion started by: kowit010
5 Replies

4. Shell Programming and Scripting

[Solved] Run multiple commands in invoked program

Hi, I have coded a program in Haskell using the compiler Hugs and the program requires multiple commands (with parameters) to be entered into it, it then outputs the result of its execution. I need to test a lot of different options (i.e. the parameters) so it would be obvious to automate the... (0 Replies)
Discussion started by: tz742
0 Replies

5. UNIX for Advanced & Expert Users

[SOLVED] Code does not run when assigned to a variable

I am more of a newbie, but wanted to post this in this forum as I was afraid no one would look at it in unix forums as it concerns shell scripting. I have a shell script that now runs fine with the exclusion of one line: x=`su nbadmin -c "ssh -t servery /usr/openv/netbackup/bin/bplist -C... (7 Replies)
Discussion started by: newbie2010
7 Replies

6. UNIX for Dummies Questions & Answers

[Solved] Cron - job to run every 3rd Friday of the month only

Hi Expert Please help me to set a cron job schedule, Ihave a job that run every 3rd Friday of the month at 1030am. I tried to set up like this, but the job still runs every friday at 1030am. I want the job to run every 3rd Friday of the month at 1030am only 30 10 15,16,17,18,19,20,21... (2 Replies)
Discussion started by: kaibiganmi
2 Replies

7. Shell Programming and Scripting

run shell script under nohup directly [solved]

Hi, i am not able to run the loop in nohup directly. nohup 'for i in $(seq 10); do echo $i;./mscript.sh $i; done' can some one help me how to run this directly in nohup? ---------- Post updated 03-15-12 at 12:20 AM ---------- Previous update was 03-14-12 at 11:59 PM ---------- From... (0 Replies)
Discussion started by: johninweb
0 Replies

8. Shell Programming and Scripting

run command with ssh[solved]

Hi all, Is it possible to make this possible ? $ echo $SKY_HOME /var/sink/SKY $ echo $SKY_HOME /home/smily/SKY $ ssh root@xyz "echo $SKY_HOME" root@xyz 's password: ****** /home/smily/SKY wrong output I was expecting the output as /var/sink/SKY (3 Replies)
Discussion started by: linuxadmin
3 Replies

9. Shell Programming and Scripting

Run perl command in script[solved]

Hi all, When I put the Perl command in a script, I got error. system("perl -pi -e 's@words@words@g' myFile"); The error is: Unrecognized character \x8A; marked by <-- HERE after دت مد�<-- HERE near column 15 at -e line 1. Thanks in advance. ---------- Post updated at 06:30 AM... (0 Replies)
Discussion started by: Lham
0 Replies
Login or Register to Ask a Question