Search code examples
bashcoproc

"${COPROC[0]}"/"${COPROC[1]}": Bad file descriptor


I have a script which i am using as a filter:

#!/usr/bin/env gjs

const { Gio } = imports.gi;

let file = Gio.File.new_for_path(ARGV[0]);
let fileInfo = file.query_info('*', Gio.FileQueryInfoFlags.NONE, null);
let icons = fileInfo.get_icon().get_names();

if (typeof icons[1] === 'undefined') {
    // does not exist
    print(icons[0]);
}
else {
    print(icons[1]);
}

It gives icon when a path is given.

I am calling it like:

#!/usr/bin/env bash

shopt -s lastpipe

coproc get_icon_from_path_stdin

while IFS= read -r line; do

    echo "$line" >&"${COPROC[1]}"
    read -r icon <&"${COPROC[0]}"
    echo -en "$line"
    echo -en "\0icon\x1f${icon}\n"
done < <(cat $HOME/.dotfiles/rofi-bookmarks-list) | rofi -dmenu -i -theme-str 'window {fullscreen:true;}' | read select

if [ ${PIPESTATUS[1]} -eq 0 ]; then
    nemo "$select"
fi

The point is, $HOME/.dotfiles/rofi-list has 10,000+ file path so executing this function this many times is slow. What I want is to execute this function once and then just send file in stdin and get stdout from it.

However, this gives error like

/home/ismail/.dotfiles/.resources/bash-scripts-rofi/rofi-bookmarks: line 21: "${COPROC[1]}": Bad file descriptor
/home/ismail/.dotfiles/.resources/bash-scripts-rofi/rofi-bookmarks: line 22: "${COPROC[0]}": Bad file descriptor

what am i doing wrong here?


Solution

  • Bash closes COPROC in subshells. You have to just pick another descriptors.

    exec 10>&"${COPROC[1]}"
    exec 11<&"${COPROC[0]}"
    
    ...
         echo "$line" >&10
         read -r icon <&11