Me again with another command-line script question :)
I made a helper script that looks like this
echo 'target.bak -> target.old_bak'
mv target.bak target.old_bak
echo 'target -> target.bak'
mv target target.bak
echo 'git clone:target -> target'
git clone ssh://Shark@mcs15:29418/our-trunk-target target
cd target
echo 'Managing hooks...'
scp -p -P 29418 Shark@mcs15:/hooks/commit-msg .git/hooks/
echo '... done. Enjoy the new clone while it lasts...'
This is pretty basic but it gets my point across - it keeps the last two clones and makes a new one, I need to retain my latest clone in the target
folder because i symlinked my source folder in my eclipse project to point there :D
I would like to make an improvement to this script - instead of renaming target.bak -> target.old_bak
and target -> target.bak
I would like the following to happen:
if bakN exists then rename bakN -> bak(N+1)
rename recursivelly bak(N-1) -> bakN
rename target to target.bak
clone new repo into target
So in case I had gone through 6 clones and I'm cloning for the seventh time I'd like this to happen:
target.bak4 -> target.bak5
target.bak3 -> target.bak4
target.bak2 -> target.bak3
target.bak1 -> target.bak2
target.bak0 -> target.bak1
target -> target.bak0
Cloning into 'target'...
This community helped me immensely with my previous mass-diffing question so I hope you will be helpful enough to provide me with a decent way to do this one as well :)
EDIT:
The finalized script looks like this, thanks Wolfie for your major contributions.
#!/bin/bash
MYUSER="Shark"
TRUNK_FOLDER="target-on-trunk"
TARGET_FOLDER="target"
if [ -n "`ls -1d -v ${TARGET_FOLDER}.bak?`" ]; then
echo "Pushing back numbered backups..."
MAXBCK=`ls -1d ${TARGET_FOLDER}.bak* 2>/dev/null | egrep -o "[0-9]+$" | sort -n | tail -n1`
if [ "$MAXBCK" != "" ]; then
for (( c=$MAXBCK; c>=0; c-- ))
do
mv -v ${TARGET_FOLDER}.bak$c ${TARGET_FOLDER}.bak$(($c+1))
done
fi
fi
if [ -e "$TARGET_FOLDER.bak" ]; then
mv -v ${TARGET_FOLDER}.bak ${TARGET_FOLDER}.bak0
fi
if [ -e "$TARGET_FOLDER" ]; then
mv -v ${TARGET_FOLDER} ${TARGET_FOLDER}.bak
fi
echo "git clone: ${TRUNK_FOLDER} -> ${TARGET_FOLDER}"
git clone ssh://${MYUSER}@mcs15:29418/${TRUNK_FOLDER} ${TARGET_FOLDER}
cd ${TARGET_FOLDER}
echo 'Managing hooks...'
scp -p -P 29418 ${MYUSER}@mcs15:/hooks/commit-msg .git/hooks/
echo '... done. Enjoy the new clone while it lasts...'