linux del and undel exercise

del script

#!/usr/bin/bash

TRASH=/trash
DB=/trash/db
touch /trash/db

mkdir ${TRASH} 2> /dev/null
if (($# == 0))
then
echo "usage: del "
fi

FILE=`basename $1`
DIR=`dirname $1`
DIR=`echo $DIR|sed 's/\//_/g'`
SUFFIX=`date +%Y_%m_%d_%H_%M_%S`
FILE=${FILE}_${SUFFIX}_$DIR
ID=`wc -l ${DB}|awk '{print $1}'`
echo $ID
((ID+=1))

echo "deleting $1"
echo ${ID}_${FILE} >> ${DB}
mv $1 /trash/${FILE}

echo "done"
#end script

=======

undel script
=======
#!/usr/bin/bash

TRASH=/trash
DB=/trash/db

if (($# == 0))
then
echo "usage: undel "
fi

for i in `grep $1 ${DB}`
do
ID=`echo $i|awk -F"_" '{print $1}'`
FILE=`echo $i|awk -F"_" '{print $2}'`
VERSION=`echo $i|awk -F"_" '{print $3"-"$4"-"$5"-"$6"-"$7"-"$8}'`
echo $ID $FILE $VERSION
done

read -p "Choose version number : " ID

if grep "^$ID" $DB > /dev/null 2>&1
then
RESTORE=`grep "^$ID" $DB | awk -F "_" '{print $2"_"$3"_"$4"_"$5"_"$6"_"$7"_"$8"_"$9}'`
FILE=`echo $RESTORE|awk -F"_" '{print $1}'`
cp ${TRASH}/${RESTORE} $FILE
fi

#end script

This entry was posted in Uncategorized. Bookmark the permalink.

Comments are closed.