| 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
|
Posted: Mon Jul 25, 2005 4:01 am Post subject: Shell Loops me mad |
|
|
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 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
i would be grate full of any help you can provide as to why as im at a loss right now |
|
| Back to top |
|
 |
RD LXF regular
Joined: Mon Jul 25, 2005 3:53 am Posts: 272 Location: irc.ixl2.net
|
Posted: Mon Jul 25, 2005 4:14 am Post subject: RE: Shell Loops me mad |
|
|
sorry guys did not relize id already posted twice _________________ [url=irc://irc.ixl2.org/ixl2]irc.ixl2.org[/url] |
|
| Back to top |
|
 |
Nigel LXF regular

Joined: Fri Apr 08, 2005 9:03 pm Posts: 1141 Location: Gloucestershire, UK
|
Posted: Mon Jul 25, 2005 10:49 am Post subject: RE: Shell Loops me mad |
|
|
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. |
|
| Back to top |
|
 |
Nobber LXF regular

Joined: Mon Apr 11, 2005 4:24 pm Posts: 370 Location: Nova Scotia
|
Posted: Mon Jul 25, 2005 11:13 am Post subject: RE: Shell Loops me mad |
|
|
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 |
|
| Back to top |
|
 |
RD LXF regular
Joined: Mon Jul 25, 2005 3:53 am Posts: 272 Location: irc.ixl2.net
|
Posted: Mon Jul 25, 2005 12:44 pm Post subject: |
|
|
| 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 |
|
 |
Nobber LXF regular

Joined: Mon Apr 11, 2005 4:24 pm Posts: 370 Location: Nova Scotia
|
Posted: Mon Jul 25, 2005 1:59 pm Post subject: |
|
|
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 |
|
 |
Nigel LXF regular

Joined: Fri Apr 08, 2005 9:03 pm Posts: 1141 Location: Gloucestershire, UK
|
Posted: Mon Jul 25, 2005 3:57 pm Post subject: Re: Shell Loops me mad |
|
|
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 |
|
 |
RD LXF regular
Joined: Mon Jul 25, 2005 3:53 am Posts: 272 Location: irc.ixl2.net
|
Posted: Mon Jul 25, 2005 5:21 pm Post subject: RE: Re: Shell Loops me mad |
|
|
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
Thanks guys for the help _________________ [url=irc://irc.ixl2.org/ixl2]irc.ixl2.org[/url] |
|
| Back to top |
|
 |
Nigel LXF regular

Joined: Fri Apr 08, 2005 9:03 pm Posts: 1141 Location: Gloucestershire, UK
|
Posted: Mon Jul 25, 2005 5:31 pm Post subject: Re: RE: Re: Shell Loops me mad |
|
|
| 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)  |
|
| Back to top |
|
 |
RD LXF regular
Joined: Mon Jul 25, 2005 3:53 am Posts: 272 Location: irc.ixl2.net
|
Posted: Mon Jul 25, 2005 6:29 pm Post subject: RE: Re: RE: Re: Shell Loops me mad |
|
|
near the start of the script  _________________ [url=irc://irc.ixl2.org/ixl2]irc.ixl2.org[/url] |
|
| Back to top |
|
 |
RD LXF regular
Joined: Mon Jul 25, 2005 3:53 am Posts: 272 Location: irc.ixl2.net
|
Posted: Mon Jul 25, 2005 6:47 pm Post subject: |
|
|
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 |
|
 |
Nigel LXF regular

Joined: Fri Apr 08, 2005 9:03 pm Posts: 1141 Location: Gloucestershire, UK
|
Posted: Mon Jul 25, 2005 11:06 pm Post subject: |
|
|
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: |
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
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 |
|
 |
RD LXF regular
Joined: Mon Jul 25, 2005 3:53 am Posts: 272 Location: irc.ixl2.net
|
Posted: Wed Jul 27, 2005 7:02 pm Post subject: |
|
|
Hi Nigel
First thanks for the help
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 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 |
|
 |
RD LXF regular
Joined: Mon Jul 25, 2005 3:53 am Posts: 272 Location: irc.ixl2.net
|
Posted: Wed Jul 27, 2005 7:28 pm Post subject: |
|
|
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 |
|
 |
Nigel LXF regular

Joined: Fri Apr 08, 2005 9:03 pm Posts: 1141 Location: Gloucestershire, UK
|
Posted: Wed Jul 27, 2005 9:43 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|