【问题标题】:Why does a bash script require an execute bit if a windows batch script can just be executed?如果只能执行 Windows 批处理脚本,为什么 bash 脚本需要执行位?
【发布时间】:2016-05-31 20:35:49
【问题描述】:

昨天我遇到了git execute bit bash script 怪癖——需要:

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

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

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

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

【问题讨论】:

  • 真的没有那么多。这只是意味着您不能意外运行实际上不是脚本的文件。这是 Windows 没有选择实现的功能,可能是因为它使用文件扩展名。 (事实上​​,你可以反过来说“为什么 Windows 要求我将脚本重命名为以 .bat 结尾,而 UNIX 文件只能执行?”)
  • 这个问题的推论是 “为什么在允许我运行文件之前,Windows 要求我用.EXE.BAT 命名我的文件?为什么我不能命名我的文件?我想要的文件?”

标签: linux windows git bash batch-file


【解决方案1】:

嗯,Linux 不是 Windows。 Linux/Unix 文件系统支持可执行位来区分可执行文件和纯数据文件,并控制用户|组|其他的执行权限。如果您在脚本前面加上要启动它的 shell/二进制文件的名称,您仍然可以运行该脚本,但如果您想执行 ./scriptname.sh 或从路径执行它,则需要将其标记为可执行文件为onwer|a 组成员|其他用户,对于脚本,通常在第一行定义解释器以启动脚本的 shebang:#!/bin/bash

【讨论】:

    【解决方案2】:

    要运行脚本,在类 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 仅使用文件扩展名来检测可执行文件。

    【讨论】:

      猜你喜欢
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 2012-09-08
      • 2017-02-06
      • 2011-01-03
      • 2012-11-27
      • 2015-04-12
      • 1970-01-01
      相关资源
      最近更新 更多