【问题标题】:IRIX bash shell expands expression in single quotes, yet shouldntIRIX bash shell 用单引号扩展表达式,但不应该
【发布时间】:2010-07-20 15:36:11
【问题描述】:

在 shell 中,我输入一个单引号,然后是回车,然后是一系列行,然后是另一个单引号:

root@aim:/root > '
> @stat = lstat($ARGV[0]);
> if (!@stat) {
if (@stat = lstat($ARGV[0]);) {
> print "nil\n";
> exit 0;
>  }
> '

但是,如果您注意到 shell 的解释输出:

bash: 
@stat = lstat($ARGV[0]);
if (@stat = lstat($ARGV[0]);) {
print "nil\n";
exit 0;
 }
: command not found
root@aim:/root > uname -a
IRIX64 aim 6.5 04091957 IP27
root@aim:/root > echo $0
-bash
root@aim:/root > 

您注意到!@stat 被转换为@stat = lstat($ARGV[0]);

应该如何编写以下 shell 程序,以便其中的 perl 程序按字面意思解释?

tramp_perl_file_attributes () {
\perl -e '
@stat = lstat($ARGV[0]);
if (!@stat) {
   print "nil\n";
   exit 0;
}
if (($stat[2] & 0170000) == 0120000)
{
   $type = readlink($ARGV[0]);
   $type = "\"$type\"";
}
elsif (($stat[2] & 0170000) == 040000)
{
   $type = "t";
}
else
{
   $type = "nil"
};
$uid = ($ARGV[1] eq "integer") ? $stat[4] : "\"" . getpwuid($stat[4]) . "\"";
$gid = ($ARGV[1] eq "integer") ? $stat[5] : "\"" . getgrgid($stat[5]) . "\"";
printf(
   "(%s %u %s %s (%u %u) (%u %u) (%u %u) %u.0 %u t (%u . %u) -1)\n",
   $type,
   $stat[3],
   $uid,
   $gid,
   $stat[8] >> 16 & 0xffff,
   $stat[8] & 0xffff,
   $stat[9] >> 16 & 0xffff,
   $stat[9] & 0xffff,
   $stat[10] >> 16 & 0xffff,
   $stat[10] & 0xffff,
   $stat[7],
   $stat[2],
   $stat[1] >> 16 & 0xffff,
   $stat[1] & 0xffff
);' "$1" "$2"
}

【问题讨论】:

    标签: bash shell irix


    【解决方案1】:

    转义 ! 或禁用历史扩展(在键入命令时使用 set +H)

    由于某种原因,! 是从历史中扩展的(!! 扩展为您在命令行上键入的最后一个命令),这不应该出现在单引号之间。

    【讨论】:

    • 来自 bash 手册:“只有反斜杠 (\) 和单引号可以引用历史扩展字符。”
    • 可能是这样,但看看 OP 的问题,您可以清楚地看到 !@ 从历史中扩展。我不知道 why (因为我同意单引号应该防止这种情况)但它发生了。我已经更新了我的答案以更清楚地强调这一点。
    【解决方案2】:

    在 Debian Linux 下与 bash 4.1.5 完美配合。你在 IRIX 上有什么版本的 bash?也许它又旧又破?作为一种解决方法,您可以使用 here-docs:

    tramp_perl_file_attributes () {
    perl - "$1" "$2" <<'EOF'
    @stat = lstat($ARGV[0]);
    if (!@stat) {
       print "nil\n";
       exit 0;
    }
    if (($stat[2] & 0170000) == 0120000)
    {
       $type = readlink($ARGV[0]);
       $type = "\"$type\"";
    }
    elsif (($stat[2] & 0170000) == 040000)
    {
       $type = "t";
    }
    else
    {
       $type = "nil"
    };
    $uid = ($ARGV[1] eq "integer") ? $stat[4] : "\"" . getpwuid($stat[4]) . "\"";
    $gid = ($ARGV[1] eq "integer") ? $stat[5] : "\"" . getgrgid($stat[5]) . "\"";
    printf(
       "(%s %u %s %s (%u %u) (%u %u) (%u %u) %u.0 %u t (%u . %u) -1)\n",
       $type,
       $stat[3],
       $uid,
       $gid,
       $stat[8] >> 16 & 0xffff,
       $stat[8] & 0xffff,
       $stat[9] >> 16 & 0xffff,
       $stat[9] & 0xffff,
       $stat[10] >> 16 & 0xffff,
       $stat[10] & 0xffff,
       $stat[7],
       $stat[2],
       $stat[1] >> 16 & 0xffff,
       $stat[1] & 0xffff
    );
    EOF
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-25
      • 2015-02-28
      • 2017-08-18
      • 1970-01-01
      • 2010-09-18
      • 1970-01-01
      相关资源
      最近更新 更多