在Linux中创建可执行文件

发布于 2024-07-18 21:30:23 字数 418 浏览 2 评论 0原文

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

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

如果做不到这一点,有没有办法声明在特定目录中创建的所有文件都应该具有执行权限?

我的 umask 设置为 022,据我了解,应该没问题,但这些文件似乎是作为文本文件(默认权限为 666)而不是可执行文件(默认权限为 777)创建的。

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

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:

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.

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).

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

攀登最高峰 2024-07-25 21:30:23

使文件可执行:

chmod +x 文件

查找 perl 的位置:

哪个perl

这应该返回类似的内容

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

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

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

#!/bin/perl

然后你可以执行该文件

./文件

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

Make file executable:

chmod +x file

Find location of perl:

which perl

This should return something like

/bin/perl sometimes /usr/local/bin

Then in the first line of your script add:

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

#!/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 ...

孤独患者 2024-07-25 21:30:23

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

相反,我们可以提出一个脚本来监视您的开发目录和 chmod 文件的创建。 这就是我在附加的 bash 脚本中所做的。 您可能想阅读注释并根据您的需要编辑“config”部分,然后我建议将其放在 $HOME/bin/ 目录中并将其执行添加到 $HOME/.login 或类似文件中。 或者您可以从终端运行它。

该脚本确实需要 inotifywait,它位于 Ubuntu 上的 inotify-tools 包中,

sudo apt-get install inotify-tools

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

#!/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 $0`"
MATCHES=`ps ax | egrep $SCRIPT | grep -v grep | awk '{print $1}' | 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=$1
    E_TAGS=$2
    E_FILE=$3
    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

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.

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

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 $0`"
MATCHES=`ps ax | egrep $SCRIPT | grep -v grep | awk '{print $1}' | 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=$1
    E_TAGS=$2
    E_FILE=$3
    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
苏别ゝ 2024-07-25 21:30:23

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

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

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

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

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.

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.

au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent !chmod +x <afile> | endif | endif
最单纯的乌龟 2024-07-25 21:30:23

这确实没什么大不了的。 您可以使用单个命令创建一个脚本:

chmod a+x *.pl

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

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

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:

touch filename.pl && chmod a+x filename.pl && vi filename.pl # choose your favorite editor
时光倒影 2024-07-25 21:30:23

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

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

有鉴于此,必须 chmod 您的文件确实不是那么糟糕,对吧?

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).

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.)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文