如何在Linux中为sed命令设置临时文件目录? [英] How to set temp file directory for sed command in linux?
问题描述
我有bash脚本,它会像这样修改文件
I have bash script which modified file like this
sed -i "/hello world/d" /etc/postfix/virtual
,然后从Web应用程序运行了该脚本.sed命令在该目录中创建临时文件,但是Web应用程序所在的用户无权在该目录中创建文件.我不想为该文件夹的用户提供更多权限.可以为sed命令指定临时文件位置吗?
and I ran this script from web application. sed command create temporary file in that directory but user under which web application works doesn't have permissions to create files in that directory. I do not want to give more permissions for the user to that folder. Is it possible to specify temp file location for sed command?
我是Linux的新手,如果我的问题太简单了,但是我没有找到任何解决方案,那么对不起.
I am new in linux so sorry if my question is too easy but I didn't find any solution.
谢谢!
推荐答案
该文件必须通过临时文件进行修改,这实际上是sed的工作,但缺少权限.
The file must be modified via temporary file, this is actually what sed was doing but it lacked permissions.
在处理这种情况时,我们只是手动执行此过程.
When dealing with such a situation, we just do this process manually.
## TEMP File
TFILE=`mktemp --tmpdir tfile.XXXXX`
trap "rm -f $TFILE" 0 1 2 3 15
##
sed 's_XXX_YYY_' /etc/example > "$TFILE"
cat "$TFILE" > /etc/example
## trap Deletes TFILE on Exit
这篇关于如何在Linux中为sed命令设置临时文件目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!