LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-13-2012, 11:46 PM   #1
kakalig2007
LQ Newbie
 
Registered: Jul 2008
Posts: 11

Rep: Reputation: 0
palindromes within a range


I cannot find what's wrong with my code, but it is not printing anything.Please help....
#!/bin/bash
echo Enter range
read l h
for (( num=l;num<=h;num++ ))
do
a=$num
rev=0
while [ $num -ne 0 ]
do
let rem=num%10
let rev=rev*10+rem
let num=num/10
done

if [ $a -eq $rev ]
then
break
fi
echo $a
done
 
Old 03-14-2012, 12:21 AM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
Suggestion: You should explain what you're trying to do, and then ask an actual question.

Also, check out the [ code ] tags and indentation -- makes it a lot nicer to read posts.
 
Old 03-14-2012, 12:22 AM   #3
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Checkout this code:

Code:
echo "Enter a String : "
read string
i=0
len=${#string}
#get the mid value upto which the comparison would be done
mid=$(($len/2))
while [ $i -lt $mid ]
  do
    if [ "${string:$i:1}" != "${string: -$(($i+1)):1}" ]
      then
      echo "\"$string\" IS NOT a Palindrome"
      exit
    fi
    i=$(($i+1))
done
echo "\"$string\" is a Palindrome"

Last edited by Satyaveer Arya; 03-14-2012 at 12:41 AM.
 
Old 03-14-2012, 12:49 AM   #4
kakaka
Member
 
Registered: Sep 2003
Posts: 382

Rep: Reputation: 87
Hi kakalig2007,

Are you trying to find numeric palindromes?

To try to see what your code is doing, you can use:

Code:
set -x
to turn on "debug" output for your code, to be able to see some substitutions
and to watch your code work. I'd also use some indenting, it might make it
easier to find things in the code. With those changes, the code might look
like this:

Code:
#!/bin/bash

set -x

echo Enter range
read l h

for ((  num=l ;  num <= h ;  num++  ))
    do

        a=$num
        rev=0

        while [  $num  -ne  0  ]
            do
                let rem=num%10
                let rev=rev*10+rem
                let num=num/10
            done

        if [  $a  -eq  $rev  ]
            then
                break
            fi
            
        echo $a

    done

But I would probably change the way the numbers are reversed, to just use
the rev command. That could look like this:

Code:
#!/bin/bash

set -x

echo Enter range
read l h

for ((  num=l ;  num <= h ;  num++  ))
    do

        original=$num
        reversed=`echo $original | rev`
        
        if [  $reversed  -eq  $original  ]
            then
                echo $original
        fi

    done
 
Old 03-14-2012, 12:53 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,423

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Try placing set -xv at the start of the script to see where it might be going wrong or not behaving as you expect.

Also, I find it odd that you use (()) for the for loop but then all other arithmetic is done using other, more antiquated, methods?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to ping a range or own IP-range ugurgazi Programming 13 05-02-2016 10:08 AM
Gnuplot - Combining a linear range and log-scale range in the same X-axis llauro Linux - Software 2 04-26-2011 01:55 PM
Out of range divyashree Linux - Hardware 2 07-20-2009 01:29 PM
[SOLVED] What are short range link and long range links in routing? mq15 Linux - Networking 6 06-27-2009 12:16 AM
finding half palindromes (java) randomx Programming 2 10-08-2003 07:16 PM


All times are GMT -5. The time now is 06:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration