Nano write check before editing

Wed, Jul 8, 2009

Linux

Have you ever been using nano to edit a long configuration file and when you've just finished you go to save and you get "Error writing /dir/somefile: Permission denied" as you've forgotten to use sudo or switch user if so this tutorial might just be your solution. What this does is it runs a script to check if you have write access on the file if you do it will continue and open nano as normal if you don't it will ask you do you want to continue so lets get started

  1. Save the following to /usr/bin/writecheck
    #!/bin/bash
    FILE="$1"
    
    [ $# -eq 0 ] && exit 1
    
    if [ -w "$FILE" ]
    then
    nano $FILE
    else
       echo "You Do Not Have Write Acess To $FILE"
       echo -n "Do You Want To Continue (y/n)?"
       read reply
       [ $reply == "y" ] && nano $FILE
    fi
  2. Allow the script to be executed
    chmod +x /usr/bin/writecheck
  3. Finally we need to add a alias for it
    echo "alias nano='writecheck'" >> /etc/bash.bashrc
, , , , , , , , ,

This post was written by:

Duffy - who has written 48 posts on Tuts4Tech.

I am the owner of this website, please feel free to ask me any questions you have

Contact the author

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Leave a Reply