Rusty
Rusty

Reputation: 389

View and choose files to delete from list

I have a bunch of graphs saved as png files. Most of them are not very useful, but some are very useful.

I want to write a script that will display them each one at a time and wait for me to hit y or n. If I hit n, delete it. If not, move on to the next one.

I'm running into two problems.

First, feh opens a new window so I have to alt-tab back into my shell in order to press y or n. Is it possible to have bash listen for any key presses including ones in different windows?

Second, I was trying to use read to listen for a character, but it says that -n is not a valid option. That same line works fine in the terminal though.

Any idea how to do this? Appreciate the help.

#! /bin/sh                                                                                                                                                               

FILES=./*.png
echo $FILES
for FILE in $FILES
do
    echo $FILE
    feh "$FILE" &
    CHOICE="none"
    read -p "d to delete, any other key to keep: " CHOICE -n 1 -s
    killall feh
    if [$CHOICE -eq "d"]
    then
        rm $FILE
    fi
done

Upvotes: 4

Views: 207

Answers (1)

sarnold
sarnold

Reputation: 104090

This might only be tangential to your question, but your script is set to use /bin/sh (the POSIX shell) for execution but you are probably using /bin/bash as your interactive terminal shell. read handles differently depending upon which shell you're using:

Here's the output of your one read command three different shells; the dash shell is used for /bin/sh on my system, but to make sure it handles the same when called as sh and as dash, I've run it twice, once with each name:

$ bash
$ read -p "d to delete, any other key to keep: " CHOICE -n 1 -s
d to delete, any other key to keep: d
bash: read: `-n': not a valid identifier
$ exit
$ dash
$ read -p "d to delete, any other key to keep: " CHOICE -n 1 -s
d to delete, any other key to keep: d
read: 1: -n: bad variable name
$ 
$ pdksh
$ read -p "d to delete, any other key to keep: " CHOICE -n 1 -s
pdksh: read: -p: no coprocess
$ $ sh
$ read -p "d to delete, any other key to keep: " CHOICE -n 1 -s
d to delete, any other key to keep: d
read: 1: -n: bad variable name
$ 
$ 

Upvotes: 2

Related Questions