I am trying to make most of it, and I reached a point where I don't know exactly where the problem is.
Current script:
Code:
#!/bin/bash
# Automatically erase Vim swap files, when they are not necessary (no information lost)
# Calls `vimdiff -o` if there are unsaved changes
COPIA=$(mktemp /dev/shm/arq.XXX)
echo Copy = $COPIA
VIM_SCRIPT=$(mktemp /dev/shm/vim_script.XXX)
echo Vim script = $VIM_SCRIPT
MD5_SUM=$(mktemp /dev/shm/md5_sum.XXX)
echo MD5 file = $MD5_SUM
# #############
# If no arguments are given, just open vim
if (( $# < 1)); then
vim
exit
else
sleep 1 # should be an "elif more than 1 argument, give all to vim
fi
# #############
# 1. Check if the file (if possible, more than one argument?) has a .swp
# file.
# File should exist and be readable, or we do not proceed
if [[ !( (-r $1) && !(-d $1) ) ]]; then
echo Cannot read file or it is a directory
# Swap file exists and is readable?
if [[ !( ( -r .$1.swp ) && !( -f .$1.swp ) ) ]]; then
echo Also cannot read swap file
fi
exit
else
# #############
# 2. If swap does not exist, just open the file. End.
# Swap file not there?
if [[ !( -r .$1.swp ) ]]; then
echo "Just open"
vim $1
exit
fi
fi
# #############
# 3. If it does, do:
# #############
# 3.2. Recover the file, save it to a temporary folder (no change to
# actual file!). Use /dev/shm to save disk a bit.
# Make a vim script file to save the temporary fily
echo ":sav $COPIA" > $VIM_SCRIPT
echo ":q!" >> $VIM_SCRIPT
# This recovers the file with the swap file, executes the vim script to
# save it somewhere else and quits, making no change to the original file
echo chamando: vim -r $1 -s $VIM_SCRIPT
vim -r $1 -s $VIM_SCRIPT
# #############
# 3.3. Compare the MD5 sums of both files.
echo "line: md5sum $COPIA"
recovered_md5=$( md5sum $COPIA | sed -e 's/\s.*$//' )
echo Rec: $recovered_md5
echo Arqmd5: $MD5_SUM
echo -e "$recovered_md5 $1" > $MD5_SUM
echo Checking
if md5sum --status -c $MD5_SUM; then
echo samesum=1
else
echo samesum=0
fi
# #############
# 3.4. If the sum is the same: delete the .swp file, the temporary file
# and open the file normally. End.
declare -i x=0
((x)) && echo "x zero" $x
x=1
((x)) && echo "x um" $x
# #############
# 3.5. If the sum is not the same, open both files with 'vimdiff -o'
Steps to test:
Code:
cd /dev/shm
echo something > teste.txt
vim teste.txt
# In another terminal, kill the vim we just started:
kill -9 $that_vim
# Wherever:
cd /dev/shm
/path/to/script.sh teste.txt
The output for script.sh is:
Code:
$/var/tmp/rec.sh teste.txt
Copy = /dev/shm/arq.9Zh
Vim script = /dev/shm/vim_script.CYZ
MD5 file = /dev/shm/md5_sum.C3s
calling: vim -r teste.txt -s /dev/shm/vim_script.CYZ
line: md5sum /dev/shm/arq.9Zh
^[[>1;2305;0cRec: d41d8cd98f00b204e9800998ecf8427e
Arqmd5: /dev/shm/md5_sum.C3s
Checking
md5sum: teste.txt: File or directory not found
samesum=0
x um 1
I do not understand why md5sum cannot find the file. It is in the current directory! I also do not understand why some garbage is written in the line below "line: md5sum /dev/shm/arq.9Zh".
Does this thread stop being in the email messages "Zero reply threads" when I reply to myself? It should not, right? At least while I cannot solve the problem.