2015-06-21 468 views
3

我遇到了一个奇怪的问题。当exec中使用变量时,“exec not found”

mkfifo "spch2008" 
exec 100<>"spch2008" 

没关系。但是,当我使用变量来代替“100”时,发生错误。

高管:100:找不到

PIPE_ID=100 
mkfifo "spch2008" 
exec ${PIPE_ID}<>"spch2008" 

我不知道原因。请高兴我,谢谢。

+0

虽然没有文件描述符数量没有禁止,还有的一般使用**使用文件重定向时,下一个可用的**。 (如果是你的第一个重定向,就是'3')(例如'stdin-0','stdout-1','stderr-2','redirect-3')至于你的问题,这是一个奇怪的问题,但似乎你遇到了shell变量没有被输入(例如'int','char'等等)的限制。你使用'exec $ var <> fifo'没有办法确保'$ {PIPE_ID}'实际上是一个有效的数字。 –

回答

3

这是由于shell没有在重定向操作符的左侧执行变量扩展引起的。您可以使用一种变通方法:

eval exec "${PIPE_ID}"'<>"spch2008"' 

这将迫使壳里做变量扩展,生产

eval exec 100'<>"spch2008"' 

然后eval内置养活命令外壳,这将有效地执行

exec 100<>"spch2008" 
0

I/O重定向不允许变量通常指定的文件描述符,不只是在<> redirectio的背景下ñ。

考虑:

$ cat > errmsg      # Create script 
echo "[email protected]" >&2      # Echo arguments to standard error 
$ chmod +x errmsg     # Make it executable 
$ x=2 
$ ./errmsg Hi      # Writing on standard error 
Hi 
$ ./errmsg Hi ${x}>&1    # Writing on standard error 
Hi 2 
$ ./errmsg Hi 2>&1     # Redirect standard error to standard output 
Hi 
$ ./errmsg Hi 2>/dev/null   # Standard error to /dev/null 
$ ./errmsg Hi ${x}>/dev/null  # Standard output to /dev/null 
Hi 2 
$