-
Notifications
You must be signed in to change notification settings - Fork 1
/
scriptfor
executable file
·145 lines (120 loc) · 3.6 KB
/
scriptfor
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env bash
set -o errexit \
-o nounset \
-o pipefail \
-o noclobber
active_job_search_dir=/var/opt/sge/shared/qmaster/job_scripts
archive_job_search_dir=/var/opt/sge/shared/saved_job_scripts
function Usage() {
echo "
usage: $0 [-p|-c] [-d DIR] jobid [jobid [...]]
$0 -D
$0 -h
Options:
-h show this help message
-p print path to script(s) instead of opening
-c cat -v script(s) instead of using \$PAGER (shows non printing chars)
-d DIR use DIR as active (flat) search directory instead of default
-k DIR use DIR as archive (date-filed) search directory instead of default
-D print default search directories
-a keep searching after first find
Active job search dir is expected to be a flat directory full of job-ID named files.
Archive job search dir is expected to be a directory containing YYYY-MM-DD directories
each containing job-ID named files.
By default, only the first match will be shown, searching the active dir first, then
archive dirs in reverse order (to find the most recent).
"
}
if [ -z "$1" ] ; then
Usage
exit 1
fi
# Defaults
print_mode="pager"
stop_on_first_result="yes"
while getopts ":hpcd:k:aD" opt; do
case $opt in
h)
Usage
exit 0
;;
p)
print_mode="just paths"
;;
c)
print_mode="cat"
;;
a)
stop_on_first_result="no"
;;
d)
active_job_search_dir="$OPTARG"
;;
k)
archive_job_search_dir="$OPTARG"
;;
D)
printf "Active search dir: %s\nArchive search dir: %s\n" "$active_job_search_dir" "$archive_job_search_dir"
exit 0
;;
:)
echo "Invalid parameter to option \"$OPTARG\"" >&2
exit 3
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 4
;;
esac
done
shift $((OPTIND-1));
declare -a files_found
while [[ -n "${1:-}" ]]; do
if [[ ! "$1" =~ ^[0-9]+$ ]]; then
echo "Invalid argument: $1" >&2
exit 2
fi
found_for_this_jobid=0
# First search active jobs
if [[ -f "$active_job_search_dir/$1" ]]; then
files_found+=("$active_job_search_dir/$1")
found_for_this_jobid=$((found_for_this_jobid + 1))
if [[ "$stop_on_first_result" == "yes" ]]; then
shift
continue
fi
fi
# Then try searching the date-marked directories :(
#shellcheck disable=SC2010
# ^-- complains about using ls to list files in a script like this
# but getting minimum latency from a networked filesystem is a listed
# and valid exception
for directory in $(ls -1U "$archive_job_search_dir" | grep -e '-' | sort -r); do
if [[ -f "$archive_job_search_dir/$directory/$1" ]]; then
files_found+=("$archive_job_search_dir/$directory/$1")
found_for_this_jobid=$((found_for_this_jobid + 1))
if [[ "$stop_on_first_result" == "yes" ]]; then
break
fi
fi
done
if [[ "${found_for_this_jobid}" -eq 0 ]]; then
echo "Job script not found for id: $1" >&2
fi
shift
done
if [ ${#files_found[*]} -eq 0 ]; then
echo "No job scripts found.">&2
exit 5
fi
case "$print_mode" in
pager)
${PAGER:-less} "${files_found[@]}"
;;
cat)
cat -v "${files_found[@]}"
;;
"just paths")
printf '%s\n' "${files_found[@]}"
;;
esac