&1-6ren">
gpt4 book ai didi

linux - 找不到 exec(因为文件描述符)

转载 作者:太空狗 更新时间:2023-10-29 11:20:14 25 4
gpt4 key购买 nike

这是我的 shell 脚本:

#!/bin/sh
exec 100>&1
exec 1>hello.txt
echo "hello exec"
echo "hello world"
exec 1>&100 100>&-

但是我运行的时候,系统提示:

exec: 2: 100: not found

当我使用命令lsof查看其文件描述符的信息时,它会提示:

 lsof -p 31931 -d 100
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
dbus-daem 1230 kaiwii 100u unix 0x00000000 0t0 3753938 socket
bash 31931 kaiwii cwd DIR 8,7 4096 6316395 /home/kaiwii/test
bash 31931 kaiwii rtd DIR 8,7 4096 2 /
bash 31931 kaiwii txt REG 8,7 822420 8208388 /bin/bash
bash 31931 kaiwii mem REG 8,7 1434180 1688385 /lib/i386-linux-gnu/libc-2.13.so
bash 31931 kaiwii mem REG 8,7 38500 1688435 /lib/i386-linux-gnu/libnss_nis-2.13.so
bash 31931 kaiwii mem REG 8,7 79672 1688425 /lib/i386-linux-gnu/libnsl-2.13.so
bash 31931 kaiwii mem REG 8,7 26400 1688427 /lib/i386-linux-gnu/libnss_compat-2.13.so
bash 31931 kaiwii mem REG 8,7 117960 1688372 /lib/i386-linux-gnu/ld-2.13.so
bash 31931 kaiwii mem REG 8,7 9736 1688395 /lib/i386-linux-gnu/libdl-2.13.so
bash 31931 kaiwii mem REG 8,7 223468 1687611 /lib/libncurses.so.5.7
bash 31931 kaiwii mem REG 8,7 42580 1688431 /lib/i386-linux-gnu/libnss_files-2.13.so
bash 31931 kaiwii mem REG 8,7 123384 5685786 /usr/share/locale-langpack/zh_CN/LC_MESSAGES/bash.mo
bash 31931 kaiwii mem REG 8,7 8322432 319489 /usr/lib/locale/locale-archive
bash 31931 kaiwii 0r CHR 136,0 0t0 3 /dev/pts/0
bash 31931 kaiwii 1u CHR 136,0 0t0 3 /dev/pts/0
bash 31931 kaiwii 2u CHR 136,0 0t0 3 /dev/pts/0
bash 31931 kaiwii 255u CHR 136,0 0t0 3 /dev/pts/0

由于我对命令exec不熟悉,尤其是修改文件描述符的方式,我完全不知道发生了什么!顺便说一下,当我将文件描述符 100 更改为 8 时,它运行正常!

最佳答案

这是因为您正在使用 /bin/sh 运行脚本,它可能链接到 dash 或其他最小的 shell 解释器。 XCU 2.7 重定向说:

Open files are represented by decimal numbers starting with zero. The largest possible value is implementation-defined; however, all implementations shall support at least 0 to 9, inclusive, for use by the application.

因此其中一些实现最多支持 9 个 fd。

如果你想使用更多,使用功能齐全的 shell,如 bash,将 /bin/sh 更改为 /bin/bash

关于linux - 找不到 exec(因为文件描述符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9561300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com