|
|
@ -7,61 +7,58 @@ |
|
|
|
# |
|
|
|
# usage cp_keep.sh [src file] [dest folder] [prefix newfile] |
|
|
|
|
|
|
|
|
|
|
|
export IFS=$'\n'; |
|
|
|
export src=$1; |
|
|
|
export dst=$2; |
|
|
|
export prefix=$3 |
|
|
|
export IFS=$'\n' |
|
|
|
src=$1 |
|
|
|
dst=$2 |
|
|
|
prefix=$3 |
|
|
|
|
|
|
|
if [ -z $1 ]; then { |
|
|
|
echo "must specify source file"; |
|
|
|
exit; |
|
|
|
} fi |
|
|
|
echo "must specify source file" |
|
|
|
exit |
|
|
|
}; fi |
|
|
|
|
|
|
|
if [ -z $2 ]; then { |
|
|
|
echo "must specify destination directory"; |
|
|
|
exit; |
|
|
|
} fi |
|
|
|
|
|
|
|
echo "must specify destination directory" |
|
|
|
exit |
|
|
|
}; fi |
|
|
|
|
|
|
|
if [ ! -d $2 ]; then { |
|
|
|
echo "destination must be a directory"; |
|
|
|
exit; |
|
|
|
} fi |
|
|
|
echo "destination must be a directory" |
|
|
|
exit |
|
|
|
}; fi |
|
|
|
|
|
|
|
if [ -d $1 ]; then { |
|
|
|
echo "$1 is directory. .. quitting."; |
|
|
|
exit; |
|
|
|
} fi |
|
|
|
|
|
|
|
export src_dir=$(dirname $src); |
|
|
|
export src_file=$(basename $src); |
|
|
|
echo "$1 is directory. .. quitting." |
|
|
|
exit |
|
|
|
}; fi |
|
|
|
|
|
|
|
src_dir=$(dirname $src) |
|
|
|
src_file=$(basename $src) |
|
|
|
|
|
|
|
if [ "$dst" -ef "$src_dir" ]; then { |
|
|
|
echo "source and destiation are same directory... quitting" |
|
|
|
exit; |
|
|
|
} fi |
|
|
|
|
|
|
|
export fname_new=$prefix$src_file; |
|
|
|
|
|
|
|
count=1; |
|
|
|
while [ -e $dst/$fname_new ]; do { #destination filename already taken? |
|
|
|
#add suffix before any dot, if no dot just append |
|
|
|
if [[ "$fname_new" =~ \. ]]; then { |
|
|
|
fname_new=$(echo $fname_new | sed -e "s/\./_$count\./g"); |
|
|
|
} else { |
|
|
|
fname_new=$fname_new"_$count" |
|
|
|
} fi |
|
|
|
# echo "filename already taken, suffix added: $dst/$fname_new" |
|
|
|
count=$(($count+1)); |
|
|
|
} done |
|
|
|
echo "source and destiation are same directory... quitting" |
|
|
|
exit |
|
|
|
}; fi |
|
|
|
|
|
|
|
export fname_new=$prefix$src_file |
|
|
|
|
|
|
|
count=1 |
|
|
|
while [ -e $dst/$fname_new ]; do { #destination filename already taken? |
|
|
|
#add suffix before any dot, if no dot just append |
|
|
|
if [[ "$fname_new" =~ \. ]]; then { |
|
|
|
fname_new=$(echo $fname_new | sed -e "s/\./_$count\./g") |
|
|
|
}; else { |
|
|
|
fname_new=$fname_new"_$count" |
|
|
|
}; fi |
|
|
|
# echo "filename already taken, suffix added: $dst/$fname_new" |
|
|
|
count=$(($count + 1)) |
|
|
|
}; done |
|
|
|
|
|
|
|
if [ "$count" -gt 1 ]; then { |
|
|
|
echo "$dst/$src_file already existed, new name: $fname_new"; |
|
|
|
} fi |
|
|
|
echo "$dst/$src_file already existed, new name: $fname_new" |
|
|
|
}; fi |
|
|
|
|
|
|
|
export cmd="cp \"$src_dir/$src_file\" \"$dst/$fname_new\""; |
|
|
|
export cmd="cp \"$src_dir/$src_file\" \"$dst/$fname_new\"" |
|
|
|
|
|
|
|
#echo $cmd; |
|
|
|
eval $cmd; |
|
|
|
eval $cmd |
|
|
|