1

I would like to create a temporary shell script, and pass it as a parameter to another script, i.e. a callback hook. Is it possible to use process substitution for that?

In this example, aria2c allows a hook as a parameter. aria2c will call that hook with a few parameters once the page is downloaded. Ideally, I would like aria2c to call my "virtual" script, without first making any temp files, like so:

aria2c \
  --on-download-complete <(echo '#!/bin/sh'; echo 'echo "Called with [$1] [$2] [$3]"') \
  https://aria2.github.io/

but as a result i get a permissioning error:

Could not execute user command: /dev/fd/63: Permission denied
Yuri Astrakhan
  • 8,808
  • 6
  • 63
  • 97
  • 2
    Because `aria2` uses [`execlp`](https://github.com/aria2/aria2/blob/master/src/util.cc#L2243), I believe it is impossible, and you have to create a temporary file. – KamilCuk Feb 05 '20 at 21:23
  • "I would like my script to call aria2c directly" ? Do you mean "I would like aria2c to call my script directly" ? – jhnc Feb 06 '20 at 03:16
  • @jhnc I meant that I would like aria2c to call a script which does not exist as a file, but rather only as a `/dev/fd`. I clarified the language in the post. – Yuri Astrakhan Feb 06 '20 at 23:59

1 Answers1

0

As pointed out in the comments, aria2c expects the hook argument to be the filename of a program to execute as the callback. However the filename that results from process substitution is not such a program; process substitution returns the filename of a named pipe.

You cannot do what you want without creating any files. However, a single static helper program and an exported bash function gets close:

/my/odc/helper

#!/bin/bash
__aria2c_odc_helper_function "$@"
exit

use as something like:

__aria2c_odc_helper_function(){
    echo "Called with [$1] [$2] [$3]"
}
export -f __aria2c_odc_helper_function

aria2c --on-download-complete /my/odc/helper \
    https://aria2.github.io/
jhnc
  • 11,310
  • 1
  • 9
  • 26
  • 1
    A way safer option would be to just export a function rather then a variable if using bash anyway. – KamilCuk Feb 07 '20 at 02:48
  • @KamilCuk Yes, that's true. – jhnc Feb 07 '20 at 02:57
  • Would this approach work if aria2c is called from python rather than bash? E.g. is it possible to add a shell function as an env var to the context of aria2c subprocess call? – Yuri Astrakhan Feb 09 '20 at 18:39
  • Probably not (I'm not sure one can export bash functions through a process that isn't bash), but a variant of the `eval` approach from original version of this answer would probably work (ie. export an environment variable that can be read by the subprocess). – jhnc Feb 10 '20 at 00:13