-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpusub
More file actions
executable file
·118 lines (102 loc) · 2.9 KB
/
cpusub
File metadata and controls
executable file
·118 lines (102 loc) · 2.9 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
111
112
113
114
115
116
117
118
#!/usr/bin/env bash
function cpulook/initialize-cpudir {
unset -f "$FUNCNAME"
local script=$0
if [[ -h $script ]]; then
local path=$(realpath "$0" || readlink -f "$0" || /bin/readlink -f "$script") 2>/dev/null
[[ $path ]] && script=$path
fi
[[ $script =~ ^(/?)(.*/)[^/]*$ ]]
local script_dir=${BASH_REMATCH[1]:-$PWD/}${BASH_REMATCH[2]}
script_dir=${script_dir%/}
if [[ -d ${script_dir:-/} ]]; then
cpudir=$script_dir
elif local dir=${XDG_DATA_HOME:-$HOME/.local/share}/cpulook; [[ -d $dir ]]; then
cpudir=$dir
else
cpudir=$script_dir
fi
# Go to the parent directory until we find $cpudir/lib/cpudef.bash
while
local common=$cpudir/lib/cpudef.bash
if [[ -f $common ]]; then
if [[ ! -r $common ]]; then
printf '%s\n' "$script: permission denied for the cpulook directory." >&2
return 1
fi
source "$common"
return "$?"
fi
[[ $cpudir == */* ]]
do
cpudir=${cpudir%/*}
done
cpudir=
printf '%s\n' "$script: failed to detect the cpulook directory." >&2
return 1
}
cpulook/initialize-cpudir || exit "$?"
##----CPULOOK_COMMON_HEADER_END----
source "$cpudir/lib/echox.bash"
#------------------------------------------------------------------------------
# argument
print_usage(){
cat <<EOF
usage: cpusub [options] command...
options
-i HOST instant execution on the specified HOST
-o FNAME set the standard output file
-e FNAME set the standard error file
-l FNAME set standard output and standard error file.
it is the same as the options '-o FNAME -e FNAME'.
EOF
}
ihost=
stderr=
stdout=
while [[ $1 == -* ]]; do
case "$1" in
(-i) ihost=$2 ; shift 2 ;;
(-i*) ihost=${1:2} ; shift ;;
(-o) stdout=$2 ; shift 2 ;;
(-o*) stdout=${1:2} ; shift ;;
(-e) stderr=$2 ; shift 2 ;;
(-e*) stderr=${1:2} ; shift ;;
(-l) stdout=$2 ; stderr=$2 ; shift 2 ;;
(-l*) stdout=${1:2} ; stderr=${1:2} ; shift ;;
(--help)
print_usage
exit
;;
(*)
cpulook/print "cpusub: unknown option '$1'" >&2
exit 1
;;
esac
done
#------------------------------------------------------------------------------
command="$*"
if [[ ! $command ]]; then
print_usage
exit 1
fi
if [[ $ihost ]]; then
cpulook/parse-host "$ihost" || exit 1
redirect=
[[ $stdout ]] && redirect="$redirect 1>$stdout"
[[ $stderr ]] && redirect="$redirect 2>$stderr"
echom "host=$host cd='$PWD' cmd=($command$redirect)"
rsh "$host" ". ~/.bashrc ; cd $PWD ; $command$redirect"
else
[[ $stderr ]] || stderr=/dev/null
[[ $stdout ]] || stdout=/dev/null
if [[ $stderr == "$stdout" ]]; then
redirect=" &>$stdout"
else
redirect=" 1>$stdout 2>$stderr"
fi
echom "throwing task:"
echom "- cd=$PWD"
echom "- ($command$redirect)"
cpulook/print ". ~/.bashrc ; cd $PWD ; $command$redirect" >> "$cpudir/task.txt"
fi