Shell Loops me mad

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Shell Loops me mad

Postby RD » Mon Jul 25, 2005 3:01 am

Hi

Im trying to write my first shell script and well its not going too well right now as it keeps looping in one area :shock: and im not too sure as why im kinda hoping you guys could help out and tell me what im doing wrong

here is the section of code it loops on

add_record_tracks() {
echo "Enter track information for this CD"
echo "When no more tracks enter q"
cdtrack=1
cdtitle=""
while [ "$cdtitle" != "q" ]
do
echo -e "Track $cdtrack, track title? \c"
read tmp
cdtitle=${tmp%%,*}
if [ "$tmp" != "$cdttitle" ] ; then # Keeps looping here ???
echo "Sorry, no commas allowed"

continue
fi

if [ -n "$cdttitle" ] ; then
if [ "$cdttitle" != "q" ] ; then
insert_track $cdcatnum,$cdtrack,$cdtitle
fi
else
cdtrack=$(cdtrack-1)
fi
cdtrack=$(cdtrack+1)
done

It keeps repeating that no commas are allowed even when i don't put any in :shock:
i would be grate full of any help you can provide as to why as im at a loss right now
RD
LXF regular
 
Posts: 272
Joined: Mon Jul 25, 2005 2:53 am
Location: irc.ixl2.net

RE: Shell Loops me mad

Postby RD » Mon Jul 25, 2005 3:14 am

sorry guys did not relize id already posted twice
RD
LXF regular
 
Posts: 272
Joined: Mon Jul 25, 2005 2:53 am
Location: irc.ixl2.net

RE: Shell Loops me mad

Postby Nigel » Mon Jul 25, 2005 9:49 am

You have a typo in your if test - you are comparing cdttitle to tmp when I think you want to be comparing cdtitle with one less "t" :)

You have the same typo further down as well.

And your increment/decrement statements for cdtrack won't work - they should be of the form

cdtrack=$(($cdtrack+1))
Hope this helps,

Nigel.
User avatar
Nigel
LXF regular
 
Posts: 1141
Joined: Fri Apr 08, 2005 8:03 pm
Location: Gloucestershire, UK

RE: Shell Loops me mad

Postby Nobber » Mon Jul 25, 2005 10:13 am

Atrithmetic in bash scripts can be confusing, because there are so many ways to do it. For example, to add 1 to cdtrack, any of these would work:

((cdtrack++))

cdtrack=$(($cdtrack+1))

cdtrack=$((cdtrack+1))

let cdtrack=cdtrack+1

And there are probably several other ways. I can never make up my mind which form to use. :?
800 LINES ERIC - GET BACK TO PYSKOOL
User avatar
Nobber
LXF regular
 
Posts: 370
Joined: Mon Apr 11, 2005 3:24 pm
Location: Nova Scotia

Postby RD » Mon Jul 25, 2005 11:44 am

Hi giuys thanks for the repliys but it did not work its still looping the sorry commas not allowed no matter what i put
RD
LXF regular
 
Posts: 272
Joined: Mon Jul 25, 2005 2:53 am
Location: irc.ixl2.net

Postby Nobber » Mon Jul 25, 2005 12:59 pm

Odd. I copied and pasted your script, corrected the variable and arithmetic typos already pointed out, and it seems to work OK.

Post your script again.
800 LINES ERIC - GET BACK TO PYSKOOL
User avatar
Nobber
LXF regular
 
Posts: 370
Joined: Mon Apr 11, 2005 3:24 pm
Location: Nova Scotia

Re: Shell Loops me mad

Postby Nigel » Mon Jul 25, 2005 2:57 pm

I also copied & ran the script to find the original problem (typos like that are very easy to miss !)

Make sure that you changed

Code: Select all
      if [ "$tmp" != "$cdttitle" ] ; then # Keeps looping here ???


to read

Code: Select all
      if [ "$tmp" != "$cdtitle" ] ; then # Keeps looping here ???


which stopped the looping for me.
Hope this helps,

Nigel.
User avatar
Nigel
LXF regular
 
Posts: 1141
Joined: Fri Apr 08, 2005 8:03 pm
Location: Gloucestershire, UK

RE: Re: Shell Loops me mad

Postby RD » Mon Jul 25, 2005 4:21 pm

That was not a typo here is how it was suppose to be

while [ "$cdttitle" != "q" ]
do
echo -e "Track $cdtrack, track title? \c"
read tmp
cdtitle=${tmp%%,*}
if [ "$tmp" != "$cdttitle" ] ; then # Keeps looping here ???
echo "Sorry, no commas allowed"
continue
fi

if [ -n "$cdttitle" ] ; then
if [ "$cdttitle" != "q" ] ; then


I had another look after posting it did not work then found out i missed a t in while [ "$cdttitle" != "q" ] also
cdtrack=$((cdtrack+1))

worked just fine i am having another problem which when i get back home i will post at where i think the problem is :D

Thanks guys for the help
RD
LXF regular
 
Posts: 272
Joined: Mon Jul 25, 2005 2:53 am
Location: irc.ixl2.net

Re: RE: Re: Shell Loops me mad

Postby Nigel » Mon Jul 25, 2005 4:31 pm

RD wrote:That was not a typo here is how it was suppose to be


sorry - you are setting cdtitle (one "t") to the value of tmp without the commas, then testing cdttitle (two "t"s) against the value of tmp...
either one of these has a typo in it, or you're not showing us some code which is vital to sorting this problem (ie where you are setting cdttitle (with two "t"s) :!:
User avatar
Nigel
LXF regular
 
Posts: 1141
Joined: Fri Apr 08, 2005 8:03 pm
Location: Gloucestershire, UK

RE: Re: RE: Re: Shell Loops me mad

Postby RD » Mon Jul 25, 2005 5:29 pm

near the start of the script :D
RD
LXF regular
 
Posts: 272
Joined: Mon Jul 25, 2005 2:53 am
Location: irc.ixl2.net

Postby RD » Mon Jul 25, 2005 5:47 pm

Sorry was in a bit of a rush so heres the script please run it then you will see the problems i seem to be having

Code: Select all

# Tidy up and leave the application

rm -f $temp_file
echo "Thank you for using this application :-)"
exit 0
   
Last edited by RD on Wed Jul 27, 2005 11:15 pm, edited 1 time in total.
RD
LXF regular
 
Posts: 272
Joined: Mon Jul 25, 2005 2:53 am
Location: irc.ixl2.net

Postby Nigel » Mon Jul 25, 2005 10:06 pm

Well, it doesn't loop for me - as posted, the script seems to work fine in that area... but you have fixed the typo from your earlier post :)

The only problem I see with it now is that it won't let you quit from the main menu...
you need to change the section
Code: Select all
      b)
        echo
        more $title_file
        echo
        get_return
      q | Q  quit=y;;
      *) echo "Sorry, choice not recognized";;

near the end to read
Code: Select all
      b)
        echo
        more $title_file
        echo
        get_return;;
      q | Q)  quit=y;;
      *) echo "Sorry, choice not recognized";;


One tip - the line
Code: Select all
#!/bin/bash

usually goes right at the top of the file, before any comments. Doesn't matter for users whose default shell is bash, but as written it throws up a heap of errors if run under csh. Moving the /bin/bash line to the top sorts that out.
Hope this helps,

Nigel.
User avatar
Nigel
LXF regular
 
Posts: 1141
Joined: Fri Apr 08, 2005 8:03 pm
Location: Gloucestershire, UK

Postby RD » Wed Jul 27, 2005 6:02 pm

Hi Nigel

First thanks for the help :D

Now i've been getting the same problem as yourself ie won't let me quit how ever the original was wirtten with q | Q) quit=y;; and every time i tryed to run the script it keept giving errors :(

also as it was written using kdevelop i felt it best to just leave the default settings ie #!/bin/sh or bash which ever you want :D i do know that this is normally located at the top of the shell script, any way what do you suggest as to the not quitting problem ?

RD
RD
LXF regular
 
Posts: 272
Joined: Mon Jul 25, 2005 2:53 am
Location: irc.ixl2.net

Postby RD » Wed Jul 27, 2005 6:28 pm

Here is more info on the error im getting

Mini CD manager
cd_manager.sh: line 325: syntax error near unexpected token `)'
cd_manager.sh: line 325: ` q | Q) quit=y;;'


i've tryed in vi emacs and kdevelop and still getting the same error
Last edited by RD on Wed Jul 27, 2005 11:17 pm, edited 1 time in total.
RD
LXF regular
 
Posts: 272
Joined: Mon Jul 25, 2005 2:53 am
Location: irc.ixl2.net

Postby Nigel » Wed Jul 27, 2005 8:43 pm

Hi RD,

the basic problem was that you haven't terminated the section above properly - check you have two semicolons at the end of line 324.

I don't think the editor you use will make any difference (I use vi, mostly because I'm too lazy to learn anything else :) )
Hope this helps,

Nigel.
User avatar
Nigel
LXF regular
 
Posts: 1141
Joined: Fri Apr 08, 2005 8:03 pm
Location: Gloucestershire, UK

Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron