-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbcp
110 lines (87 loc) · 2.02 KB
/
bcp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/sh
#############################
# For: backup a file with a uniq name
# Usage: bcp file_or_dir
# Create: Rickie Liao <rickie622 at gmail.com>
# Version: v0.01 create 2012-02-28
##########################################################
# Usage:
# bcp [-h|--help] [-V|--version]
# bcp [-c|--confirm] dirname/filename
error( )
{
echo "$@" 1>&2
usage_and_exit 1
}
usage( )
{
echo "Usage: bcp [-h|--help] [-V|--version] [-c|--confirm] dirname/filename"
}
usage_and_exit( )
{
usage
exit $1
}
version( )
{
echo "bcp version $Version"
}
warning( )
{
echo "$@" 1>&2
EXITCODE=$(expr $EXITCODE + 1)
}
EXITCODE=0
Version=0.01
Date_time=$(date +"%Y%m%d_%H%M%S")
Confirm="Y"
_Confirm=
while test $# -gt 0
do
case $1 in
--version | -V )
version
exit 0
;;
--help | -h )
usage_and_exit 0
;;
--confirm | -c )
_Confirm="YES"
;;
-*)
error "Unrecognized option: $1"
;;
*)
break
;;
esac
shift
done
if [ $# -eq 1 ]; then
if [ -e $1 ]; then
cp_File_Dir=$1
else
error "The argument input is *not* a file or a directory, please *check* carefully!"
fi
else
error "There is no argv or more than one argv!"
fi
# check if need to confirm while copying
if [ "x$_Confirm" = "xYES" ] ; then
read -p "Sure to copy $cp_File_Dir [Y/n]: " Confirm
if [ "x$Confirm" != "xY" ] ; then
error " $cp_File_Dir *didn't* copy."
fi
fi
# check weather it is a directory or a file? If it is a dir, then exclude the "/", "/tmp", "/root" directories.
if [ -d $cp_File_Dir ] ; then
cp_Dir="$(dirname $cp_File_Dir)/$(basename $cp_File_Dir)"
if [ "x$cp_Dir" = "x///" ] || [ "x$cp_Dir" = "x/tmp" ] || [ "x$cp_Dir" = "x/root" ] || [ "x$cp_Dir" = "x//tmp" ] || [ "x$cp_Dir" = "x//root" ] ; then
error "The Directoris \"/\", \"/tmp\", \"/root\" are banned to copy!"
fi
cp -r $cp_Dir $cp_Dir.$Date_time
else
cp $cp_File_Dir $cp_File_Dir.$Date_time
fi
# test some thing new