I need to backup and then recover multiple files in different directorys with /bin/sh
As the title say. I need to be able to backup and then recover files.开发者_如何学运维
To make the back up was pretty simple
if [ "$bup" = "true" ]; then
cp $file $file.bak
But to make the recovery.. was not as stright forward.
elif [ "$rup" = "true" ]; then
bak=`find /path/to/file/ | grep .bak`
cp $bak ${bak/\.bak}
fi
It works in bash but i need it to work in sh..
The bash shell parameter expansion doesn't work in sh. Try this:
elif [ "$rup" = "true" ]; then
bak=`find /path/to/file/ | grep .bak`
orig=`echo $bak | sed 's/\.bak$//'`
cp $bak $orig
fi
Don't forget to add a loop around that find output if you intend to process multiple .bak files.
Use substring processing instead of the regex variant:
orig=${bak%.bak}
精彩评论