我计划做的一件事是编写(极其简单的) Perl脚本,我希望能够在不从终端显式调用Perl的情况下运行它们。我意识到,要做到这一点,我需要授予他们执行权限。用chmod做这件事很容易,但这似乎也是一个稍微费力的额外步骤。我想要的是两件事中的一件:
首先,有没有办法在保存文件时设置执行标志?目前我正在试验gedit和geany,但如果它有这个功能,我愿意切换到类似的(或更好的)功能编辑器。
如果做不到这一点,有没有办法声明在特定目录中创建的所有文件都应该具有执行权限?
我的umask设置为022,据我所知,这应该是可以的,但看起来文件是作为文本文件(具有666默认权限)而不是可执行文件(具有777默认权限)创建的。
也许我只是在偷懒,但我想一定有一种更方便的方法,而不是修改每个创建的脚本。
发布于 2009-05-03 13:48:29
使文件成为可执行文件:
chmod +x文件
查找perl的位置:
哪一个perl
这应该返回类似如下的内容
/bin/perl有时/usr/local/bin
然后在脚本的第一行添加:
#!" path "/perl带有上面的路径,例如
#!/bin/perl
然后您就可以执行该文件了
文件./
路径可能会有一些问题,所以您可能也想要更改它...
发布于 2009-05-04 05:40:08
不需要修改你的编辑器,或者切换编辑器。
相反,我们可以想出一个脚本来监视您的开发目录和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发布于 2009-05-03 17:13:22
你所描述的才是处理这个问题的正确方法。
你说过你想留在GUI里。通常可以通过文件属性菜单设置执行位。如果您愿意的话,您还可以学习如何为上下文菜单创建自定义操作来完成此操作。当然,这取决于您的桌面环境。
如果使用更高级的编辑器,则可以编写保存文件时发生的操作的脚本。例如(我只对vim很熟悉),你可以把它添加到你的.vimrc中,使任何以"#!/*/bin/*“开头的新文件成为可执行文件。
au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent !chmod +x <afile> | endif | endifhttps://stackoverflow.com/questions/817060
复制相似问题