Linux Bash:尝试打开文件时权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3842850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:33:22  来源:igfitidea点击:

Bash: Permission denied when trying to open a file

linuxbash

提问by Michi

I recently decided to try to learn some bash scripting and as a fun exercise I decided to make a script to open up a daily file for me to write notes in whenever the script is run.

我最近决定尝试学习一些 bash 脚本,作为一个有趣的练习,我决定制作一个脚本来打开一个每日文件,让我在脚本运行时写笔记。

It worked fine until I logged out and back in to the system later, when I received an error

它工作正常,直到我退出并稍后重新登录系统,当我收到错误时

/usr/local/bin/notes: line 45: /home/MY_USERNAME/notes/2010-10-01:Permission denied

/usr/local/bin/notes: line 45: /home/MY_USERNAME/notes/2010-10-01:Permission denied

Code

代码

I might be mistaken, but this certainly doesn't seem like something that shouldn't require extra permissions, does it?

我可能弄错了,但这当然看起来不像是不需要额外权限的东西,不是吗?

Editor is set to nano

编辑器设置为 nano

File's permissions are -rw-rw-r--

文件的权限是 -rw-rw-r--

Script's permissions are -rwxr-xr-x

脚本的权限是 -rwxr-xr-x

采纳答案by djna

My guess is that in

我的猜测是在

$EDITOR $DAILY_FILENAME 

$EDITORis null, so it's trying to execute $DAILY_FILENAMEwhich not executable. Probably while you were testing you set EDITOR manually, but didn't add it to your .bashrc (or whatever) file.

$EDITOR为空,所以它试图执行$DAILY_FILENAME哪个不可执行。可能在您测试时手动设置了 EDITOR,但没有将其添加到您的 .bashrc(或其他)文件中。

Use the -xoption to prove it.

使用-x选项来证明它。

回答by hvgotcodes

check the permission on the file with

检查文件的权限

ls -l /path/to/your/file

you should see something like

你应该看到类似的东西

-rw-r--r--

r mean readable, w writeable, and x executable.

r 表示可读,w 表示可写,x 表示可执行。

the first set is for your user, the second set of three is for your group, and the third set is for anyone.

第一组用于您的用户,第二组三个用于您的组,第三组用于任何人。

so in my example, the file i have shown is read/write for me, and read only for my group and for any other user.

所以在我的例子中,我显示的文件对我来说是读/写的,而对我的组和任何其他用户来说都是只读的。

Use the chmod command to change permissions.

使用 chmod 命令更改权限。

chmod 744 file

will make the file read/write/exec for you, and just read for user/world.

将使文件为您读/写/执行,并为用户/世界读取。

回答by Andrew Walker

If I had to guess, I would suggest that the $EDITORenvironment variable is undefined for some reason. It looks like your script is attempting to execute the notes file - as this isn't executable you get the unhelpful error message.

如果我不得不猜测,我会建议$EDITOR由于某种原因未定义环境变量。看起来您的脚本正在尝试执行注释文件 - 因为这不是可执行文件,所以您会收到无用的错误消息。