2

相同的命令:echo 1 > filename创建不同的文件名:

$ sh -c 'echo $LANG >=с=.sh' && ls *.sh | od -c
0000000   = 321   =   .   s   h  \n
0000007

$ bash -c 'echo $LANG >=с=.bash' && ls *.bash | od -c
0000000   = 321 201   =   .   b   a   s   h  \n
0000012

с字符在哪里U+0441-西里尔小写字母 ES。很明显,sh吃掉了utf-8编码中的第二个字节。

$ ls *sh
=?=.sh  =с=.bash

$LANG在这两种情况下是:

$ cat *sh
en_US.utf8
en_US.utf8

sh链接到dash我的系统:

$ apt-cache show dash | grep -i version
Version: 0.5.5.1-7ubuntu1

stty iutf8已设置。

是否有任何设置dash不允许破坏多字节字符?

我在手册中没有看到任何关于字符编码的提及:

$ man dash | grep -i encoding
$ man dash | grep -Pi 'multi.*byte'

更新

utf-8 编码中字符的第二个字节在 C 中是 '\201'有符号字符(或无符号字符)。'с'-127129

在源代码 ( apt-get source dash) 中-127搜索结果:

src/parser.h:38:#define CTL_FIRST -127      /* first 'special' character */
src/parser.h:39:#define CTLESC -127     /* escape next character */

搜索CTLESC导致rmescapes()以下片段的宏src/expand.c:expandarg()

/*
 * TODO - EXP_REDIR
 */
if (flag & EXP_FULL) {
    ifsbreakup(p, &exparg);
    *exparg.lastp = NULL;
    exparg.lastp = &exparg.list;
    expandmeta(exparg.list, flag);
} else {
    if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
        rmescapes(p);
    sp = (struct strlist *)stalloc(sizeof (struct strlist));
    sp->text = p;
    *exparg.lastp = sp;
    exparg.lastp = &sp->next;
}

TODOXXX暗示更新的版本可能会有所帮助。debian/dash.README.source指着:

$ git clone http://smarden.org/git/dash.git/
$ cd dash

有两个分支:

$ git br
* debian-sid
  release+patches

debian-sid转义字节被删除。在release+patches 分支上grep找到丢失的字节。

$ ./configure
$ make && rm *.dash -f; ./dash -c 'echo 1 >fсf.dash' && 
> ls *.dash | od -c | grep 201

git diff debian-sid...release+patches显示rmescapes()已删除release-patches

diff --git a/src/expand.c b/src/expand.c
index e4c4c8b..f2f964c 100644
--- a/src/expand.c
+++ b/src/expand.c
...
@@ -213,8 +210,6 @@ expandarg(union node *arg, struct arglist *arglist, int flag)
                exparg.lastp = &exparg.list;
                expandmeta(exparg.list, flag);
        } else {
-               if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
-                       rmescapes(p);
                sp = (struct strlist *)stalloc(sizeof (struct strlist));
                sp->text = p;
                *exparg.lastp = sp;
@@ -412,7 +407,7 @@ lose:
 }

目前尚不清楚这些更改是否会包含在dash 0.5.6.1Ubuntu 中。

目前唯一的方法是发出命令:

$ sh -c 'echo 1 >fсf.dash' &&  ls *.dash | od -c | grep 201

工作是重新配置shbash

$ sudo dpkg-reconfigure dash

还有其他选择吗?

4

1 回答 1

1

在我尝试的几个 shell(或版本)中,只有 Dash 和 Busybox Ash 失败了。

$ for sh in bash2.05b bash3.2 bash4.0 bash4.1 bash4.2 dash zsh ksh pdksh mksh ash; do $sh -c 'locale > с.$0'; done
$ csh -c 'locale > с.csh'
$ fish -c 'locale > с.fish'
$ ls -1
?.ash
?.dash
с.bash2.05b
с.bash3.2
с.bash4.0
с.bash4.1
с.bash4.2
с.csh
с.fish
с.ksh
с.mksh
с.pdksh
с.zsh

内容都一样。

来自man dash

只有 POSIX 指定的功能,以及一些 Berkeley 扩展,才被合并到这个 shell 中。本手册页不打算作为教程或 shell 的完整规范。

POSIX说:

POSIX 语言环境包含 Portable Character Set 中的字符,这些字符具有 LC_CTYPE 中列出的属性。在其他语言环境中,任何附加字符的存在、含义和表示都是特定于语言环境的。

其他字符的宽字符代码是语言环境和实现定义的。... POSIX.1-2008 没有提供定义宽字符代码集的方法。

于 2011-03-02T01:12:06.610 回答