⬆️ ⬇️

A simple bash script to run the editor.

I am quite a beginner ubuntovod. I want to share my first simple bash script:



#!/bin/bash

if [ -z "$1" ]; then

gedit

exit 0

fi

test ! -e "$1" && gedit "$1" || $(test -w "$1" && gedit "$1" || gksu gedit "$1")



A brief explanation (for those who are already understandable and unable to read my verbal blizzard):

Before launching the editor checks whether the user has the right to modify the file. If no rights, then run the editor on behalf of the superuser.

(If the input file is not specified, it opens the editor as a simple user)



Long explanation (for those who read not broke):

How do you usually edit text files? If you run gedit, vi, nano, etc. from the terminal, then the script is probably not for you.

I'm out of old habit in gnome-commander (or in another file manager) I press F4.

And now - a situation that I have repeatedly encountered. I need to edit a file, which, as it turns out, has limited access rights. For example, I want to add a new repository. I find the sources.list file, I press F4, I edit, I try to save and ... I receive a message about the lack of rights. Well, yes, of course, I also had to start the editor on behalf of the superuser, yes I know, I know ... But what now to do? There would be a “change current user ...” button right here, so no. Therefore you have to copy all the text, close the editor, open the terminal, again search for the desired directory, run gksu gedit sources.list by hand, enter the password, insert the saved text and, finally, save the changes. Is it too unergonomic?

If you always run gnome-commander or gedit on behalf of the superuser, this will, of course, solve the problem, but this approach is obviously bad.

')

Therefore, I came up with this solution:

  1. Create a text file called edit
  2. Paste the code above into it.
  3. Save
  4. Give this file permissions to execute
  5. Put it in / usr / local / bin
  6. In the settings of gnome-commander (or your favorite file manager), edit the% edit command
From the command line, you can also call edit [file name].



PS: gedit can be changed to your favorite editor. If it is a console, then gksu can be changed to sudo.



UPD: Files with a space in the name were processed incorrectly. Thanks bappoy , fixed.

Source: https://habr.com/ru/post/72167/



All Articles