0

I'd like to delete some mp3 files interactively as follows

$ for i in *.mp3; do mplayer "$i"; echo "$i"; sleep 5; interactive_code; done

The interactive_code should delete "$i" file when I press rm, move it to tmp dir when I press mv or continue in a loop when I press space.

xralf
  • 16,149
  • 29
  • 101
  • 149
  • 1
    it might simplify things to use "continue in a loop if I press ENTER" instead of space-bar; would that be acceptable? – Jeff Schaller Sep 01 '17 at 00:57
  • Why sleep 5? Wouldn't you want to just wait for input? – Wildcard Sep 01 '17 at 00:58
  • Have you tried to use a menu option in bash? I believe this can solve your problem since you are required to enter some input in order to follow the original loop. http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_06.html – Christopher Díaz Riveros Sep 01 '17 at 01:21
  • 1
    @ChristopherDíazRiveros, I recommend [*not* using tldp](https://unix.stackexchange.com/a/59425/135943). Instead, try the [Wooledge Bash Wiki](http://mywiki.wooledge.org/BashGuide/TestsAndConditionals#Choices_.28case_and_select.29). – Wildcard Sep 01 '17 at 01:32
  • @JeffSchaller Yes, key is not important – xralf Sep 01 '17 at 06:03
  • @Wildcard Yes, I wasn't sure if there is wait function. – xralf Sep 01 '17 at 06:04
  • What do you exactly mean by "continue in a loop"? Do you want to play the song indefinitely until you press `rm` or `mv`? Or neither delete nor move the file and continue the `for` loop with the next file? I made an answer assuming the second scenario. – nxnev Sep 01 '17 at 06:15
  • @nxnev Yes, the second scenario. – xralf Sep 01 '17 at 08:52

3 Answers3

3

This script should do the work:

#!/usr/bin/env bash

for i in ./*.mp3; do

  mplayer "${i}"
  printf '%s\n' "${i}"

  read -p 'What to do?: ' -r ans

  if [[ "${ans}" == 'rm' ]]; then
    rm "${i}"
  elif [[ "${ans}" == 'mv' ]]; then
    mv "${i}" 'tmp'
  fi

done

PS: It assumes that the tmp directory already exists and is in your current working directory. If it is in another location or you were refering to the /tmp directory, just change that part of the code.

nxnev
  • 3,634
  • 2
  • 12
  • 28
  • Read comments; given the `read` command, no need for `sleep`. – Wildcard Sep 01 '17 at 06:18
  • btw. When I type `$ man read` it will display man page for read(2) not [this](http://linuxcommand.org/lc3_man_pages/readh.html). Can I see it from the command line as well? – xralf Sep 01 '17 at 09:03
  • 1
    @xralf `read` is a shell builtin, so you should type `$ help read` instead. – nxnev Sep 01 '17 at 09:05
1

Untested. This isn't quite what you asked for as it gives two prompts. Type 'y' and press enter on the first prompt to move the file to /tmp (in which case second prompt is skipped), or pass 'y' to the second prompt to remove the file. Ignore both prompts (press enter) to continue with no action.

find *.mp3 -type f -exec mplayer {} \; \( \
  -ok mv -i {} /tmp \; -o \
  -ok rm {} \; \)
Wildcard
  • 35,316
  • 26
  • 130
  • 258
0

This gives the user a menu after playing a file:

TMPDIR=${TMPDIR:-/tmp}

for name in ./*.mp3; do
    test -f "$name" || continue

    printf 'Playing "%s"...\n' "$name"
    mplayer "$name"

    select ch in "Move $name to $TMPDIR" "Remove $name"; do
        case "$REPLY" in
            1)  mv "$name" "$TMPDIR" ;;
            2)  rm "$name" ;;
            *)  echo 'Invalid choice' >&2
                continue ;;
        esac
        break
    done
done

This script does not require bash, any sh shell should do.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936