0

I've done a shell (bash) script that applies predefined rules to files dropped into the terminal.

It works quite well but because it uses 'read' it requires to press Enter once the files are dropped to the term window

This is part of the current code

    while true ; do
        echo "Drop file(s) here then press [ENTER]:"
        echo "( x,q or exit,quit )"
        read -p "> " read_file
        while read dropped_file ;do
            if [ -e ${dropped_file} ] ; then
                ...bunch of code here...
            else
                [[ "${dropped_file}" == *[xXqQ]* ]] && exit 1
            fi
        done <<< $(echo ${read_file} | tr " " "\n")
        clear
    done

I'd like to omit to press Enter each time I drop files and I was wondering if there is some wizardry to avoid to interact with the keyboard, except when I want to quit the script

Any help would be appreciated

Thanks in advance

  • Do you like https://stackoverflow.com/a/36870432/3220113 or https://unix.stackexchange.com/a/24955/57293 ? – Walter A Nov 22 '20 at 16:06
  • Thanks, this is not what I was looking for but thanks anyway, I solved using -n1 to decompose the whole file_path then to recompose it again – Michele Frau Nov 22 '20 at 20:48
  • 1
    @MicheleFrau : Your program does not **interact with the keyboard**, it reads from standard input, which in general is a file, but if no file is supplied, stdin is bound to your tty, which in turn is bound to the keyboard. bash itself has no idea of "keys". As you already found out, `-n1` instructs to read just one character from stdin. However, I advice against assuming "someone presses a key" when designing a program which is supposed to process stdin. See also [here](https://stackoverflow.com/questions/8725925/how-to-read-just-a-single-character-in-shell-script). – user1934428 Nov 23 '20 at 09:00
  • @user1934428 thanks for the reply but there is a misunderstanding , probably I expressed myself badly because english is not my primary language and I tought my code snippet was self explanatory; my intent was to drop files onto the terminal to get their filepath, so with the code above I, my self not the script, had to interact with keyboard and press Enter ; as I wrote further I've solved using -n1 to decompose the dropped file_path character by character and then to recompose it later : not elegant nor beatiful but it does the job it's meant for – Michele Frau Nov 23 '20 at 17:55
  • Well, you do not **have** to interact with the keyboard. You could also provide an input file with the necessary filennames and x/q commands using a text editor and feed this via stdin (no repeated keyboard entry necessary). I agree that using `-n 1` is not an elegant solution, but this is the price you have to pay for an ugly user interface. I would design the script by either expecting the list of files to be dropped be stored in a separate file (and passing this file to your script), or accept the list of the files to be dropped on the command line. – user1934428 Nov 24 '20 at 07:57

2 Answers2

0

EDIT

I've solved with this

while true ; do
    read -sn1 read_file
    while read splitted_filename; do
    dropped_file="${dropped_file}${splitted_filename}"
    done <<< $(echo ${read_file})
    functionAction
    [[ "${dropped_file}" == [xXqQ] ]] && exit 1
done 
-1

I think you can use the yes, printf, or the expect command and use the \n key, which should be able to perform what you are looking for.

Shaqil Ismail
  • 1,794
  • 1
  • 4
  • 5