Why is bash denying my shell script permission to open a text file with default text editor?

I am in: GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)

the script is: (note.sh)

 #! /bin/bash
edit="edit"
if [[ $edit = $1 ]]
then touch ~/.notes/"$2".txt $EDITOR ~/.notes/"$2".txt
else tree ~/.notes
fi

I was hoping if I input in bash: ./note.shI get output as if i had typed tree ~/.notesBut i want this script to basically accept arguments, so if I input ./note.sh edit new_notethen if new_note.txt does not exist, touch ~/.notes/new_note.txtthen (Gedit for me) text editor opening new_note.txt in terminal for editing

the else statement works, but./note.sh edit new_note returns

./note.sh: line 10: /home/username/.notes/testnote.txt: Permission denied

It does the touch but not the editor. What is being denied permission here?

Thanks in advance! I am very new to both shell scripting and askubuntu and much appreciate any help

0

3 Answers

In bash the variable $EDITOR is not set by default. However, there is a command that will invoke the default editor.

For this command it is:

editor <filename>

To set the command to your choice:

sudo update-alternatives --config editor

Example:

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:

After choosing your default editor, then all you have to do to call it in your script is:

editor ~/.notes/"$2".txt

Hope this helps!

1

The $EDITOR variable is not set, so it is blank when reaching that line. Leaving ~/.notes/"$2".txt for bash to call.
So bash then tries to execute /home/username/.notes/testnote.txt which gives permission denied because there is no executable flag set for the file.

As Terrance already mentioned, call the command editor directly or assign a valid texteditor to the variable $EDITOR.

EDITOR="/usr/bin/vi"

or

EDITOR="/usr/bin/vim"

or

EDITOR="/bin/nano"

or any other editor of your choice.

In brief: On Debian/Ubuntu: run sensible-editor.


But about the error you see:

As others have mentioned, if EDITOR is empty or not set, the unquoted expansion of $EDITOR results in nothing, so what the shell sees as the first word on the line, and as the command is the filename that comes after.

A quoted expansion "$EDITOR" would result in an empty word, which would give you a confusing error when the shell tries to run an empty string as a command:

bash: : command not found

You probably want to set a fallback in the case EDITOR is not set. You can use the ${par:-word} expansion for that. E.g. "${EDITOR:-vi}" would use the value of EDITOR, and if it's empty or unset, fall back to vi.

Then there's also VISUAL, which pretty much serves the same function, so you could check both: "${VISUAL:-${EDITOR:-vi}}".


On Ubuntu and other Debian-based systems you should use sensible-editor (instead of editor, vi or nano) since it allows a per-user setting of the default editor, unlike editor which depends on the system-wide symlink set by update-alternatives.

sensible-editor also checks VISUAL and EDITOR, so you don't need to check them explicitly, but could just run sensible-editor $file and it will do the right thing.

(Even if yours may be a single-user system, it's good to keep in mind that multi-user systems do exist, and you can have multiple users even on a desktop machine.)

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like