Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am currently writing a small setup script for a Linux application that needs the user to edit a configuration file before the application is started. I've chosen to make the script simply open the configuration file in Nano, and resume the script afterwards. I do, however, need to detect whether the user saved the changes (to then continue starting the application), or whether he discarded them (which would indicate the user doesn't want to continue).

I have already checked whether this is possible with the returned exit code from Nano, and it apparently isn't - it always returns 0 even if the changes were discarded. Is there another way to figure out whether the file was changed and saved, or will I have to do this in an entirely different way?

share|improve this question
1  
Make a tmp copy of the file and have the user edit that instead of the original. When they exit nano, diff the tmp file with the default to see if it was changed. –  jw013 May 4 '12 at 17:33
2  
You shouldn't default to nano, it is better to use whatever $EDITOR is set to. –  Bryan Garza May 4 '12 at 17:56
add comment

1 Answer

up vote 2 down vote accepted

You can use the stat command to check the file's modification time before and after nano. Something like:

oldtime=`stat -c %Y "$filename"`
nano "$filename"

if [[ `stat -c %Y "$filename"` -gt $oldtime ]] ; then
  echo $filename has been modified
fi

Of course, this won't detect whether nano modified the file, or some other program did, but that could be considered a feature. (You can use some other program to edit the file, and then exit nano without saving.)

share|improve this answer
add comment

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.