Ore*_*asm 5 permissions command-line bash scripts
我在:GNU bash,版本 4.4.12(1)-release (x86_64-pc-linux-gnu)
脚本是:(note.sh)
#! /bin/bash
edit="edit"
if [[ $edit = $1 ]]
then
touch ~/.notes/"$2".txt
$EDITOR ~/.notes/"$2".txt
else
tree ~/.notes
fi
Run Code Online (Sandbox Code Playgroud)
我希望如果我在 bash 中输入:
./note.sh
我得到的输出就像我输入的一样
tree ~/.notes
但我希望这个脚本基本上接受参数,所以如果我输入
./note.sh edit new_note
然后如果 new_note.txt 不存在,
touch ~/.notes/new_note.txt
那么(Gedit 对我来说)文本编辑器打开 new_note .txt 在终端中进行编辑
else 语句有效,但
./note.sh edit new_note返回
./note.sh: line 10: /home/username/.notes/testnote.txt: Permission denied
它可以触摸但不是编辑器。这里被拒绝许可是什么意思?
提前致谢!我对 shell 脚本和 askubuntu 都很陌生,非常感谢任何帮助
Ter*_*nce 11
在 bash 中$EDITOR,默认情况下未设置该变量。但是,有一个命令可以调用默认编辑器。
对于此命令,它是:
editor <filename>
Run Code Online (Sandbox Code Playgroud)
将命令设置为您的选择:
sudo update-alternatives --config editor
Run Code Online (Sandbox Code Playgroud)
例子:
terrance@terrance-ubuntu:~$ sudo update-alternatives --config editor
There are 3 choices for the alternative editor (providing /usr/bin/editor).
Selection Path Priority Status
------------------------------------------------------------
0 /bin/nano 40 auto mode
1 /bin/ed -100 manual mode
2 /bin/nano 40 manual mode
* 3 /usr/bin/vim.tiny 10 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Run Code Online (Sandbox Code Playgroud)
选择默认编辑器后,在脚本中调用它所需要做的就是:
editor ~/.notes/"$2".txt
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
该$EDITOR变量没有设置,所以到达这条线时是空白。离开~/.notes/"$2".txt的bash调用。
因此,bash然后试图执行/home/username/.notes/testnote.txt赋予权限被拒绝,因为是文件没有可执行标志设置。
正如 Terrance 已经提到的,editor直接调用命令或为变量分配一个有效的文本编辑器$EDITOR。
EDITOR="/usr/bin/vi"
Run Code Online (Sandbox Code Playgroud)
或者
EDITOR="/usr/bin/vim"
Run Code Online (Sandbox Code Playgroud)
或者
EDITOR="/bin/nano"
Run Code Online (Sandbox Code Playgroud)
或您选择的任何其他编辑器。