如何自动添加评论等文件路径?
#!/bin/bash
pwd=scripts/test_copy-content
files=$(find $pwd -type f)
for file in $files; do
if [[ "$file" == *.sh ]]; then
elseif [[ "$file" == *.go ]]; then
First-line_comment="# *$file"
elseif [[ "$file" == *.py ]]; then
First-line_comment="# *$file"
elseif [[ "$file" == *.md ]]; then
First-line_comment="<!-- *$file -->"
elseif [[ "$file" == *.css ]]; then
First-line_comment="/* *$file */"
elseif [[ "$file" == *.js ]]; then
First-line_comment="/* *$file */"
elseif [[ "$file" == *.html ]]; then
First-line_comment="<!-- *$file -->"
elseif [[ "$file" == *.ico ]]; then
# avoid doing something
elseif [[ "$file" == *.jpg ]]; then
# avoid doing something
elseif [[ "$file" == *.png ]]; then
# avoid doing something
elseif [[ "$file" == Makefile ]]; then
First-line_comment="# *$file"
elseif [[ "$file" == *.own ]]; then
First-line_comment="<!-- *$file -->"
elseif any other file type
First-line_comment="# *$file"
fi
firstline=$(head -n 1 "$file")
First-line_comment="# *$file"
# Check if the first line contains the relative path
if [[ "$firstline" != "$First-line_comment" ]]; then
# Add the relative path to the first line of the file
echo -e "$First-line_comment\n$(cat "$file")" > "$file"
fi
done
bash: syntax error near unexpected token `elif'
bash: syntax error near unexpected token `elif'
bash: !-: event not found
bash: syntax error near unexpected token `elif'
bash: syntax error near unexpected token `elif'
bash: !-: event not found
bash: syntax error near unexpected token `elif'
bash: continue: only meaningful in a `for', `while', or `until' loop
bash: syntax error near unexpected token `else'
bash: syntax error near unexpected token `fi'
bash: syntax error near unexpected token `done'
您的脚本中有很多问题。您应该始终将脚本粘贴到 Shellcheck 并修复所有问题,然后在此处发布。它大大减少了需要解决的问题数量。
您当前面临的问题是:
pwd
以外的其他内容,它本身就是 shell 命令的名称来返回 present-working-directory。虽然您可以使用它,但最好避免使用 shell 命令名称作为变量名称。file
是另一个 shell 命令名称,fname
适用于文件名的简短描述性变量名称。*.ext
测试中使用 [[ ... ]]
。通配将扩展到目录中与扩展名匹配的每个文件,从而导致测试失败。相反,使用内置的 "${var##*.}"
从左侧修剪路径名,只留下扩展名。sed -i
来完成。如果您的 sed
不支持 -i
(就地编辑)选项,则需要创建一个临时文件,在这种情况下,可以使用几乎任何其他方法,但如果您可以让 sed
担心的话关于插入,它简化了事情。#!/bin/bash
您想要将注释插入到第二行或后面的行以避免禁用解释器行。=
,而不是 ==
,尽管 bash 也会接受 ==
。if ...; then; elif ...; then; fi
case
陈述更有意义。通过允许您组合共享通用注释语法的扩展,可以轻松减少案例数量。应该执行您想要的操作的示例可以编写如下(注意未经测试)。另请注意,它使用
while read -r fname; do ... done < <(find ..)
形式,而不是循环 find
的换行符分隔结果。一种稍微更标准的方法,可以避免一些分词问题。如果您有不常见的带有嵌入换行符的文件名 - 无论哪种方式都必须修改。
内嵌注释的示例进一步解释了逻辑:
#!/bin/bash
# use another name than pwd -- that is a shell command
cwd="${1:-scripts/test_copy-content}"
# loop over each filename
while read -r fname; do
# check if file is a makefile
if [ "${fname##/}" = "Makefile" ]; then
ext="sh" # set extension to sh
else
ext="${fname##*.}" # extension from filename
fi
# if no extension, get next filename
[ "$ext" = "$fname" ] && continue
# insert comment based on extension
case "$ext" in
sh | go | py ) # shell, go, python or Makefile
if [ -s "$fname" ]; then
# non-empty, ins 2nd line # note insert as 2nd line
sed -i "2i # *$fname" "$fname"
else
# empty-file, create 1st line
printf "# *%s\n" "$fname" > "$fname"
fi
;;
md | html | own ) # markdown, html or own
if [ -s "$fname" ]; then
# non-empty, ins 1st line
sed -i "1i <!-- *$fname -->" "$fname"
else
# empty-file, create 1st line
printf "<!-- *%s -->\n" "$fname" > "$fname"
fi
;;
css | js ) # css or js
if [ -s "$fname" ]; then
# non-empty, ins 1st line
sed -i "1i /* *$fname */" "$fname"
else
# empty-file, create 1st line
printf "/* *%s */\n" "$fname" > "$fname"
fi
;;
ico | jpg | png ) # images
# do nothing
;;
* ) # all other files
if [ -s "$fname" ]; then
# non-empty, ins 1st line
sed -i "1i # *$fname" "$fname"
else
# empty-file, create 1st line
printf "# *%s\n" "$fname" > "$fname"
fi
;;
esac
done < <(find "$cwd" -type f)
注意:
sed
不会在空文件中插入值,因此 [ -s "$fname" ]
测试用于确保文件存在且非空。否则,只需使用重定向在空文件中创建新的第一行。
如果您有疑问或问题,请告诉我,我很乐意为您提供进一步帮助。