【发布时间】:2020-11-08 05:01:13
【问题描述】:
这可能很简单,但我需要这样做:
我有一个文件名a.sh,它包含以下几行
# firstname banuka
firstcolor green
lastname jananath
# age 25
我想将firstname banuka 写入这个文件,所以它看起来像
echo "firstname banuka" > a.sh
但是,在写 firstname banuka 之前,我想检查文件是否已经具有该值(或行)
正如您在a.sh 的文件内容中看到的那样,我们要编写的部分(firstname banuka)可能已经存在但带有注释。
So if it has a comment,
1. I want to un-comment it (remove `#` in front of `firstname banuka`)
If no comment and no line which says `firstname banuka`,
2. Add the line `firstname banuka`
If no comment and line is already there,
3. skip (don't write `firstname banuka` part to file)
有人可以帮帮我吗?
【问题讨论】:
-
我可以像
echo "firstname banuka" > a.sh这样写入文件,但这就是我能实现的全部 -
>将清除您可能指的文件>>