2

I've used shell environments in the past where the text editor "nano" will warn me if I don't have write permissions on a file when I open the file for editing. This saves me a lot of time when I forget to type "sudo" beforehand and then spend time editing the file only to have to do all over it again.

I'd like to duplicate this behavior on several other servers, but don't see anywhere in the nano manpages where this is an option.

1
  • Are you sure it was nano? I've only seen this behavior with VI and VIM.
    – Josh Budde
    Sep 30, 2009 at 19:43

2 Answers 2

2

Why not replace the nano command with a script that checks to see if you have write permissions and automatically call sudo nano if you don't? Saving you even more time!

You might want to have the script output that you don't have write permissions before calling sudo nano so that you're not wondering why you're being asked for your password.

1
2

I modified the script linked to in the correct answer to check whether the user has write permissions for files that don't exist (checks the proper directory permissions), and a few other updates.

This is a modification of: http://tuts4tech.net/2009/07/08/nano-write-check-before-editing/

#!/bin/bash
FILE="$1"

[ $# -eq 0 ] && exit 1

if [ -w "$FILE" ] || ( [ ! -e "$FILE" ] && [ -w `readlink -f $FILE | grep -o '/.*/'` ] ) ; then
   nano $FILE
else
   tput bel
   echo "WARNING: You do not have write acess to $FILE"
   echo -n "Continue? y/[n]: "
   read REPLY
   if [ $REPLY ] && [ $REPLY == "y" ] ; then
        nano $FILE
   fi
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .