Linux shell脚本批量注释(Linux批量注释)

在Shell脚本中,你可以使用sed命令批量注释或取消注释文件中的行。

以下示例将演示如何为一个目录中的所有 .sh 文件添加或删除行首的井号(#)注释符。

图片[1]-Linux shell脚本批量注释(Linux批量注释)-不念博客

编写批量添加注释的Shell脚本(例如:add_comments.sh):

#!/bin/bash

# 指定目标目录
target_directory="/path/to/your/directory"

# 遍历目标目录下的.sh文件
for file in "$target_directory"/*.sh; do
  # 使用sed在每行行首添加井号(#)
  sed -i 's/^/#/' "$file"

  # 输出已修改的文件名
  echo "已为 $file 添加注释"
done

编写批量取消注释的Shell脚本(例如:remove_comments.sh):

#!/bin/bash

# 指定目标目录
target_directory="/path/to/your/directory"

# 遍历目标目录下的.sh文件
for file in "$target_directory"/*.sh; do
  # 使用sed删除每行行首的井号(#)
  sed -i 's/^#//' "$file"

  # 输出已修改的文件名
  echo "已为 $file 移除注释"
done

为脚本添加执行权限:

chmod +x add_comments.sh
chmod +x remove_comments.sh

根据需要运行脚本:

  • 添加注释:./add_comments.sh
  • 移除注释:./remove_comments.sh

这将分别在指定目录(target_directory变量)中的所有 .sh 文件的行首添加或删除井号(#)注释符。

© 版权声明
THE END
喜欢就支持一下吧
点赞131赞赏 分享
评论 抢沙发
头像
欢迎光临不念博客,留下您的想法和建议,祝您有愉快的一天~
提交
头像

昵称

取消
昵称

    暂无评论内容