Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Are you sure it was nano? I've only seen this behavior with VI and VIM. –  Josh Budde Sep 30 '09 at 19:43

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
Oh, it must have been a script. In fact, here's one I can modify: tuts4tech.net/2009/07/08/nano-write-check-before-editing –  scotts Sep 30 '09 at 20:29

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.