In order to pass my script's stdin to stdin of a command in an alacritty instance it spawns, I'm trying to redirect it to a file in the background (so that the command can start reading it):

t="$(mktemp --directory)"
#mkfifo "$in"
#mkfifo "$out"

cat - 3>"$in" >&3 &
alacritty -e /bin/sh -c "cat '$in' | {...} > '$out'" >&2

cat "$out"

As you can see, I tried making it a named pipe, and also using fd3 thinking the problem might be some sort of special casing that backgrounded processes don't get a 'stdin' file descriptor.

In case it's not clear, the reason for trying is that the input may be large enough that I want to start the ... process without waiting for EOF.

I'm probably going about this all wrong - is there a way to 'reassign' 'my' file descriptors to the shell launched by alacritty? That was my first thought, but I couldn't find anything on passing file descriptors from a shell, just C (etc.) pointers.

  • It is not really clear what is not working as expected. Where does the input of the outer script come from? For completeness: what shell is your script running in?
    – fra-san
    Oct 12, 2020 at 17:27
  • @fra-san I will use it as something | this-script | something-else, where something varies, essentially it's to be a poor man's dmenu for some cases. Shell - currently just targeting POSIX /bin/sh, but requiring bash is fine if it somehow really makes life easier.
    – OJFord
    Oct 12, 2020 at 18:59

1 Answer 1

cat - 3>"$in" >&3 &

If you run that from a script, cat's stdin will be redirected from /dev/null.

If you run it from an interactive shell, cat will be stopped by a SIGTTIN signal.

In any case, it won't write anything to the "$in" pipe.

If that's part of a script, the "solution" (scarequotes because I'm not really clear what you're trying to do) is to "save" the stdin with an exec [fd]<&0 redirection, and then redirect the input of the background process from that [fd]. Compare:

echo yes | sh -c 'cat & sleep 1'

which won't write anything to stdout, with

echo yes | sh -c 'exec 5<&0; cat <&5 & sleep 1'

which will do.

  • exec's file descriptor behaviour was what I was missing, thank you! The file was because for some reason I thought the file descriptors seen by alacritty -e /bin/sh would be different to my script's, a different PID namespace if that's correct terminology. So, exec 5<&0; alacritty -e /bin/sh -c '<&5 ...' works well - and I can get the output from redirecting to a file - but is there a way to also redirect that shell's &1, so I can 'capture' it in and output it from the script?
    – OJFord
    Oct 12, 2020 at 18:56
  • I don't know how I messed that up earlier, just a regular 4>&1 on that command then works fine. Thanks again!
    – OJFord
    Oct 12, 2020 at 21:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .