Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Shell Loops me mad
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
RD
LXF regular


Joined: Mon Jul 25, 2005 3:53 am
Posts: 272
Location: irc.ixl2.net

PostPosted: Mon Jul 25, 2005 4:01 am    Post subject: Shell Loops me mad Reply with quote

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 Shocked 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 Shocked
i would be grate full of any help you can provide as to why as im at a loss right now
Back to top
View user's profile Send private message Visit poster's website
RD
LXF regular


Joined: Mon Jul 25, 2005 3:53 am
Posts: 272
Location: irc.ixl2.net

PostPosted: Mon Jul 25, 2005 4:14 am    Post subject: RE: Shell Loops me mad Reply with quote

sorry guys did not relize id already posted twice
_________________
[url=irc://irc.ixl2.org/ixl2]irc.ixl2.org[/url]
Back to top
View user's profile Send private message Visit poster's website
Nigel
LXF regular


Joined: Fri Apr 08, 2005 9:03 pm
Posts: 1141
Location: Gloucestershire, UK

PostPosted: Mon Jul 25, 2005 10:49 am    Post subject: RE: Shell Loops me mad Reply with quote

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" Smile

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.
Back to top
View user's profile Send private message
Nobber
LXF regular


Joined: Mon Apr 11, 2005 4:24 pm
Posts: 370
Location: Nova Scotia

PostPosted: Mon Jul 25, 2005 11:13 am    Post subject: RE: Shell Loops me mad Reply with quote

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. Confused
_________________
800 LINES ERIC - GET BACK TO PYSKOOL
Back to top
View user's profile Send private message
RD
LXF regular


Joined: Mon Jul 25, 2005 3:53 am
Posts: 272
Location: irc.ixl2.net

PostPosted: Mon Jul 25, 2005 12:44 pm    Post subject: Reply with quote

Hi giuys thanks for the repliys but it did not work its still looping the sorry commas not allowed no matter what i put
Back to top
View user's profile Send private message Visit poster's website
Nobber
LXF regular


Joined: Mon Apr 11, 2005 4:24 pm
Posts: 370
Location: Nova Scotia

PostPosted: Mon Jul 25, 2005 1:59 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Nigel
LXF regular


Joined: Fri Apr 08, 2005 9:03 pm
Posts: 1141
Location: Gloucestershire, UK

PostPosted: Mon Jul 25, 2005 3:57 pm    Post subject: Re: Shell Loops me mad Reply with quote

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:

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


to read

Code:

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


which stopped the looping for me.
_________________
Hope this helps,

Nigel.
Back to top
View user's profile Send private message
RD
LXF regular


Joined: Mon Jul 25, 2005 3:53 am
Posts: 272
Location: irc.ixl2.net

PostPosted: Mon Jul 25, 2005 5:21 pm    Post subject: RE: Re: Shell Loops me mad Reply with quote

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 Very Happy

Thanks guys for the help
_________________
[url=irc://irc.ixl2.org/ixl2]irc.ixl2.org[/url]
Back to top
View user's profile Send private message Visit poster's website
Nigel
LXF regular


Joined: Fri Apr 08, 2005 9:03 pm
Posts: 1141
Location: Gloucestershire, UK

PostPosted: Mon Jul 25, 2005 5:31 pm    Post subject: Re: RE: Re: Shell Loops me mad Reply with quote

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) Exclamation
Back to top
View user's profile Send private message
RD
LXF regular


Joined: Mon Jul 25, 2005 3:53 am
Posts: 272
Location: irc.ixl2.net

PostPosted: Mon Jul 25, 2005 6:29 pm    Post subject: RE: Re: RE: Re: Shell Loops me mad Reply with quote

near the start of the script Very Happy
_________________
[url=irc://irc.ixl2.org/ixl2]irc.ixl2.org[/url]
Back to top
View user's profile Send private message Visit poster's website
RD
LXF regular


Joined: Mon Jul 25, 2005 3:53 am
Posts: 272
Location: irc.ixl2.net

PostPosted: Mon Jul 25, 2005 6:47 pm    Post subject: Reply with quote

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:


# Tidy up and leave the application

rm -f $temp_file
echo "Thank you for using this application :-)"
exit 0
   


Last edited by RD on Thu Jul 28, 2005 12:15 am; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
Nigel
LXF regular


Joined: Fri Apr 08, 2005 9:03 pm
Posts: 1141
Location: Gloucestershire, UK

PostPosted: Mon Jul 25, 2005 11:06 pm    Post subject: Reply with quote

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 Smile

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:

      b)
        echo
        more $title_file
        echo
        get_return
      q | Q  quit=y;;
      *) echo "Sorry, choice not recognized";;

near the end to read
Code:

      b)
        echo
        more $title_file
        echo
        get_return;;
      q | Q)  quit=y;;
      *) echo "Sorry, choice not recognized";;


One tip - the line
Code:

#!/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.
Back to top
View user's profile Send private message
RD
LXF regular


Joined: Mon Jul 25, 2005 3:53 am
Posts: 272
Location: irc.ixl2.net

PostPosted: Wed Jul 27, 2005 7:02 pm    Post subject: Reply with quote

Hi Nigel

First thanks for the help Very Happy

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 Sad

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 Very Happy 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
_________________
[url=irc://irc.ixl2.org/ixl2]irc.ixl2.org[/url]
Back to top
View user's profile Send private message Visit poster's website
RD
LXF regular


Joined: Mon Jul 25, 2005 3:53 am
Posts: 272
Location: irc.ixl2.net

PostPosted: Wed Jul 27, 2005 7:28 pm    Post subject: Reply with quote

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 Thu Jul 28, 2005 12:17 am; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
Nigel
LXF regular


Joined: Fri Apr 08, 2005 9:03 pm
Posts: 1141
Location: Gloucestershire, UK

PostPosted: Wed Jul 27, 2005 9:43 pm    Post subject: Reply with quote

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 Smile )
_________________
Hope this helps,

Nigel.
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast