LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with bash script (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-bash-script-789474/)

catkin 02-17-2010 10:02 AM

Please don't change the posted scripts when you fix an error; it's very confusing. Better add an "EDIT: " line at the top of the post to let us know you have moved on from the posted script and post the new one in another post.

BTW, regards
Code:

read ANS
ANS = $ANS

Assignments must not have space either side of the = sign so you wanted ANS=$ANS except you don't because it doesn't do anything, IYSWIM.

schneidz 02-17-2010 10:11 AM

^ that should be
Code:

echo debug: ANS = "$ANS"
i put that in so that we would know what the value of the ANS variable was in case it was not being assigned correctly.

i dont know, i dont use cases much. maybe a space between 1 and ) and 2 and ) like the ones above it ?

is it still complaining about line 119 ?

catkin 02-17-2010 10:33 AM

Quote:

Originally Posted by schneidz (Post 3866996)
i dont know, i dont use cases much. maybe a space between 1 and ) and 2 and ) like the ones above it ?

In case commands the space between pattern and ) is optional.

m2dmhot 02-17-2010 10:36 AM

Quote:

Originally Posted by schneidz (Post 3866996)
^ that should be
Code:

echo debug: ANS = "$ANS"
i put that in so that we would know what the value of the ANS variable was in case it was not being assigned correctly.

i dont know, i dont use cases much. maybe a space between 1 and ) and 2 and ) like the ones above it ?

is it still complaining about line 119 ?

Ok i went back to original script with the latest help..
still chokin near bottom line 118
./123456789_fedora11: line 118: syntax error near unexpected token `newline'
./123456789_fedora11: line 118: `... '


Code:

1:#!/bin/bash
3:PLAYLIST=/home/Fedora11
5:AUDIOPATH=$PLAYLIST/MP3
7:## Functions for each task
9:create_playlist() {
11:  cd "$AUDIOPATH"
12:  echo -e "Enter Name Of Playlist --> \c"
13:  read PNAME
14:  touch $PNAME.m3u
16:  echo -e
17:  ls -v *.mp3
18:  echo -e
19:}
23:edit_playlist() {
24:  : put appropriate commands here
25:}
28:delete_playlist() {
29:  : put appropriate commands here
30:}
33:generate_playlist() {
34:  : put appropriate commands here
35:}
39:play_mp3() {
41:    cd "$AUDIOPATH"
42:    ls -v *.mp3
43:    echo -e
44:    echo -e "Please Select Your Song --> \c"
45:    IFS= read DOG
46:    mplayer "$AUDIOPATH"/$DOG
48:}
51:copy_mp3() {
53:    cd "$AUDIOPATH"
54:    ls -v *.mp3 >> "$AUDIOPATH"/tempdir.lst # Create Temp file with Directory listing
56:    echo -e "Please Select Your Song To Copy --> \c"
57:    read DONKEY
60:    cat "$AUDIOPATH"/tempdir.lst | while read line; do #Read each line of the file and store the line to $line variable
61:                                    ...
62:                                    done < "$AUDIOPATH"/temp.lst
63:        if [[ $line == $DONKEY ]]; then
64:            echo "File Already Exist, Copy Aborted!"
65:        else
66:            cp $DONKEY "$PLAYLIST"/MP3_1
67:      fi
68:   
69:    rm "$AUDIOPATH"/tempdir.lst # Delete the temporary file containing the directory listing
71:}
74:delete_mp3() {
76:    cd "$AUDIOPATH"
77:    ls -v *.mp3
78:    echo -e
80:    echo -e "Please Select Which Song To Delete --> \c"
81:    read SONG
82:    rm $SONG
83:    cd "$AUDIOPATH" || exit 1
85:}
87:## Print menu and execute user's selection
89:while  :
90:do
91:  printf %s '
94:Mp3 Playlist Program:
95:================================
96:C)reate Mp3 Playlist
97:E)dit Mp3 Playlists
98:D)isplay Mp3 Playlists
99:G)enerate Mp3 Database
100:P)lay Mp3
101:1)Copy Mp3
102:2)Remove Mp3
103:Q)uit
104:Enter your selection ==> '
106:  read ANS
107:debug: ANS= "$ANS"
108:...
109: case "$ANS" in
110:    "c" | "C" ) create_playlist ;;
111:    "e" | "E" ) edit_playlist ;;
112:    "d" | "D" ) delete_playlist ;;
113:    "g" | "G" ) generate_playlist ;;
114:    "p" | "P" ) play_mp3 ;;
115:    "o" | "O" ) copy_mp3 ;;
116:    "l" | "L" ) delete_mp3 ;;
117:    "q "| "Q" ) exit ;;
118:... 
119:esac
120:done


catkin 02-17-2010 10:46 AM

Quote:

Originally Posted by m2dmhot (Post 3867020)
Ok i went back to original script with the latest help..
still chokin near bottom line 118
./123456789_fedora11: line 118: syntax error near unexpected token `newline'
./123456789_fedora11: line 118: `... '


Code:

1:#!/bin/bash
3:PLAYLIST=/home/Fedora11
5:AUDIOPATH=$PLAYLIST/MP3
7:## Functions for each task
9:create_playlist() {
11:  cd "$AUDIOPATH"
12:  echo -e "Enter Name Of Playlist --> \c"
13:  read PNAME
14:  touch $PNAME.m3u
16:  echo -e
17:  ls -v *.mp3
18:  echo -e
19:}
23:edit_playlist() {
24:  : put appropriate commands here
25:}
28:delete_playlist() {
29:  : put appropriate commands here
30:}
33:generate_playlist() {
34:  : put appropriate commands here
35:}
39:play_mp3() {
41:    cd "$AUDIOPATH"
42:    ls -v *.mp3
43:    echo -e
44:    echo -e "Please Select Your Song --> \c"
45:    IFS= read DOG
46:    mplayer "$AUDIOPATH"/$DOG
48:}
51:copy_mp3() {
53:    cd "$AUDIOPATH"
54:    ls -v *.mp3 >> "$AUDIOPATH"/tempdir.lst # Create Temp file with Directory listing
56:    echo -e "Please Select Your Song To Copy --> \c"
57:    read DONKEY
60:    cat "$AUDIOPATH"/tempdir.lst | while read line; do #Read each line of the file and store the line to $line variable
61:                                    ...
62:                                    done < "$AUDIOPATH"/temp.lst
63:        if [[ $line == $DONKEY ]]; then
64:            echo "File Already Exist, Copy Aborted!"
65:        else
66:            cp $DONKEY "$PLAYLIST"/MP3_1
67:      fi
68:   
69:    rm "$AUDIOPATH"/tempdir.lst # Delete the temporary file containing the directory listing
71:}
74:delete_mp3() {
76:    cd "$AUDIOPATH"
77:    ls -v *.mp3
78:    echo -e
80:    echo -e "Please Select Which Song To Delete --> \c"
81:    read SONG
82:    rm $SONG
83:    cd "$AUDIOPATH" || exit 1
85:}
87:## Print menu and execute user's selection
89:while  :
90:do
91:  printf %s '
94:Mp3 Playlist Program:
95:================================
96:C)reate Mp3 Playlist
97:E)dit Mp3 Playlists
98:D)isplay Mp3 Playlists
99:G)enerate Mp3 Database
100:P)lay Mp3
101:1)Copy Mp3
102:2)Remove Mp3
103:Q)uit
104:Enter your selection ==> '
106:  read ANS
107:debug: ANS= "$ANS"
108:...
109: case "$ANS" in
110:    "c" | "C" ) create_playlist ;;
111:    "e" | "E" ) edit_playlist ;;
112:    "d" | "D" ) delete_playlist ;;
113:    "g" | "G" ) generate_playlist ;;
114:    "p" | "P" ) play_mp3 ;;
115:    "o" | "O" ) copy_mp3 ;;
116:    "l" | "L" ) delete_mp3 ;;
117:    "q "| "Q" ) exit ;;
118:... 
119:esac
120:done


That's funny; running the above code (as lasted edited at 22:09) I got
Code:

c@CW8:~$ ./trash
./trash: line 58: syntax error near unexpected token `do'
./trash: line 58: `do'

Not surprising as there is no until, while or for line before the do.

schneidz 02-17-2010 11:00 AM

^
Code:

60:    cat "$AUDIOPATH"/tempdir.lst | while read line; do #Read each line of the file and store the line to $line variable
61:                                    ...

@m2dmhot: what is line 108 and line 118 supposed to do (they contain ...) ?
also 107 should be: echo debug: ANS = "$ANS"
you need to clean up these ellipsises
we use them to describe code that comes before or after but is not pertinent to the current issue.

m2dmhot 02-17-2010 12:33 PM

Quote:

Originally Posted by schneidz (Post 3867053)
^
Code:

60:    cat "$AUDIOPATH"/tempdir.lst | while read line; do #Read each line of the file and store the line to $line variable
61:                                    ...

@m2dmhot: what is line 108 and line 118 supposed to do (they contain ...) ?
also 107 should be: echo debug: ANS = "$ANS"
you need to clean up these ellipsises
we use them to describe code that comes before or after but is not pertinent to the current issue.

Im not sure what those ... does i was told to change it in this post
http://www.linuxquestions.org/questi...69#post3866069
I fixed the echo also but not sure how to fix
Code:

51:copy_mp3() {
53:    cd "$AUDIOPATH"
54:    ls -v *.mp3 >> "$AUDIOPATH"/tempdir.lst # Create Temp file with Directory listing
56:    echo -e "Please Select Your Song To Copy --> \c"
57:    read DONKEY
60:    cat "$AUDIOPATH"/tempdir.lst | while read line; do #Read each line of the file and store the line to $line variable
61:                                    ...
62:                                    done < "$AUDIOPATH"/temp.lst
63:        if [[ $line == $DONKEY ]]; then
64:            echo "File Already Exist, Copy Aborted!"
65:        else
66:            cp $DONKEY "$PLAYLIST"/MP3_1
67:      fi
68:   
69:    rm "$AUDIOPATH"/tempdir.lst # Delete the temporary file containing the directory listing
71:}


casperz 02-17-2010 01:07 PM

I have a question regarding this script....
When I run this script, create a playlist. I get a 0 bytes m3u file. (contains nothing)
I read somewhere that there was an issue with mplayer and m3u files??? Maybe it was an older posting..
I could just have is save it as a .txt file?

casperz 02-17-2010 01:18 PM

find /home/woot/Mp3 -name "*.mp3" -print > playlist.txt

This will work.....only scans from the mp3 folder

schneidz 02-17-2010 01:31 PM

^ line 17 doesnt redirect ls -v to the file name therefore it remains blank. m2dmhot hasnt gotten that for because s/he got other errors to worry about first.

@m2dmhot
in Perl6, the 3-character ellipsis is also known as the "yadda yadda yadda" operator and, similarly to its linguistic meaning, serves as a "stand-in" for code to be inserted later.
http://en.wikipedia.org/wiki/Ellipsis

m2dmhot 02-17-2010 01:34 PM

ok i removed the '...' in both locations
now it gives me unexpected end of file syntax error
line 123

????

schneidz 02-17-2010 01:52 PM

hmm, thats interesting considering that the script has only 120 lines ?

maybe a blank line it doesnt like ?

possibly because exit isnt defined as a function so it doesnt know what to do (but i think it should exit from the shell so i am not sure) ?

m2dmhot 02-17-2010 02:39 PM

Quote:

Originally Posted by schneidz (Post 3867250)
hmm, thats interesting considering that the script has only 120 lines ?

maybe a blank line it doesnt like ?

possibly because exit isnt defined as a function so it doesnt know what to do (but i think it should exit from the shell so i am not sure) ?

Seems like its me.. I got another fellow coder to run the script and it runs fine??? My bash version is 4.0.23 any ideas???

m2dmhot 02-17-2010 03:01 PM

it was the colons at the parts that were not filled in yet.. THanks guys i will finish this up and post back if any probs

penguiniator 02-17-2010 09:31 PM

You should delete this code:
Code:

cat "$AUDIOPATH"/tempdir.lst |
from in front of your while loop. This code:
Code:

< "$AUDIOPATH"/tempdir.lst
at the end of the loop is meant to replace it.


All times are GMT -5. The time now is 06:59 PM.