【问题标题】:"sed delete" command in bash not always workbash 中的“sed delete”命令并不总是有效
【发布时间】:2020-05-10 14:27:55
【问题描述】:

附上代码:

#!/bin/bash
echo 'if [ -f /etc/skel/.bashrc ]; then . /etc/skel/.bashrc; fi' >> ~/.bashrc
echo 'enable -n echo' >> ~/.bashrc
echo "alias cls=\"echo -en '\033c\033[3J'\"" >> ~/.bashrc
echo "alias cmdlist='compgen -c | sort -b'" >> ~/.bashrc
sed -i '/if [ -f /etc/skel/.bashrc ]; then . /etc/skel/.bashrc; fi/d' ~/.bashrc
sed -i '/enable -n echo/d' ~/.bashrc
sed -i "/alias cls=\"echo -en '\033c\033[3J'\"/d" ~/.bashrc
sed -i "/alias cmdlist='compgen -c | sort -b'/d" ~/.bashrc

我期望一个空文件 .bashrc,而不是它包含:

if [ -f /etc/skel/.bashrc ]; then . /etc/skel/.bashrc; fi
alias cls="echo -en '\033c\033[3J'"

为什么?

【问题讨论】:

  • 方括号([])在正则表达式中有意义。
  • 转义\[不起作用。
  • 哈哈哈!你确定吗?或者你遇到了不同的问题?你的正则表达式有更多的问题,比如正则表达式中的斜线。他们告诉表达式在哪里结束,你告诉sed 它在if -f 之后结束。更多的东西要逃脱!
  • 谢谢,34分钟后,我解决了!

标签: bash shell terminal linux-mint


【解决方案1】:

感谢 Poshi,我已经解决了!

#!/bin/bash
echo 'if [ -f /etc/skel/.bashrc ]; then . /etc/skel/.bashrc; fi' >> ~/.bashrc
echo 'enable -n echo' >> ~/.bashrc
echo "alias cls=\"echo -en '\033c\033[3J'\"" >> ~/.bashrc
echo "alias cmdlist='compgen -c | sort -b'" >> ~/.bashrc
sed -i '/if \[ -f \/etc\/skel\/.bashrc \]; then . \/etc\/skel\/.bashrc; fi/d' ~/.bashrc
sed -i '/enable -n echo/d' ~/.bashrc
sed -i "/alias cls=\"echo -en '\\\033c\\\033\[3J'\"/d" ~/.bashrc
sed -i "/alias cmdlist='compgen -c | sort -b'/d" ~/.bashrc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-09
    • 2011-04-28
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    相关资源
    最近更新 更多