Simple bash script to run the editor

    I’m a complete 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 already understand how to read my verbal blizzard):

    Before starting the editor, it checks to see if the user has permission to modify the file. If there are no rights, it starts 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 do not need to read):

    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 an old habit in gnome-commander (or in another file manager) I press F4.
    And now - a situation that I have already encountered several times. I need to edit some kind of 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, press F4, edit, try to save and ... I get a message about the lack of rights. Well, yes, of course, I had to run the editor on behalf of the superuser, but I know, I know ... But now what to do now? There would be a button like “change current user ...” right here, but no. Therefore, you have to copy all the text, close the editor, open the terminal, again look for the desired directory, start gksu gedit sources.list with your hands, 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, then this, of course, will solve the problem, but this approach is obviously bad.

    So I came up with this solution:
    1. Create a text file called edit
    2. Paste the above code into it
    3. Save
    4. We give this file permission to execute
    5. Put it in / usr / local / bin
    6. In the settings of gnome-commander (or your favorite file manager) for editing, assign the edit% s command
    You can also call edit [file name] from the command line.

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

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

    Also popular now: