here is my shell script:
#!/bin/sh
exec 100>&1
exec 1>hello.txt
echo "hello exec"
echo "hello world"
exec 1>&100 100>&-
but when I run it, the system prompts that:
exec: 2: 100: not found
And when I use the command lsof
to check the info of its file descriptor, it prompts that:
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
For the sake that I am not familiar to the command exec
especially about how to revise the file descriptor, I have no idea what happened! By the way, when I change the file descriptor 100 to 8, it runs okay!
This is because you're running the script with /bin/sh
which probably links to dash
or other minimal shell interpreter. XCU 2.7 Redirection says:
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.
So some of these implementations support maximum 9 fd.
If you want to use more, use a full featured shell like bash
, change /bin/sh
with /bin/bash
From the GNU bash manual at http://www.gnu.org/software/bash/manual/bashref.html#Redirections :
Redirections using file descriptors greater than 9 should be used with care, as they may conflict with file descriptors the shell uses internally.
In general, only the first 3 descriptors are assigned by the system:
stdin
, usually input from a terminal stdout
, usually output to a terminal stderr
, also usually output to a terminal, but reserved for error messages so they don't get sent down a pipe accidentally. You're free to use the higher numbers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.