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/)

m2dmhot 02-16-2010 12:24 PM

Ok will try that .. Love this forum so quick and guys so friendly

catkin 02-16-2010 01:06 PM

Quote:

Originally Posted by m2dmhot (Post 3865822)
Ok onto next line..
I need help with the copy file function. I got the basic commands but how can you check for duplicates??

Have a look at the cksum command.

BTW, it is safest to use double quotes around variables that may have whitespace in them. That way you ensure their value is not parsed into multiple whitespace-separated words by the shell.

m2dmhot 02-16-2010 03:25 PM

OK still chuggin along here bit by bit but it gives me error near the end any ideas??
Code:

#!/bin/bash

PLAYLIST=/home/woot

AUDIOPATH=$PLAYLIST/MP3

## Functions for each task

create_playlist() {

  cd "$AUDIOPATH"

  echo -e "Enter Name Of Playlist --> \c"

  read PNAME

  touch $PNAME.m3u

 

  echo -e

  ls -v *.mp3

  echo -e

}



edit_playlist() {

  : put appropriate commands here

}



delete_playlist() {

  : put appropriate commands here

}



generate_playlist() {

  : put appropriate commands here

}



play_mp3() {

    cd "$AUDIOPATH"

    ls -v *.mp3

    echo -e



    echo -e "Please Select Your Song --> \c"

    IFS= read DOG

    mplayer "$AUDIOPATH"/$DOG

}



copy_mp3() {

    cd "$AUDIOPATH"

    ls -v *.mp3 >> "$AUDIOPATH"/tempdir.lst # Create Temp file with Directory listing



    echo -e "Please Select Your Song To Copy --> \c"

    read DONKEY



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

        if $line == $DONKEY then

            echo "File Already Exist, Copy Aborted!"

        else

            cp "$DONKEY" "$PLAYLIST_MP3_1/"

     

    rm "$AUDIOPATH"/tempdir.lst # Delete the temporary file containing the directory listing

}



delete_mp3() {

    cd "$AUDIOPATH"

    ls -v *.mp3

    echo -e



    echo -e "Please Select Which Song To Delete --> \c"

    read SONG

    rm $SONG
    cd "$AUDIOPATH" || exit 1

}



## Print menu and execute user's selection

while  :

do

  printf %s '



Mp3 Playlist Program:

================================

C)reate Mp3 Playlist

E)dit Mp3 Playlists

D)isplay Mp3 Playlists

G)enerate Mp3 Database

P)lay Mp3

1)Copy Mp3

2)Remove Mp3

Q)uit

Enter your selection ==> '



  read ANS



  case "$ANS" in

    c|C ) create_playlist ;;

    e|E ) edit_playlist ;;

    d|D ) delete_playlist ;;

    g|G ) generate_playlist ;;

    p|P ) play_mp3 ;;

    1)    copy_mp3 ;;

    2)    delete_mp3 ;;

    q|Q ) exit ;;

  esac

done

this is where it chokes. any ideas??

'/12345_2_woot.txt: line 87: syntax error near unexpected token `in
'/12345_2_woot.txt: line 87: ` case "$ANS" in

schneidz 02-16-2010 04:23 PM

^ not quite sure but are you allowed to have a blank line after the case ?

rite after you read ANS can you do something like
Code:

debug: ANS = "$ANS"
maybe that will help us find the culprit.
____________________
http://tldp.org/LDP/abs/html/testbranch.html

maybe this will work
Code:

...
case "$ANS" in

    "c" | "C" ) create_playlist ;;

    "e" | "E" ) edit_playlist ;;

    "d" | "D" ) delete_playlist ;;

...
  esac


catkin 02-16-2010 10:18 PM

Quote:

Originally Posted by m2dmhot (Post 3866022)

this is where it chokes. any ideas??

'/12345_2_woot.txt: line 87: syntax error near unexpected token `in
'/12345_2_woot.txt: line 87: ` case "$ANS" in

Unbalanced quotes in (corrected)
Code:

cat "$AUDIOPATH"/tempdir.lst | while read line; do #Read each line of the file and store the line to $line variable
Missing fi and done in function copy_mp3.

m2dmhot 02-17-2010 07:43 AM

still chokin????

Code:

#!/bin/bash

PLAYLIST=/home/Fedora11

AUDIOPATH=$PLAYLIST/MP3

## Functions for each task

create_playlist() {

  cd "$AUDIOPATH"
  echo -e "Enter Name Of Playlist --> \c"
  read PNAME
  touch $PNAME.m3u

  echo -e
  ls -v *.mp3
  echo -e
}



edit_playlist() {
  : put appropriate commands here
}


delete_playlist() {
  : put appropriate commands here
}


generate_playlist() {
  : put appropriate commands here
}



play_mp3() {

    cd "$AUDIOPATH"
    ls -v *.mp3
    echo -e
    echo -e "Please Select Your Song --> \c"
    IFS= read DOG
    mplayer "$AUDIOPATH"/$DOG

}


copy_mp3() {

    cd "$AUDIOPATH"
    ls -v *.mp3 >> "$AUDIOPATH"/tempdir.lst # Create Temp file with Directory listing

    echo -e "Please Select Your Song To Copy --> \c"
    read DONKEY


    cat "$AUDIOPATH"/tempdir.lst | while read line; do #Read each line of the file and store the line to $line variable
        if $line == $DONKEY then
            echo "File Already Exist, Copy Aborted!"
        else
            cp $DONKEY "$PLAYLIST"/MP3_1
    done

    rm "$AUDIOPATH"/tempdir.lst # Delete the temporary file containing the directory listing

}


delete_mp3() {

    cd "$AUDIOPATH"
    ls -v *.mp3
    echo -e

    echo -e "Please Select Which Song To Delete --> \c"
    read SONG
    rm $SONG
    cd "$AUDIOPATH" || exit 1

}

## Print menu and execute user's selection

while  :
do
  printf %s '


Mp3 Playlist Program:
================================
C)reate Mp3 Playlist
E)dit Mp3 Playlists
D)isplay Mp3 Playlists
G)enerate Mp3 Database
P)lay Mp3
1)Copy Mp3
2)Remove Mp3
Q)uit
Enter your selection ==> '

  read ANS
debug: ANS = "$ANS"
...
 case "$ANS" in

    "c" | "C" ) create_playlist ;;
    "e" | "E" ) edit_playlist ;;
    "d" | "D" ) delete_playlist ;;
    "g" | "G" ) generate_playlist ;;
    "p" | "P" ) play_mp3 ;;
    "1" )    copy_mp3 ;;
    "2" )    delete_mp3 ;;
    "q "| "Q" ) exit ;;
... 
esac

/123456789_fedora11: line 63: syntax error near unexpected token `else'
./123456789_fedora11: line 63: ` else'

catkin 02-17-2010 08:15 AM

Missing [[ ]] and ";" in
Code:

if [[ $line == $DONKEY ]]; then
and its closing "fi" is still missing.

penguiniator 02-17-2010 08:43 AM

Go back to the previous "then" on line 61. You forgot the ; between the if clause and the then keyword. You also forgot the test command: [[ and ]]. Line 61 should read:
Code:

if [[ $line == $DONKEY ]]; then
The space after [[ and before ]] is required.

You can also reform your while read loop to:
Code:

while read line; do
  ...
done < "$AUDIOPATH"/tempdir.lst

This will eliminate piping the output of cat (which is almost never necessary) to read. Incidentally, you can also eliminate the echo before the read statement on line 56 by using read's -p argument.

Finally, you have an unterminated while loop beginning on line 87. Unless, of course, this is only a code fragment, which is suggested by the ... on line 106.

m2dmhot 02-17-2010 08:48 AM

Quote:

Originally Posted by catkin (Post 3866863)
Missing [[ ]] and ";" in
Code:

if [[ $line == $DONKEY ]]; then
and its closing "fi" is still missing.


im not catching what your saying??

Code:

copy_mp3() {

    cd "$AUDIOPATH"
    ls -v *.mp3 >> "$AUDIOPATH"/tempdir.lst # Create Temp file with Directory listing

    echo -e "Please Select Your Song To Copy --> \c"
    read DONKEY


    cat "$AUDIOPATH"/tempdir.lst | while read line; do #Read each line of the file and store the line to $line variable
                                    ...
                                    done < "$AUDIOPATH"/temp.lst
        if [[ $line == $DONKEY ]]; then
            echo "File Already Exist, Copy Aborted!"
        else
            cp $DONKEY "$PLAYLIST"/MP3_1
      fi
    done

    rm "$AUDIOPATH"/tempdir.lst # Delete the temporary file containing the directory listing

}


m2dmhot 02-17-2010 08:58 AM

Quote:

Originally Posted by penguiniator (Post 3866898)
Go back to the previous "then" on line 61. You forgot the ; between the if clause and the then keyword. You also forgot the test command: [[ and ]]. Line 61 should read:
Code:

if [[ $line == $DONKEY ]]; then
The space after [[ and before ]] is required.

You can also reform your while read loop to:
Code:

while read line; do
  ...
done < "$AUDIOPATH"/tempdir.lst

This will eliminate piping the output of cat (which is almost never necessary) to read. Incidentally, you can also eliminate the echo before the read statement on line 56 by using read's -p argument.

Finally, you have an unterminated while loop beginning on line 87. Unless, of course, this is only a code fragment, which is suggested by the ... on line 106.

Im a newb dont forget lol kinda have to hold my hand.. still learning

schneidz 02-17-2010 08:58 AM

^ at the end of an if,then,else statement you need to fi so that bash knows where the conditional statement ends.
_____
i see from your last edit 5 mins. ago, you did it rite.

m2dmhot 02-17-2010 09:11 AM

Quote:

Originally Posted by schneidz (Post 3866069)
^ not quite sure but are you allowed to have a blank line after the case ?

rite after you read ANS can you do something like
Code:

debug: ANS = "$ANS"
maybe that will help us find the culprit.
____________________
http://tldp.org/LDP/abs/html/testbranch.html

maybe this will work
Code:

...
case "$ANS" in

    "c" | "C" ) create_playlist ;;

    "e" | "E" ) edit_playlist ;;

    "d" | "D" ) delete_playlist ;;

...
  esac


Dying on line 119 does this look right??

Code:

...
 read ANS
debug: ANS = "$ANS"
...
 case "$ANS" in

    "c" | "C" ) create_playlist ;;
    "e" | "E" ) edit_playlist ;;
    "d" | "D" ) delete_playlist ;;
    "g" | "G" ) generate_playlist ;;
    "p" | "P" ) play_mp3 ;;
    "1" |    ) copy_mp3 ;;
    "2" |    ) delete_mp3 ;;
    "q" | "Q" ) exit ;; 
...
  esac

/123456789_fedora11: line 119: syntax error near unexpected token `newline'
./123456789_fedora11: line 119: `...'

casperz 02-17-2010 09:12 AM

This script looks pretty cool.
Can you post it when you get up and running?
I am trying to do something similar to this.

schneidz 02-17-2010 09:33 AM

can you print it with line numbers
Code:

grep -n . script.ksh
maybe it is choking on this
Quote:

"1" | ) copy_mp3 ;;
"2" | ) delete_mp3 ;;
the | are probably not needed.
in this instance | means or.

m2dmhot 02-17-2010 09:50 AM

Quote:

Originally Posted by schneidz (Post 3866951)
can you print it with line numbers
Code:

grep -n . script.ksh
maybe it is choking on thisthe | are probably not needed.
in this instance | means or.

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
109:case $ANS in

110:    c|C ) create_playlist
111:          ;;

112:    e|E ) edit_playlist
113:          ;;

114:    d|D ) delete_playlist
115:          ;;

116:    g|G ) generate_playlist
117:          ;;

118:    p|P ) play_mp3
119:          ;;

120:    1)    copy_mp3
121:          ;;

122:    2)    delete_mp3
123:          ;;

124:    q|Q ) exit
125:          ;;

126:esac

127:

i got rid of the quotations on the letters thinking that might fix it but nope


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