在Linux中创建可执行文件
我计划做的一件事是编写(极其简单的)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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使文件可执行:
查找 perl 的位置:
这应该返回类似的内容
然后在脚本的第一行添加:
然后你可以执行该文件
PATH 可能存在一些问题,因此您可能也想更改它......
Make file executable:
Find location of perl:
This should return something like
Then in the first line of your script add:
Then you can execute the file
There may be some issues with the PATH, so you may want to change that as well ...
无需破解您的编辑器或切换编辑器。
相反,我们可以提出一个脚本来监视您的开发目录和 chmod 文件的创建。 这就是我在附加的 bash 脚本中所做的。 您可能想阅读注释并根据您的需要编辑“config”部分,然后我建议将其放在 $HOME/bin/ 目录中并将其执行添加到 $HOME/.login 或类似文件中。 或者您可以从终端运行它。
该脚本确实需要 inotifywait,它位于 Ubuntu 上的 inotify-tools 包中,
欢迎提出建议/编辑/改进。
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,
Suggestions/edits/improvements are welcome.
您所描述的是处理此问题的正确方法。
你说你想留在 GUI 中。 您通常可以通过文件属性菜单设置执行位。 如果您愿意,您还可以学习如何为上下文菜单创建自定义操作来为您执行此操作。 当然,这取决于您的桌面环境。
如果您使用更高级的编辑器,则可以编写保存文件时要发生的操作的脚本。 例如(我只非常熟悉 vim),您可以将其添加到 .vimrc 中,以使任何以“
#!/*/bin/*
”开头的新文件可执行。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.这确实没什么大不了的。 您可以使用单个命令创建一个脚本:
并在创建 perl 文件后运行该脚本。 或者,您可以使用如下命令打开文件:
It's really not that big of a deal. You could just make a script with the single command:
And run the script after creating a perl file. Alternatively, you could open a file with a command like this:
我认为您遇到的问题是,即使您可以在系统中设置自己的 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?