使用bash进程替换拒绝文件描述符权限?
发布时间:2020-12-15 22:37:36 所属栏目:安全 来源:网络整理
导读:我有一个bash脚本,我想在标准输出中与用户通信,但也通过文件描述符将命令发送到子进程 – 如下所示: # ...# ...echo "Hello user,behold a cleared gnuplot window"# pass the string "clear" to gnuplot via file descriptor 3echo "clear" 3 所以我认为我
我有一个bash脚本,我想在标准输出中与用户通信,但也通过文件描述符将命令发送到子进程 – 如下所示:
# ... # ... echo "Hello user,behold a cleared gnuplot window" # pass the string "clear" to gnuplot via file descriptor 3 echo "clear" >&3 所以我认为我可以通过首先启动子进程来“设置它”: #!/bin/bash # Initiate(?) file descriptor 3,and let it direct to a newly # started gnuplot process: exec >3 >( gnuplot ) 但这会产生错误: /dev/fd/63: Permission denied 这是预期的吗? 我不明白发生了什么. (我做错了什么?可能是我的系统有一些特殊的安全设置,不允许我正在尝试做什么?(运行Ubuntu Linux 12.10.)) “解决方法” – 以下似乎与我正在尝试的内容相同,并且可以正常工作: #!/bin/bash # open fd 3 and direct to where fd 1 directs to,i.e. std-out exec 3>&1 # let fd 1 direct to a newly opened gnuplot process exec 1> >( gnuplot ) # fd 1 now directs to the gnuplot process,and fd 3 directs to std-out. # I would like it the other way around. So we'll just swap fd 1 and 3 # (using an extra file descriptor,fd 4,as an intermediary) exec 4>&1 # let fd 4 direct to wherever fd 1 directs to (the gnuplot process) exec 1>&3 # let fd 1 direct to std-out exec 3>&4 # let fd 3 direct to the gnuplot process exec 4>&- # close fd 4 或者,作为一个班轮: #!/bin/bash exec 3>&1 1> >( gnuplot ) 4>&1 1>&3 3>&4 4>&- 为什么这样可行,但初始版本不是? 任何帮助非常感谢. $bash --version GNU bash,version 4.2.37(1)-release (x86_64-pc-linux-gnu) [...] 解决方法
你有一个错字;使用exec 3> >(gnuplot)而不是exec> 3>(gnuplot).
顺便说一句,是的,这是预期的. exec> 3>(gnuplot)将stdout重定向到名为3的文件,然后尝试执行>(gnuplot)(转换为/ dev / fd / 63)作为程序. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |