bash 循环跳过注释行

标签 bash shell

我正在遍历文件中的行。我只需要跳过以“#”开头的行。 我该怎么做?

 #!/bin/sh 

 while read line; do
    if ["$line doesn't start with #"];then
     echo "line";
    fi
 done < /tmp/myfile

感谢您的帮助!

最佳答案

while read line; do
  case "$line" in \#*) continue ;; esac
  ...
done < /tmp/my/input

然而,坦率地说,转向 grep 通常会更清楚:

grep -v '^#' < /tmp/myfile | { while read line; ...; done; }

关于bash 循环跳过注释行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12488556/

相关文章:

linux - 从 bash 脚本中的文本文件添加新用户

javascript - Web 服务器上的 NodeJS 应用程序导致 fork 炸弹 - bash : fork: retry: No child processes

linux - 计算bash中每个进程的CPU使用率%

macos - 在 shell 脚本中执行 Vim 命令

linux - 如何在shell中同时定义和打印变量值

bash - 阅读docker事件时,如何退出docker事件而不是整个程序?

linux - ZOC 终端 :text has been cut halfway down the page

shell - python 的 shlex.split 替代 Go

shell - 是否可以在命令提示符中创建带有多个选项卡的 CSV 文件?

Linux 和 SVN : Need a shelll script to transfer updated files to test server after making an svn update