【问题标题】:Why is bash inserting the output of "ls /" in output?为什么bash在输出中插入“ls /”的输出?
【发布时间】:2016-07-26 02:05:24
【问题描述】:

我在 bash 中遇到了一个相当神秘的错误,我怀疑这与 shell 扩展规则有关。

故事是这样的:在工作中,我的任务是记录一个用于协调公司资源的大型内部网站。不幸的是,代码非常难看,因为它已经超出了最初的目的,并“演变”成了协调公司工作的主要资源。

大部分代码是 PHP。我写了一些帮助脚本来帮助我编写文档;例如,一个脚本提取 php 函数中使用的所有全局 php 变量。

所有这些脚本的中心是“extract_function.sh”脚本。基本上,给定一个 php 函数名称和一个 php 源文件,它会提取并输出该 php 函数。

现在问题来了:不知何故,当脚本提取函数时,它基本上是在输出中随机插入ls / 的输出。

例如:

$ ./extract_function my_function my_php_file.php
function my_function {
    // php code
/etc
/bin
/proc
...
   // more php code
}

更令人困惑的是,我只让一个特定文件中的一个特定功能发生这种情况!现在,由于函数非常庞大(500 多行,当我说代码丑陋时我的意思是!),我一生都无法弄清楚是什么原因造成的,或者想出一个更简单的临时函数来产生这种行为。此外,公司政策禁止我分享实际代码。

但是,这是我的代码:

#!/usr/bin/env bash
program_name=$(basename $0);
function_name=$1;
file_name=$2;

if [[ -z "$function_name" ]]; then
    (>&2 echo "Usage: $program_name function_name [file]")
    exit 1
fi

if [[ -z "$file_name" ]] || [ "$file_name" = "-" ]; then
    file_name="/dev/stdin";
fi

php_lexer_file=$(mktemp)
trap "rm -f $php_lexer_file" EXIT
read -r -d '' php_lexer_text << 'EOF'
<?php
    $file = file_get_contents("php://stdin");
    $tokens = token_get_all($file);
    foreach ($tokens as $token) 
        if ($token === '{')
            echo PHP_EOL, "PHP_BRACKET_OPEN", PHP_EOL;
        else if ($token == '}')
            echo PHP_EOL, "PHP_BRACKET_CLOSE", PHP_EOL;
        else if (is_array($token))
            echo $token[1];
        else
            echo $token;
?>
EOF
echo "$php_lexer_text" > $php_lexer_file;

# Get all output from beginning of function declaration
extracted_function_start=$(sed -n -e "/function $function_name(/,$ p"  < $file_name);

# Prepend <?php so that php will parse the file as php
extracted_function_file=$(mktemp)
trap "rm -f $extracted_function_file" EXIT
echo '<?php' > $extracted_function_file;
echo "$extracted_function_start" >> $extracted_function_file;
tokens=$(php $php_lexer_file < $extracted_function_file);
# I've checked, and at this point $tokens does not contain "/bin", "/lib", etc...

IFS=$'\n';
open_count=0;
close_count=0;
for token in $tokens; do # But here the output of "ls /" magically appears in $tokens!
    if [ $token = "PHP_BRACKET_OPEN" ]; then
        open_count=$((open_count+1))
        token='{';
    elif [ $token == "PHP_BRACKET_CLOSE" ] ; then
        close_count=$((close_count+1))
        token='}';
    fi

    echo $token;
    if [ $open_count -ne 0 ] && [ $open_count -eq $close_count ]; then
        break;
    fi
done

是的,我知道我不应该使用 bash 来操作 php 代码,但我基本上有两个问题:

1) 为什么 bash 会这样做?

2) 还有,我该如何解决?

【问题讨论】:

  • 你想用if [[ -z "$file_name" ]] || [ "$file_name" = "-" ]; then; file_name="/dev/stdin";fi完成什么?
  • @sjsadm 如果未指定文件名或等于“-”,则从标准输入读取。有点像“猫”,也使管道更容易。

标签: php bash shell quoting


【解决方案1】:

$tokens 中的一个标记是 *(或可以匹配多个文件的 glob 模式)。如果您无法安排令牌列表不包含 shell 元字符,则需要跳过一些环节以避免扩展。一种可能的技术是使用read -ra 将标记读入一个数组,这样更容易引用它们。

【讨论】:

  • 在这样的时刻,我真的很感谢 Stack Overflow 和它的社区。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 1970-01-01
  • 2019-06-13
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 2019-08-29
相关资源
最近更新 更多