为什么当称为SH不bash的行为不同 [英] Why does bash behave different when called as sh

查看:137
本文介绍了为什么当称为SH不bash的行为不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ubuntu机默认的shell设置为bash和$ PATH中左右逢源的二进制:

  $其中的bash
/斌/庆典
$其中SH
/ bin / sh的
$ LL / bin / sh的
lrwxrwxrwx 1根根4个2013年3月6日/ bin / sh的 - >庆典*

但是,当我试图调用使用内联文件描述符(仅Bash可以处理,但没有一个脚本SH)这两个调用不同的表现:

  $。 ./inline-pipe
到达
$庆典./inline-pipe
到达
$ SH ./inline-pipe
./inline-pipe:6号线:附近意外的标记'&LT语法错误;'
./inline-pipe:行6:'做< ≤(回声达)'

该示例脚本我指的是看起来像

 #!/ bin / sh的
而读线;做
如果[$线==达到了]];然后回声达到了;科幻
完成< ≤(回声达)

真正的一个是有点长:

 #!/ bin / sh的
声明-A元素
而读线

    在$ ELE(回声$线| grep的-o[A-Z] *:[^] *)
    做
        ID = $(回声$ ELE |切-d:-f 1)
        元素[$ ID] = $(回声$ ELE |切-d:-f 2)
    DONE
完成< ≤(ADB设备-L)
回声$ {元素[*]}


解决方案

庆典被用来作为 SH ,它(大部分)限制自身在POSIX标准中的功能。进程替换是不是那些功能之一,因此错误。

I have an ubuntu machine with default shell set to bash and both ways to the binary in $PATH:

$ which bash
/bin/bash
$ which sh
/bin/sh
$ ll /bin/sh
lrwxrwxrwx 1 root root 4 Mar  6  2013 /bin/sh -> bash*

But when I try to call a script that uses the inline file descriptor (that only bash can handle, but not sh) both calls behave differently:

$ . ./inline-pipe
reached
$ bash ./inline-pipe
reached
$ sh ./inline-pipe
./inline-pipe: line 6: syntax error near unexpected token `<'
./inline-pipe: line 6: `done < <(echo "reached")'

The example-script I am referring to looks like that

#!/bin/sh
while read line; do
if [[ "$line" == "reached" ]]; then echo "reached"; fi
done < <(echo "reached")

the real one is a little bit longer:

#!/bin/sh
declare -A elements
while read line
do
    for ele in $(echo $line | grep -o "[a-z]*:[^ ]*")
    do
        id=$(echo $ele | cut -d ":" -f 1)
        elements["$id"]=$(echo $ele | cut -d ":" -f 2)
    done
done < <(adb devices -l)
echo ${elements[*]}

解决方案

When bash is invoked as sh, it (mostly) restricts itself to features found in the POSIX standard. Process substitution is not one of those features, hence the error.

这篇关于为什么当称为SH不bash的行为不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆