gpt4 book ai didi

bash - 使用符号链接(symbolic link)时 sh 和 bash 之间的区别

转载 作者:行者123 更新时间:2023-11-29 09:26:29 25 4
gpt4 key购买 nike

我有一个 shell 脚本,它使用 process substitution

脚本是:

  #!/bin/bash
while read line
do
echo "$line"
done < <( grep "^abcd$" file.txt )

当我使用 sh file.sh 运行脚本时,我得到以下输出

$sh file.sh
file.sh: line 5: syntax error near unexpected token `<'
file.sh: line 5: `done < <( grep "^abcd$" file.txt )'

当我使用 bash file.sh 运行脚本时,脚本运行正常。

有趣的是,sh 是一个映射到 /bin/bash软链接(soft link)

$ which bash
/bin/bash
$ which sh
/usr/bin/sh
$ ls -l /usr/bin/sh
lrwxrwxrwx 1 root root 9 Jul 23 2012 /usr/bin/sh -> /bin/bash
$ ls -l /bin/bash
-rwxr-xr-x 1 root root 648016 Jul 12 2012 /bin/bash

我测试以确保符号链接(symbolic link)在我的 shell 中使用以下内容:

$ ./a.out
hello world
$ ln -s a.out a.link
$ ./a.link
hello world
$ ls -l a.out
-rwx--x--x 1 xxxx xxxx 16614 Dec 27 19:53 a.out
$ ls -l a.link
lrwxrwxrwx 1 xxxx xxxx 5 May 14 14:12 a.link -> a.out

我无法理解为什么 sh file.sh 不作为 /bin/bash file.sh 执行,因为 sh 是一个符号链接(symbolic link)到 /bin/bash

任何见解将不胜感激。谢谢。

最佳答案

作为sh调用时,bash进入posix 读取启动文件后的模式。在 posix 模式下无法识别进程替换。根据 posix,<(foo)应该直接从名为 (foo) 的文件输入. (嗯,就是按照我读的标准,语法很多地方有歧义。)

编辑:来自 bash manual :

The following list is what’s changed when ‘POSIX mode’ is in effect:
...
Process substitution is not available.

关于bash - 使用符号链接(symbolic link)时 sh 和 bash 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16551033/

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