给定以下文件file
:
my name is: @NAME@
我运行这个脚本:
name="Alin"
full_file="$(mktemp)"
cat file |
sed -e "s:@NAME@:${name}:g" |
>> "${full_file}"
cat "${full_file}"
但是我得到空文件(${full_file}
),我不明白为什么…
我希望看到这个文件作为结果:
my name is: Alin
我错过了什么?
在您的代码中,您没有将任何内容重定向到新的临时文件,因为>>
出现在管道之后。
sed
命令后需要拆除管路:
name="Alin"
full_file="$(mktemp)"
cat file |
sed -e "s:@NAME@:${name}:g"
>> "${full_file}"
cat "${full_file}"
或者,简单地把>> "${full_file}"
放到sed
行:
name="Alin"
full_file="$(mktemp)"
cat file |
sed -e "s:@NAME@:${name}:g" >> "${full_file}"
cat "${full_file}"
或者,因为在这里使用cat
管道到sed
没有意义(请注意,您总是可以将文件名直接传递给sed
命令):
name="Alin"
full_file="$(mktemp)"
sed -e "s:@NAME@:${name}:g" file >> "${full_file}"
cat "${full_file}"