Answers:
另一个不错的选择是Incron。它可以在给定位置的特定条件下进行渗入处理。
因此,我可以说监视此文件夹,当您看到创建的文件时,运行命令。
就像样本清单...
/path/to/scripts IN_CREATE chmod +x $@$# # <--- this arcane bit is path ($@) + file ($#)
类似地,可以将路径/文件用作bash脚本的参数,以允许它.py
在需要时按扩展名进行过滤。
第一步,您可以在~/.vimrc
:
autocmd BufWritePost *.py silent execute "! chmod +x %"
chmod +x
所有.py
文件时,它将在所有文件名上运行。在事件列表(:h events
)中,我找不到创建新文件的事件,因此每次写入文件时我都不得不运行。第一次chmod
应用时,文件会更改,并vim
会提醒您:
"test.py" [New] 0L, 0C written
W16: Warning: Mode of file "test.py" has changed since editing started
See ":help W16" for more info.
[O]K, (L)oad File:
我尝试了几种技巧来autoread
完成此更改,但没有运气。因此,您必须按Enter两次。
启动后,下面的脚本会自动更改目录中给定类型(扩展名)的所有文件的权限(一次)。此后,脚本每5秒检查一次目录中是否有新添加的文件,如果文件为给定类型(在本例中为.py
文件),则更改权限。
它有几个选项:在这种情况下,它使新添加的文件可执行,但是也可以执行其他操作,如在行中定义:command = "chmod +x"
。此外,您可以定义(更改)应执行哪种类型的文件(语言扩展名)。
将下面的脚本复制到一个空文件中。将其另存为,change_permission.py
并通过以下命令在后台运行:
python3 <script> <folder_to_watch>
#!/usr/bin/env python3
import subprocess
import time
import sys
directory = sys.argv[1]
command = "chmod +x"; check_interval = 5; extensions = (".py")
def current_files():
read = subprocess.check_output(["ls", directory]).decode("utf-8").strip()
return [item for item in read.split("\n") if item[item.rfind("."):] in extensions]
initial_files = current_files()
for file in initial_files:
subprocess.call(["/bin/bash", "-c", command+" "+directory+"/"+file])
while True:
update = current_files()
for file in update:
if not file in initial_files:
subprocess.call(["/bin/bash", "-c", command+" "+directory+"/"+file])
initial_files = update
time.sleep(check_interval)
*注意:如果您需要sudo特权,只需使用以下命令运行脚本 sudo
这是一些信息,可能包含一些命令,请访问http://ss64.com/bash/syntax-permissions.html。
find . -type f -print0 | xargs -0 chmod 775 # change all file permissions in current directory
find . -type d -print0 | xargs -0 chmod 755 # change directory permissions
您可以使用以下标头脚本。放置mkscript.sh
在你的$PATH
。mkscript.sh
从存储python脚本的工作目录执行。该脚本会创建一些有用的标题信息,为脚本加上标题并使其可执行,然后打开所选的编辑器。在您的情况下,VIM。
我进行了修改mkscript.sh
,它将生成带有python扩展名的脚本*.py
该变量${PYTHON_VERSION}
被调用,因此PYTHON_VERSION="/usr/bin/python --version"
已添加到/etc/environment
文件中。看看https://help.ubuntu.com/community/EnvironmentVariables
#!/bin/bash -
#title :mkscript.sh
#description :This script will make a header for a PYTHON script.
#author :bgw
#date :20111101
#version :0.4
#usage :bash mkscript.sh
#notes :Install Vim and Emacs to use this script.
#bash_version :4.1.5(1)-release
#==============================================================================
today=$(date +%Y%m%d)
div=======================================
/usr/bin/clear
_select_title(){
# Get the user input.
printf "Enter a title: " ; read -r title
# Remove the spaces from the title if necessary.
title=${title// /_}
# Convert uppercase to lowercase.
title=${title,,}
# Add .sh to the end of the title if it is not there already.
[ "${title: -3}" != '.py' ] && title=${title}.py
# Check to see if the file exists already.
if [ -e $title ] ; then
printf "\n%s\n%s\n\n" "The script \"$title\" already exists." \
"Please select another title."
_select_title
fi
}
_select_title
printf "Enter a description: " ; read -r dscrpt
printf "Enter your name: " ; read -r name
printf "Enter the version number: " ; read -r vnum
# Format the output and write it to a file.
printf "%-16s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%-16s%-8s\n\
%s\n\n\n" '#!/usr/bin/python -' '#title' ":$title" '#description' \
":${dscrpt}" '#author' ":$name" '#date' ":$today" '#version' \
":$vnum" '#usage' ":./$title" '#notes' ':' '#python_version' \
":${PYTHON_VERSION}" \#$div${div} > $title
# Make the file executable.
chmod +x $title
/usr/bin/clear
_select_editor(){
# Select between Vim or Emacs.
printf "%s\n%s\n%s\n\n" "Select an editor." "1 for Vim." "2 for Emacs."
read -r editor
# Open the file with the cursor on the twelth line.
case $editor in
1) vim +12 $title
;;
2) emacs +12 $title &
;;
*) /usr/bin/clear
printf "%s\n%s\n\n" "I did not understand your selection." \
"Press <Ctrl-c> to quit."
_select_editor
;;
esac
}
_select_editor
vim
或emacs
执行此操作。