在 Linux 中创建可执行文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/817060/
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-03 17:17:00  来源:igfitidea点击:

Creating executable files in Linux

linuxexecutablefile-permissionschmodgedit

提问by

One thing I plan to be doing is writing (painfully simple) Perl scripts, and I'd like to be able to run them without explicitly calling Perl from the terminal. I appreciate that, to do this, I need to grant them execute permissions. Doing this with chmod is easy enough, but it also seems like a slightly laborious extra step. What I would like is one of two things:

我计划做的一件事是编写(非常简单的)Perl 脚本,我希望能够在不从终端显式调用 Perl 的情况下运行它们。我很感激,为此,我需要授予他们执行权限。用 chmod 做这件事很容易,但它似乎也是一个稍微费力的额外步骤。我想要的是两件事之一:

Firstly, is there a way to set the execute flag when saving a file? Currently I'm experimenting with gedit and geany, but would be willing to switch to a similarly- (or better-) featured editor if it had this capability.

首先,有没有办法在保存文件时设置执行标志?目前我正在试验 gedit 和 geany,但如果它有这种功能,我愿意切换到类似(或更好)的功能编辑器。

Failing that, is there a way to declare that all files created in a particular directory should have execute permissions?

否则,有没有办法声明在特定目录中创建的所有文件都应该具有执行权限?

My umask is set to 022, which should be OK, as far as I understand, but it would appear that the files are created as text files (with 666 default permissions) rather than executable files (with 777 default permissions).

我的 umask 设置为 022,据我所知,这应该没问题,但看起来这些文件是作为文本文件(具有 666 个默认权限)而不是可执行文件(具有 777 个默认权限)创建的。

Perhaps I'm just being lazy, but I figure there must be a more convenient way than chmodding every single script one creates.

也许我只是懒惰,但我认为必须有一种比 chmodding 一个人创建的每个脚本更方便的方法。

回答by BobbyShaftoe

It's really not that big of a deal. You could just make a script with the single command:

这真的没什么大不了的。您可以使用单个命令制作脚本:

chmod a+x *.pl

And run the script after creating a perl file. Alternatively, you could open a file with a command like this:

并在创建 perl 文件后运行脚本。或者,您可以使用如下命令打开文件:

touch filename.pl && chmod a+x filename.pl && vi filename.pl # choose your favorite editor

回答by BobbyShaftoe

Make file executable:

使文件可执行:

chmod +x file

chmod +x 文件

Find location of perl:

查找 perl 的位置:

which perl

哪个perl

This should return something like

这应该返回类似

/bin/perl sometimes /usr/local/bin

/bin/perl 有时 /usr/local/bin

Then in the first line of your script add:

然后在脚本的第一行添加:

#!"path"/perl with path from above e.g.

#!/bin/perl

#!"path"/perl 上面的路径,例如

#!/bin/perl

Then you can execute the file

然后就可以执行文件了

./file

。/文件

There may be some issues with the PATH, so you may want to change that as well ...

PATH 可能存在一些问题,因此您可能也想更改它...

回答by JimB

What you describe is the correct way to handle this.

您所描述的是处理此问题的正确方法。

You said that you want to stay in the GUI. You can usually set the execute bit through the file properties menu. You could also learn how to create a custom action for the context menu to do this for you if you're so inclined. This depends on your desktop environment of course.

你说你想留在 GUI 中。您通常可以通过文件属性菜单设置执行位。如果您愿意,您还可以学习如何为上下文菜单创建自定义操作来为您执行此操作。这当然取决于您的桌面环境。

If you use a more advanced editor, you can script the action to happen when the file is saved. For example (I'm only really familiar with vim), you could add this to your .vimrc to make any new file that starts with "#!/*/bin/*" executable.

如果您使用更高级的编辑器,您可以编写保存文件时发生的操作的脚本。例如(我只是真正熟悉 vim),您可以将它添加到您的 .vimrc 以创建任何以“ #!/*/bin/*”开头的新文件可执行。

au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent !chmod +x <afile> | endif | endif

回答by grossvogel

I think the problem you're running into is that, even though you can set your own umask values in the system, this does not allow you to explicitly control the default permissions set on a new file by gedit (or whatever editor you use).

我认为您遇到的问题是,即使您可以在系统中设置自己的 umask 值,这也不允许您通过 gedit(或您使用的任何编辑器)明确控制在新文件上设置的默认权限.

I believe this detail is hard-coded into gedit and most other editors. Your options for changing it are (a) hacking up your own mod of gedit or (b) finding a text editor that allows you to set a preference for default permissions on new files. (Sorry, I know of none.)

我相信这个细节被硬编码到 gedit 和大多数其他编辑器中。您更改它的选项是 (a) 修改您自己的 gedit 模块或 (b) 找到一个文本编辑器,允许您为新文件的默认权限设置首选项。(对不起,我不知道。)

In light of this, it's really not so bad to have to chmod your files, right?

有鉴于此,必须对文件进行 chmod 确实还不错,对吗?

回答by Dylan

No need to hack your editor, or switch editors.

无需破解您的编辑器或切换编辑器。

Instead we can come up with a script to watch your development directories and chmod files as they're created. This is what I've done in the attached bash script. You probably want to read through the comments and edit the 'config' section as fits your needs, then I would suggest putting it in your $HOME/bin/ directory and adding its execution to your $HOME/.login or similar file. Or you can just run it from the terminal.

相反,我们可以想出一个脚本来观察您的开发目录和 chmod 文件,因为它们被创建。这就是我在附加的 bash 脚本中所做的。您可能想通读评论并根据需要编辑“配置”部分,然后我建议将其放在您的 $HOME/bin/ 目录中,并将其执行添加到您的 $HOME/.login 或类似文件中。或者您可以直接从终端运行它。

This script does require inotifywait, which comes in the inotify-tools package on Ubuntu,

这个脚本确实需要 inotifywait,它在 Ubuntu 上的 inotify-tools 包中,

sudo apt-get install inotify-tools

Suggestions/edits/improvements are welcome.

欢迎提出建议/编辑/改进。

#!/usr/bin/env bash

# --- usage --- #
# Depends: 'inotifywait' available in inotify-tools on Ubuntu
# 
# Edit the 'config' section below to reflect your working directory, WORK_DIR,
# and your watched directories, WATCH_DIR. Each directory in WATCH_DIR will
# be logged by inotify and this script will 'chmod +x' any new files created
# therein. If SUBDIRS is 'TRUE' this script will watch WATCH_DIRS recursively.
# I recommend adding this script to your $HOME/.login or similar to have it
# run whenever you log into a shell, eg 'echo "watchdirs.sh &" >> ~/.login'.
# This script will only allow one instance of itself to run at a time.

# --- config --- #

WORK_DIR="$HOME/path/to/devel" # top working directory (for cleanliness?)
WATCH_DIRS=" \
    $WORK_DIR/dirA \
    $WORK_DIR/dirC \
    "                          # list of directories to watch
SUBDIRS="TRUE"                 # watch subdirectories too
NOTIFY_ARGS="-e create -q"     # watch for create events, non-verbose


# --- script starts here --- #
# probably don't need to edit beyond this point

# kill all previous instances of myself
SCRIPT="bash.*`basename ##代码##`"
MATCHES=`ps ax | egrep $SCRIPT | grep -v grep | awk '{print }' | grep -v $$`
kill $MATCHES >& /dev/null

# set recursive notifications (for subdirectories)
if [ "$SUBDIRS" = "TRUE" ] ; then
    RECURSE="-r"
else
    RECURSE=""
fi

while true ; do
    # grab an event
    EVENT=`inotifywait $RECURSE $NOTIFY_ARGS $WATCH_DIRS`

    # parse the event into DIR, TAGS, FILE
    OLDIFS=$IFS ; IFS=" " ; set -- $EVENT
    E_DIR=
    E_TAGS=
    E_FILE=
    IFS=$OLDIFS

    # skip if it's not a file event or already executable (unlikely)
    if [ ! -f "$E_DIR$E_FILE" ] || [ -x "$E_DIR$E_FILE" ] ; then
        continue
    fi

    # set file executable
    chmod +x $E_DIR$E_FILE
done