简体   繁体   English

将 bash 脚本内容附加到另一个文件

[英]Append bash script content to another file

This is my bash script这是我的 bash 脚本

#!/bin/bash

filenames="root/simulate/*.txt"
for f in "$filenames"; do
#append "#" to all .txt files
 sed -i -e 's/^/#/' $filenames
#append content of this bash script to all .txt files
 cat "$0" >> "$f"
done

But instead of appending its bash script content to all existing ".txt" files, it creates a new "*.txt" file in the directory and adds the content of the other ".txt" files into it, and then appends the bash script to that new file.但是它不是将其 bash 脚本内容附加到所有现有的“.txt”文件,而是在目录中创建一个新的“*.txt”文件并将其他“.txt”文件的内容添加到其中,然后附加 bash脚本到那个新文件。 Can anybody help?有人可以帮忙吗? Thank you谢谢

That is because * not expanding inside "" , try it like this:那是因为*没有在""内扩展,请像这样尝试:

#!/bin/bash
for f in root/simulate/*.txt; do
    #append "#" to all .txt files
    sed -i -e 's/^/#/' "$f"
    #append content of this bash script to all .txt files
    cat "$0" >> "$f"
done

And you don't need a loop here, just this sed command:而且这里不需要循环,只需要这个 sed 命令:

#!/bin/bash
sed -i 's/^/#/;$r'"$0" root/simulate/*.txt

如果你想使用变量试试

for f in $filenames; do

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM