【问题标题】:Why testing for first character skips empty lines?为什么测试第一个字符会跳过空行?
【发布时间】:2018-12-27 12:25:17
【问题描述】:

为什么下面的文件阅读器和注释行检查会跳过空行?

while read line; do
    if [ ! ${line:0:1} == "#" ]; then # leave out comments
            function_call $line
    fi
done < list_items.txt

【问题讨论】:

  • Quotes 更有趣的是,为什么[ ! == '#' ] 返回false。

标签: bash


【解决方案1】:

跳过空行的行为是由非引用 bash 变量引起的,比较字符串时总是引用:

while read line; do
    if [ ! "${line:0:1}" == "#" ]; then # leave out comments
            function_call "${line}"
    fi
done < list_items.txt

上面将处理空行。

【讨论】:

  • 似乎函数调用参数上的引号 - ${line} - 使其成为单个参数,在我的情况下这会破坏函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
  • 1970-01-01
  • 2013-06-11
相关资源
最近更新 更多