在Linux中创建可执行文件

42 linux executable file-permissions gedit chmod

我打算做的一件事就是编写(非常简单)Perl脚本,我希望能够在没有从终端显式调用Perl的情况下运行它们.我很欣赏,要做到这一点,我需要授予他们执行权限.使用chmod这样做很容易,但它似乎也是一个稍微费力的额外步骤.我想要的是两件事之一:

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

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

根据我的理解,我的umask设置为022,应该没问题,但看起来文件创建为文本文件(具有666默认权限)而不是可执行文件(具有777默认权限).

也许我只是在懒惰,但我认为必须有一个更方便的方式,而不是chmodding每个创建的脚本.

小智 86

使文件可执行:

chmod + x文件

找到perl的位置:

哪个perl

这应该返回类似的东西

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

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

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

#!/ bin中/ perl的

然后你可以执行该文件

./文件

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

  • 我可以推荐`#!/ usr/bin/env perl`吗?`env`程序基本上在PATH上找到给出的参数(在本例中为"perl")并运行它.当您向其他人发送脚本时,它非常有用 - 例如,我使用的是Mac,而Perl位于/ opt/local/bin中. (11认同)

Dyl*_*lan 6

无需破解编辑器或切换编辑器.

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

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

sudo apt-get install inotify-tools
Run Code Online (Sandbox Code Playgroud)

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

#!/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
Run Code Online (Sandbox Code Playgroud)


Bob*_*toe 2

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

chmod a+x *.pl
Run Code Online (Sandbox Code Playgroud)

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

touch filename.pl && chmod a+x filename.pl && vi filename.pl # choose your favorite editor
Run Code Online (Sandbox Code Playgroud)