【问题标题】:sed error of "no such file or directory" in setting up bashrc设置 bashrc 时出现“没有这样的文件或目录”的 sed 错误
【发布时间】:2018-05-27 03:11:16
【问题描述】:

很抱歉提出一个对一些有经验的人来说可能看起来很幼稚的问题。我不擅长 Linux,尝试修改 ~/.bashrc 如下:

sed -i s/'history -cw'//g .bash_logout

当我获取它时,我收到一条错误消息:

sed: 无法读取 .bash_logout: 没有这样的文件或目录

这是什么意思,我该如何解决?

非常感谢。

【问题讨论】:

  • 当前目录下没有.bash_logout这个文件,使用pwd命令。
  • 错误字面意思是“无法读取.bash_logout:没有这样的文件或目录”。错误消息通常非常好(并且在过去十年中变得更好)
  • 在命令行中为任何命令指定脚本时,将脚本用引号括起来,不要在命令中途开始和结束引号!你的命令在语法上应该写成's/history -cw//g',而不是s/'history -cw'//g

标签: bash sed


【解决方案1】:

问题是您没有使用该路径。如果您只是想删除该行,请使用:

sed -i s/'history -cw'//g .bash_logout  #if I am in the current dir

sed -i s/'history -cw'//g ${HOME}/.bash_logout

sed -i s/'history -cw'//g ~/.bash_logout

sed -i s/'history -cw'//g /path/to/.bash_logout

要添加到文件中,请考虑以下事项:

#!/bin/bash
if ! grep -q "history -cw" .bash_logout ;then
        echo "lets add this baby"
        echo "history -cw" >> .bash_logout
else
        echo "it is already there, don't add .."
fi

注意:您当前的问题是您没有指定文件所在的路径。尽可能明确:

此脚本将值设置为完整路径,并在尝试执行任何操作之前先检查该文件是否存在:

LOGOUT="/home/user/.bash_logout"
if [ ! -f "$LOGOUT" ] ;then
        echo "file not found"
        exit
fi

if ! grep -q "history -cw" "$LOGOUT" ;then
        echo "lets add this baby"
        echo "history -cw" >> "$LOGOUT"
else
        echo "it is already there, don't add .."
fi

【讨论】:

  • @EdMorton 我想你的意思是发给其他人而不是我的。
  • 明白了,很好,我明白你现在在说什么了......我做了一个小更新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-03
  • 2010-12-18
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 2012-08-28
  • 1970-01-01
相关资源
最近更新 更多