Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如果可以执行windows批处理脚本,为什么bash脚本需要执行位?_Linux_Windows_Git_Bash_Batch File - Fatal编程技术网

Linux 如果可以执行windows批处理脚本,为什么bash脚本需要执行位?

Linux 如果可以执行windows批处理脚本,为什么bash脚本需要执行位?,linux,windows,git,bash,batch-file,Linux,Windows,Git,Bash,Batch File,昨天我遇到了一个怪癖——需要: git update-index --add --chmod=+x scriptname.sh 我觉得奇怪的是,甚至有可能陷入这种情况。(即创建了一个您没有运行权限的脚本文件) 如果我已经创建了一个shell脚本,那么我当然可以在shell执行权限下运行它。为什么它需要自己的执行权限位 我的问题是:如果可以执行windows批处理脚本,为什么bash脚本需要执行位?嗯,Linux不是windows。Linux/Unix文件系统支持可执行位,以区分可执行文件和纯数

昨天我遇到了一个怪癖——需要:

git update-index --add --chmod=+x scriptname.sh
我觉得奇怪的是,甚至有可能陷入这种情况。(即创建了一个您没有运行权限的脚本文件)

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


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

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

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

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

# run a bash script
bash test.sh

# run a python scripts
python test.py
第二个选项是用execute位将文件标记为可执行文件,然后在这样的调用之后

# 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仅使用文件扩展名来检测可执行文件。

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

# run a bash script
bash test.sh

# run a python scripts
python test.py
第二个选项是用execute位将文件标记为可执行文件,然后在这样的调用之后

# 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仅使用文件扩展名来检测可执行文件。

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