Shell 通过对文件进行分类,在标记中生成代码块
您好,我正在尝试Shell 通过对文件进行分类,在标记中生成代码块,shell,scripting,markdown,Shell,Scripting,Markdown,您好,我正在尝试cat目录中文件的内容,并将它们作为代码块附加到标记文件中 我可以cat将文件内容添加到标记文档中,但无法将反标记添加到标记文件中。反勾号不会出现在文件中。如果我在shell中键入printf“``”,而不是在下面的bash脚本中,我可以得到要打印的反勾号: files=$(find.-name“*.h”-name“*.c”-maxdepth 1) 在$file中为i; 做 printf“\``>>report.md cat$i>>report.md printf“\``>>re
cat
目录中文件的内容,并将它们作为代码块附加到标记文件中
我可以cat
将文件内容添加到标记文档中,但无法将反标记添加到标记文件中。反勾号不会出现在文件中。如果我在shell中键入printf“``”
,而不是在下面的bash脚本中,我可以得到要打印的反勾号:
files=$(find.-name“*.h”-name“*.c”-maxdepth 1)
在$file中为i;
做
printf“\``>>report.md
cat$i>>report.md
printf“\``>>report.md
完成
那个find
语句实际上什么也找不到。没有名为*.h
和*.c
的文件。对于或,您需要-o
:
find . -maxdepth 1 '(' -name "*.h" -o -name "*.c" ')'
反勾号应该在输出文件中,没有理由不在。也就是说,你的意思是要有三个吗?通常,标记使用3表示代码块。它们可能也应该在单独的线路上
for i in $files; do
echo '```' >> report.md
cat $i >> report.md
echo '```' >> report.md
done
您可以一次重定向整个循环的输出
for file in $files; do
echo '```'
cat "$file"
echo '```'
done > report.md
为了增强健壮性,我建议使用find-exec
对每个文件运行操作。这样,带有空格和其他奇怪字符的文件名就不会使脚本出错
find . -maxdepth 1 '(' -name "*.h" -o -name "*.c" ')' \
-exec echo '```' ';' -exec cat {} ';' -exec echo '```' ';' > report.md
或者您可以跳过查找
,只需在所需文件上循环。这在空白处也是安全的:
for file in *.h *.c; do
echo '```'
cat "$file"
echo '```'
done > report.md
什么不起作用?backticks发生了什么事?抱歉,它打印了它没有将backticks打印到report.md
文件中