-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport
executable file
·81 lines (73 loc) · 2.39 KB
/
export
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
#!/bin/bash
## export settings files from this computer to the repository
## Usage:
## ./export list of files
# exports files "list", "of" and "files", if they exist in "~"
# cannot handle paths to files
# treats all filenames as if they begin with '.'
# source for files
src="$HOME"
# path to repo from ${src}
repo="config"
# dest directory for files, relative to ${repo}
dest="src"
# make sure destination can be safely written to
if ! [ -e "${dest}" ];
then # it doesn't exist
mkdir "${dest}"
elif ! [ -d "${dest}" ];
then # it does exist, but it isnt a directory
echo "error: destination directory exists, but is not a directory"
echo "aborting"
exit 1
fi
# files to be exported
if [ "$#" -le 0 ] ; # no arguments
then
echo "no files to export, aborting"
exit 1
else
files="$(
IFS=$'\n'; echo "$*"| # all the files listed in the command line args
sed 's:^\.*:.:' # all treated as hidden files
)"
fi
# files which would be overwritten
overwrites="$(
echo "${files}"| # for each file
sed 's:^\.*::'| # trim all leading '.'s. files must not be hidden
while IFS='' read file;
do
ls -1a "${dest}" | grep "^${file}$" # file exists in destination directory, as a visible file
done
)"
if [ -n "${overwrites}" ];
then
echo "This action will overwrite the following files:"
echo "${overwrites}"|
sed "s:^:${src}/${repo}/${dest}/:" # format each line nicely with the path
fi
echo -n "Are you sure? (y/N): "
read confirm
if ! [ -t 0 ] ;
then # open in a pipe
# simulate user input
echo "${confirm}"
fi
if echo "${confirm}"|grep -Eq "[Yy]+([Ee]+[Ss]+)*" ;
then # they tried to say yes
echo "${files}"| # for each file
while IFS='' read file;
do
dstFile="$(echo "${file}"|sed 's:^\.*::')" # visible name for the file
if [ -e "${src}/${file}" ];
then # source file exists
cp -fRTL "${src}/${file}" "${src}/${repo}/${dest}/${dstFile}" # copy the file, recursively, following all symlinks, treating destination folders as files, forcing overwrite
else # file doesn't exist
echo "Warning: cannot export nonexistent file \"${src}/${file}\"" # don't create bad links
fi
done
else # they said something else; assume no
echo "aborting" # let the user know nothing happened
exit 1
fi