在bash中,我需要检查字符串是否以“#”符号开头。我怎么做?
这是我的看法-
if [[ $line =~ '#*' ]]; then
echo "$line starts with #" ;
fi
我想在文件上运行此脚本,文件看起来像这样-
03930
#90329
43929
#39839
这是我的剧本-
while read line ; do
if [[ $line =~ '#*' ]]; then
echo "$line starts with #" ;
fi
done < data.in
这是我的预期输出-
#90329 starts with #
#39839 starts with #
但是我无法使它起作用,有什么想法吗?
不需要正则表达式,模式就足够了
if [[ $line = \#* ]] ; then
echo "$line starts with #"
fi
或者,您可以使用参数扩展:
if [[ ${line:0:1} = \# ]] ; then
echo "$line starts with #"
fi
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句