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 07:56 AM

Help with bash script
 
hello im trying to make a menu type mp3 playist program from a bash script. im stuck on assigining the menu the variable. Please if anyone can throw some feedback whats wrong here thanks in advance.
im using mplayer and playlist needs to reside in
/home/Fedora11 and mp3 will reside in /home/Fedora11/MP3
Code:

#!/bin/bash


    PLAYLIST='/home/Fedora11'

    mk_playlist()
    {

AUDIOPATH=$PLAYLIST/MP3

find "$AUDIOPATH" -type f -name '*.[Mm][Pp]3' > /home/Fedora11/playlist



while  :                            #This always returns true (see /bin/true and :)
do
echo -e '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 ==> \c'

read ANS


case "$ANS" in
c  | C  ) echo e- "Create A Play List? --> \c"
          read CREATEPLAYLIST
         
          echo -e "$CREATEPLAYLIST" >> /home/Fedora11
          ;;
esac
done


irmin 02-16-2010 08:18 AM

First of all you forgot to close the function definition of mk_playlist().

Quote:

echo -e "$CREATEPLAYLIST" >> /home/Fedora11
What is this line supposed to do?

m2dmhot 02-16-2010 08:30 AM

Quote:

Originally Posted by irmin (Post 3865579)
First of all you forgot to close the function definition of mk_playlist().



What is this line supposed to do?

Im not sure?? im a newb.. Im very frusterated upon hours of reasearching and reading.. I am just trying to do this the best way i been taking bits and pieces from what people are telling me to do..
I need to create a playlist menu. list i have put in the script.
be able to read the mp3 in /home/Fedora11/mp3 and make a playlist
in /home/Fedora11 but i have the sligtest clue even where to start??

catkin 02-16-2010 08:44 AM

Quote:

Originally Posted by m2dmhot (Post 3865593)
Im not sure?? im a newb.. Im very frusterated upon hours of reasearching and reading.. I am just trying to do this the best way i been taking bits and pieces from what people are telling me to do.

Welcome to LQ :)

Relax. Smile. Count to 100 while breathing deeply :)

Surely we can help you. The first step is to be very clear about what you want to do. We're pretty smart but not mind readers so you are going to have to tell us. You're new to all this and probably don't know exactly what you want to do but that's OK -- we can work together on that. Tell us the best you can, tell us what you are uncertain about and we can go forward from there.

penguiniator 02-16-2010 09:11 AM

Try typing 'help select' at a bash prompt.

m2dmhot 02-16-2010 09:14 AM

ok i am using mplayer.
i would like the ability to create display and remove playlists.
Playlist will reside in /home/Fedora11 and the mp3s are in
/home/Fedora11/MP3
I also need a database file that shows the path directory.
ability to play mp3 and add mp3 to the playlist and check for duplicates.
I want it in sort of a menu type options something like this
Code:

## Functions for each task
create_playlist() {
  : put appropriate commands here
}

edit_playlist() {
  : put appropriate commands here
}

delete_playlist() {
  : put appropriate commands here
}

generate_playlist() {
  : put appropriate commands here
}

play_mp3() {
  : put appropriate commands here
}

copy_mp3() {
  : put appropriate commands here
}

delete_mp3() {
  : put appropriate commands here
}

## 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


I have been bouncing back from forums and reading but really unsure what the next step to acutally make the options work. If i could get one to work i could try to geth the others to work..
Also a friend said that this has to be linear so the first part of the script has to come after the menu??

catkin 02-16-2010 09:59 AM

Thanks for more info :)

I don't use any music players so can't help with that side of things but I can help with the scripting side.

Shell functions need to be defined before they are called so your layout is the right one.

In your original code you had echo e-

You can check that the menu is calling the functions by something like
Code:

edit_playlist() {
  echo "DEBUG: function ${FUNCNAME[0]} started"
  : put appropriate commands here
}


m2dmhot 02-16-2010 10:05 AM

Ok i got some progress here. Tackled the play mp3 so far
modded it a bit with litle help i added this cmd but it doesnt like when i enter in songs with spaces how can i fix this??
Code:

play_mp3() {
    cd /home/Fedora11/MP3/
    ls -v *.mp3
    echo -e

    echo -e "Please Select Your Song --> \c"
    read SONG
    mplayer $SONG
}


irmin 02-16-2010 10:22 AM

read splits the input into words. The delimiters are in IFS.

Try the following:
Code:

IFS= read SONG
mplayer $SONG


schneidz 02-16-2010 10:35 AM

this mite help:
http://www.linuxquestions.org/questi...center-719104/

m2dmhot 02-16-2010 10:55 AM

Quote:

Originally Posted by irmin (Post 3865718)
read splits the input into words. The delimiters are in IFS.

Try the following:
Code:

IFS= read SONG
mplayer $SONG


Just tried that it keeps looking for individual names..

Code:

play_mp3() {
    cd /home/Fedora11/MP3/
    ls -v *.mp3
    echo -e

    echo -e "Please Select Your Song --> \c"
    IFS= read DOG
    mplayer $DOG
}


m2dmhot 02-16-2010 11:09 AM

Got it now...
Code:

play_mp3() {
    cd /home/Fedora11/MP3/
    ls -v *.mp3
    echo -e

    echo -e "Please Select Your Song --> \c"
    IFS= read DOG
    mplayer "/home/Fedora11/MP3/$DOG"


m2dmhot 02-16-2010 12:04 PM

Ok onto next line..
I need help with the copy file function. I got the basic commands but how can you check for duplicates??

Code:

copy_mp3() {
    cd /home/Fedora11/MP3/
    ls -v *.mp3
   
    echo -e "Please Select Your Song To Copy --> \c"
    read DONKEY
    cp $DONKEY /home/Fedora11/MP3_1/
}


worm5252 02-16-2010 12:16 PM

You could create a temp file with a directory listing. Then you can read the file line by line to see if the file exist. If it does, then do nothing, if it doesn't then copy the file.

Code:

copy_mp3() {
    cd /home/Fedora11/MP3/
    ls -v *.mp3 >> /home/Fedora11/MP3/tempdir.lst # Create Temp file with Directory listing

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

    cat /home/Fedora11/MP3/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 /home/Fedora11/MP3_1/
    done   

    rm /home/Fedora11/MP3/tempdir.lst # Delete the temporary file containing the directory listing
}


worm5252 02-16-2010 12:19 PM

This Post would probably do better in the Programming forum


All times are GMT -5. The time now is 12:52 PM.