linux - 如果只能执行 Windows 批处理脚本,为什么 bash 脚本需要执行位?

标签 linux windows git bash batch-file

昨天我遇到了 git execute bit bash script怪癖 - 要求:

git update-index --add --chmod=+x scriptname.sh

我觉得很奇怪,甚至有可能陷入这种情况。 (即创建了一个您无权运行的脚本文件)。

如果我已经创建了一个 shell 脚本 - 当然我可以在 shell 执行权限的权限下运行它。为什么它需要自己的执行权限位?

我的问题是:如果只能执行 Windows 批处理脚本,为什么 bash 脚本需要执行位?

最佳答案

要运行脚本,您在类 unix 系统中有两种选择。第一个选项是使用脚本作为参数直接调用解释器。

# run a bash script
bash test.sh

# run a python scripts
python test.py

第二个选项是将您的文件标记为可执行文件,使用执行位并在这样的调用之后...

# sample bash
./test.sh

# sample python
./test.py

...您的系统会尝试为您找到合适的解释器。为此,使用了脚本的第一行“shebang”。

bash 示例:

#!/bin/bash
# points to the installed bash interpreter - bash example

Python 示例:

#!/usr/bin/python
# points to the installed python interpreter

对于您的问题,Windows 仅使用文件扩展名来检测可执行文件。

关于linux - 如果只能执行 Windows 批处理脚本,为什么 bash 脚本需要执行位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35502459/

相关文章:

OSX 上的 Python os.getenv (Django 1.4)

java - 我如何保证我的文件尾随 Linux 缓存中的最新缓冲区

git - 如何管理多个用户在 git 上的代码 merge ?

linux - 在 Ubuntu 中使用 QSerialport 时出现问题

c++ - 制作图形用户界面编辑器

eclipse - 使用适用于 Linux 的 Windows 子系统在 eclipse 上设置 Linux gcc 工具链/编译器

c++ - 使用颜色矩阵交换两种颜色

c++ - Windows 8 下的 GetVersionEx

git - 提交和推送时git名称是如何确定的

git - 如何撤消尚未推送的提交?