[英]exec not found (because of the file descriptor)
這是我的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重定向說:
打開的文件由以零開頭的十進制數表示。 最大可能的值是實現定義的; 但是,所有實現都應支持至少0到9(包括0和9),以供應用程序使用。
因此,其中一些實現支持最大9 fd。
如果你想使用更多,使用全功能的殼狀bash
,改變/bin/sh
使用/bin/bash
來自http://www.gnu.org/software/bash/manual/bashref.html#Redirections上的GNU bash手冊:
應謹慎使用使用大於9的文件描述符的重定向,因為它們可能與shell在內部使用的文件描述符沖突。
通常,系統僅分配前3個描述符:
stdin
,通常從終端輸入 stdout
,通常輸出到終端 stderr
,通常也輸出到終端,但保留用於錯誤消息,因此不會意外地將它們發送到管道。 您可以自由使用更高的數字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.