Repository navigation
Expand file tree
/
Copy pathadvanced
More file actions
110 lines (90 loc) · 2.6 KB
/
Copy pathadvanced
File metadata and controls
110 lines (90 loc) · 2.6 KB
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/bash
# No globbing
set -f
HISTFILE=~/.bash_history
# Absolute paths to executables - Specify the versions we want to use. We don't want to use whatever is in ${PATH} (as specified in the assignment)
CAT=/usr/bin/cat
CLEAR=/usr/bin/clear
CP=/usr/bin/cp
GREP=/usr/bin/grep
FIND=/usr/bin/find
MKDIR=/usr/bin/mkdir
PRINTF=/usr/bin/printf
# Use the built in for ulimit and exit
EXIT="command exit"
ULIMIT="command ulimit"
function showcpunumber() {
local LOGICALCORES
LOGICALCORES=$(${CAT} /proc/cpuinfo | ${GREP} processor -c)
if [ "${LOGICALCORES}" ]; then
$PRINTF "Amount of cpu cores = ${LOGICALCORES} \n"
else
$PRINTF "Failed to get cpu core count.\n"
fi
}
function showmaximumfilehandlecount() {
local FILEHANDLESCAP
FILEHANDLESCAP=$(${ULIMIT} -n)
if [ "${FILEHANDLESCAP}" ]; then
$PRINTF "Max open file handles = ${FILEHANDLESCAP}\n"
else
$PRINTF "Failed to obtain maximum file handle count\n"
fi
}
function findandcopy() {
local WILDCARD
local DESTINATION
WILDCARD=${1}
DESTINATION=${2}
if ! $MKDIR -p "${DESTINATION}" 2>/dev/null ; then
$PRINTF "\nFailed to create ${DESTINATION}. Exiting.\n"
$EXIT 3
fi
# Find any files that match the WILDCARD specified. Exec a copy of the found file to the DESTINATION and redirect STDERR to /dev/null as we want to handle errors ourself.
if ! $FIND . -type f -name "${WILDCARD}" -exec $CP {} "${DESTINATION}" \; ; then
$PRINTF "\nFailed to copy files matching ${WILDCARD} to ${DESTINATION}.\n"
$EXIT 3
fi
$PRINTF "\nFiles have been copied matching ${WILDCARD} to ${DESTINATION}\n"
}
function showusage() {
$PRINTF "\nusage: $0 -p - to show the amount of cpus available in the system"
$PRINTF "\nusage: $0 -o - to show the maximum number of open files allowed per user"
$PRINTF "\nusage: $0 -b [WILDCARD] [DESTINATION] - searches for all files that match the WILDCARD specified and copies them to the DESTINATION specified.\n"
$EXIT 3
}
if [ -z "$1" ]; then
$PRINTF "\nNo arguments supplied. Usage examples are :\n"
showusage
$EXIT 3
fi
${CLEAR}
declare -i ERRORARGS=0
declare -i idx
while getopts ":pob:c:" arg; do
case "${arg}" in
"p")
showcpunumber
;;
"o")
showmaximumfilehandlecount
;;
"b")
params=($@)
WILDCARD=${OPTARG}
# Obtain the destination
idx=$((OPTIND-1))
DESTINATION=${params[$idx]}
findandcopy "${WILDCARD}" "${DESTINATION}"
;;
*)
((ERRORARGS++))
;;
esac
done
shift $((OPTIND-1))
# If at least one unsupported argument as found, then show usage examples
if [ "${ERRORARGS}" -gt 0 ]; then
$PRINTF "\nUnsupported arguments found. Usage examples are :\n"
showusage
fi