11
我正在循环遍历文件中的行。我只需要跳过以“#”开头的行。 我该怎么做?bash循环跳过注释行
#!/bin/sh
while read line; do
if ["$line doesn't start with #"];then
echo "line";
fi
done < /tmp/myfile
感谢您的帮助!
我正在循环遍历文件中的行。我只需要跳过以“#”开头的行。 我该怎么做?bash循环跳过注释行
#!/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; }
你也可以做一些与'expr'或后缀去除(例如,'[-z“$ {太聪明行%%#*}“]'),但我认为这些可能会比'case'选项同样或更少。 – pilcrow
也可以使用'grep -v'^ \ s *#' tmp/myfile'来代替空格(仅)在'#'前面的行,这与'case'解决方案一致,因为'read '去掉前导和尾随的空白。 – mklement0
如果[[$ line =〜^#]];则另一个选项(如果某人想要避免使用'grep')可能是'然后继续; fi'。 –