如何为windows中的.sh文件分配执行权限以在linux中执行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13185364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:41:03  来源:igfitidea点击:

How to assign execute permission to a .sh file in windows to be executed in linux

linuxwindowsshellfile-permissions

提问by C graphics

Here is my problem,

这是我的问题,

In Windows I am making a zip file in which there is a text .sh file which is supposed to be executed in Linux. The user on the other end opens the zip file in Linux and tries to execute the .sh file but the execute permission is gone. So the user has to do it manually ( like explained here:add execute permission.

在 Windows 中,我正在制作一个 zip 文件,其中有一个应该在 Linux 中执行的文本 .sh 文件。另一端的用户在 Linux 中打开 zip 文件并尝试执行 .sh 文件,但执行权限消失了。所以用户必须手动完成(就像这里解释的那样:添加执行权限

How can I in Windows make the .sh executable and add it to a zip file so that when the zip file opens in linux the .sh file still retains its execute permission ( so that user doesn't have to do it manually)

我如何在 Windows 中使 .sh 可执行文件并将其添加到 zip 文件中,以便在 linux 中打开 zip 文件时,.sh 文件仍保留其执行权限(以便用户不必手动执行)

回答by sampson-chen

As far as I know the permission system in Linux is set up in such a way to prevent exactly what you are trying to accomplish.

据我所知,Linux 中的权限系统是以这样一种方式设置的,以防止您尝试完成的操作。

I think the best you can do is to give your Linux user a custom unzip one-liner to run on the prompt:

我认为您能做的最好的事情就是为您的 Linux 用户提供一个自定义解压缩单行程序,以便在提示符下运行:

unzip zip_name.zip && chmod +x script_name.sh

If there are multiple scripts that you need to give execute permission to, write a grant_perms.shas follows:

如果有多个脚本需要给执行权限,写一个grant_perms.sh如下:

#!/bin/bash
# file: grant_perms.sh

chmod +x script_1.sh
chmod +x script_2.sh
...
chmod +x script_n.sh

(You can put the scripts all on one line for chmod, but I found separate lines easier to work with in vim and with shell script commands.)

(对于 chmod,您可以将脚本全部放在一行中,但我发现在 vim 和 shell 脚本命令中更容易使用单独的行。)

And now your unzip one-liner becomes:

现在你的解压单行变成:

unzip zip_name.zip && source grant_perms.sh

Note that since you are using sourceto run grant_perms.sh, it doesn't need execute permission

请注意,由于您使用的source是 run grant_perms.sh,因此不需要执行权限

回答by dmarra

This is not possible. Linux permissions and windows permissions do not translate. They are machine specific. It would be a security hole to allow permissions to be set on files before they even arrive on the target system.

这不可能。Linux 权限和 Windows 权限不转换。它们是特定于机器的。允许在文件到达目标系统之前对文件设置权限将是一个安全漏洞。

回答by itsbruce

This is possible using the Info-Zipopen-source Zip utilities. If unzipis run with the -Xparameter, it will attempt to preserve the original permissions. If the source filesystem was NTFS and the destination is a Unix one, it will attempt to translate from one to the other. I do not have a Windows system available right now to test the translation, so you will have to experiment with which group needs to be awarded execute permissions. It'll be something like "Users" or "Any user"

这可以使用Info-Zip开源 Zip 实用程序实现。如果unzip使用-X参数运行,它将尝试保留原始权限。如果源文件系统是 NTFS 而目标文件系统是 Unix,它将尝试从一个文件系统转换到另一个文件系统。我现在没有可用的 Windows 系统来测试翻译,因此您必须试验需要授予执行权限的组。它将类似于“用户”或“任何用户”

回答by jmster

The ZIP file format does allow to store the permission bits, but Windows programs normally ignore it. The ziputility on Cygwin however does preserve the x bit, just like it does on Linux. If you do not want to use Cygwin, you can take a source code and tweak it so that all *.sh files get the executable bit set. Or write a script like explained here

ZIP 文件格式确实允许存储权限位,但 Windows 程序通常会忽略它。zip然而 Cygwin 上的实用程序确实保留了 x 位,就像它在 Linux 上一样。如果您不想使用 Cygwin,您可以获取源代码并对其进行调整,以便所有 *.sh 文件都设置可执行位。或者写一个像这里解释的脚本