如何在所有 .sh 文件的开头添加 she bang?

发布于 2024-09-13 07:22:23 字数 92 浏览 3 评论 0原文

我想编辑所有带有 .sh 扩展名的文件中文件顶部的 bang line #!/bin/bash 行,如何通过脚本完成。

I want to edit she bang line #!/bin/bash line on the top of files in all the files with .sh extension how it done by scripting.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

献世佛 2024-09-20 07:22:23

您可以使用 sed 命令来执行此操作:

sed -i "1i #!/bin/bash" *.sh
  • sed:命令名称
  • -i:编辑文件的 sed 选项
    inplace
  • 1 :行号
  • i :在之前插入
    #!/bin/bash之前提供的行号
  • :要成为的 shebang
    添加了
  • *.sh:使此功能适用于所有 .sh
    文件

You can do this using the sed command:

sed -i "1i #!/bin/bash" *.sh
  • sed: name of the command
  • -i : sed option to edit the file
    inplace
  • 1 : line number
  • i : to do the insertion before the
    line number provided before
  • #!/bin/bash: the shebang to be
    added
  • *.sh: to make this work on all .sh
    files
小嗲 2024-09-20 07:22:23
find /path -iname "*.sh" -type f -exec sed -i.bak '1i #!/bin/bash' "{}" +;
find /path -iname "*.sh" -type f -exec sed -i.bak '1i #!/bin/bash' "{}" +;
梦回梦里 2024-09-20 07:22:23

下面是如何实现这一点的示例:

#!/bin/bash
echo "New file name?"
read -r name
touch $name
echo "'$name' created"
cp shebang.txt "$name"
chmod +x "$name"

我使用一个仅包含 shebang.txt 的简单文本文件。

Here is an example of how this can be achieved:

#!/bin/bash
echo "New file name?"
read -r name
touch $name
echo "'$name' created"
cp shebang.txt "$name"
chmod +x "$name"

I use a simple text file containing only the shebang.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文