linux - 对临时文件的 sed 权限被拒绝

标签 linux bash shell

使用 sed 我尝试替换值 0.1.233... 在命令行上没有问题;但是,当将此命令放入 shell 脚本时,出现错误:

sed: couldn't open temporary file ../project/cas-dp-ap/sedwi3jVw: Permission denied 

我不明白这个临时 sedwi 文件是从哪里来的。

你知道为什么我有这个临时文件以及我如何传递它吗?

$(sed -i "s/$current_version/$version/" $PATHPROJET$CREATE_PACKAGE/Chart.yaml)

++ sed -i s/0.1.233/0.1.234/ ../project/cas-dp-ap/Chart.yaml
sed: couldn't open temporary file ../project/cas-dp-ap/sedwi3jVw:   Permission denied
+ printf 'The version has been updated to : 0.1.234 \n\n \n\n'
The version has been updated to : 0.1.234 
+ printf '***********************************'

最佳答案

sed -i 是“就地编辑”。然而,“就地”并不是真的。发生的事情更像是:

  • 创建一个临时文件
  • 在原始文件上运行 sed 并将更改放入临时文件
  • 删除原文件
  • 将临时文件重命名为原始文件

例如,如果我们查看已编辑文件的 inode,我们可以看到它在 sed 运行后发生了变化:

$ echo hello > a
$ ln a b
$ ls -lai a b
19005916 -rw-rw-r-- 2 jhnc jhnc 6 Jan 31 12:25 a
19005916 -rw-rw-r-- 2 jhnc jhnc 6 Jan 31 12:25 b
$ sed -i 's/hello/goodbye/' a
$ ls -lai a b
19005942 -rw-rw-r-- 1 jhnc jhnc 8 Jan 31 12:25 a
19005916 -rw-rw-r-- 1 jhnc jhnc 6 Jan 31 12:25 b
$ 

这意味着您的脚本必须能够在执行“就地”编辑的文件夹中创建文件。

关于linux - 对临时文件的 sed 权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54460445/

相关文章:

python 2.7。使用 linux 命令将文本文件的名称保存为日期

linux - 使用正则表达式进行 Grep 并使用组进行捕获

Linux:将DD结果输出到文本文件

python - 如何使用正则表达式来匹配文件中其他小写单词不连续重复的大写单词

python - 如何将 shell 命令输出重定向到 Python 脚本输入?

python - 如何在 Python 中运行上下文感知命令?

c++ - ld 返回的退出状态 1 是什么?

linux - Bash 检查文件夹中的所有文件是否可由 www-data 写入

bash - 如何在 Bash 中回显(打印)变量的值?

bash - 在 python 解释器提示中添加颜色,没有正确包装