Using bash process substitution for a sudo command

后端 未结 4 835
逝去的感伤
逝去的感伤 2021-02-06 01:41

I would like to use bash process substitution for a sudo command.

For example, here\'s a non-sudo command that works for me:

$ cat <(echo \"installed.         


        
相关标签:
4条回答
  • 2021-02-06 01:55
    sudo bash  -c 'cat <(echo "installed.txt")'
    
    0 讨论(0)
  • 2021-02-06 01:56

    The behaviour described in the question is due to the design of sudo.

    $ sudo cat <(echo "installed.txt")
    cat: /dev/fd/63: Bad file descriptor
    

    The error occurs because sudo has a default behaviour which closes file descriptors except for standard input, output and error. As described on the man page:

    By default, sudo will close all open file descriptors other than standard input, standard output and standard error when executing a command.

    This behaviour can be overridden with the -C (or --close-from) option to specify a file descriptor number below which files should not be closed. However, use of this option must be permitted by an administrator: the following needs to be added to /etc/sudoers

     Defaults closefrom_override
    

    With that in place, the command will work if -C is used:

    $ sudo -C64 cat <(echo "installed.txt")
    installed.txt
    

    (here the number 64 was given because it was greater than the 63 in the error message).

    0 讨论(0)
  • 2021-02-06 02:11

    The best approach is probably to put everything in a script, and run that script with sudo.

    0 讨论(0)
  • 2021-02-06 02:16

    Try doing this in your shell :

    $ sudo bash -c 'cat <(echo "installed.txt for UID=$UID")'
    installed.txt for UID=0
    
    0 讨论(0)
提交回复
热议问题