-
Notifications
You must be signed in to change notification settings - Fork 1
/
break-module-exports
executable file
·72 lines (59 loc) · 2.15 KB
/
break-module-exports
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
#!/bin/bash
set -o errexit \
-o nounset \
-o pipefail \
-o noclobber
function print_help() {
echo "Usage: $0 <modules install prefix>"
echo ""
echo "This script creates and links a copy of a Tcl Env Modules"
echo " bash init script, appending commands to unexport all"
echo " the module shell functions."
echo "This is done because qrsh -- which we use to run MPI tasks"
echo " on compute nodes -- cannot handle exported shell functions"
echo " and produces very noisy errors if they are present."
echo ""
echo "Because this performs the symlink as the final operation,"
echo " it should be safe to run live."
echo ""
}
if [[ "$#" != 1 ]]; then
print_help >&2
exit 1
fi
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
print_help
exit 0
fi
target_dir="$1"
target_file="$target_dir/init/bash"
if [[ ! -w "$target_file" ]]; then
echo "Error: No writable bash init file found in provided directory" >&2
exit 2
fi
if grep --quiet -e '^#UNEXPORTED_FLAG_COMMENT' "$target_file"; then
echo "Detected UNEXPORTED_FLAG_COMMENT in target file: unexports already added." >&2
exit 0
fi
if [[ -f "$target_file.old" ]]; then
echo "Error: Flag not found, but found existing backup of bash init file in: $target_file.old" >&2
echo " This script does not know how to proceed in this situation." >&2
exit 3
fi
# Okay let's get on with it
echo "Making copies of old bash init file..." >&2
cp -v "$target_file" "$target_file.old"
cp -v "$target_file" "$target_file.no_export_fs"
echo "Appending unexports to copy..." >&2
cat >>"$target_file.no_export_fs" <<'EOF'
# unexports all module shell functions to avoid a bug in qrsh
declare -Fx module && export -nf module || true
declare -Fx ml && export -nf ml || true
declare -Fx _module_raw && export -nf _module_raw || true
declare -Fx switchml && export -nf switchml || true
#UNEXPORTED_FLAG_COMMENT
# ^-- this means we can check whether we've made the change to the init file
EOF
echo "Force-linking old filename to new copy..." >&2
ln -vsf "${target_file##*/}.no_export_fs" "$target_file"
echo "Done." >&2