-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.sh
executable file
·165 lines (140 loc) · 3.98 KB
/
functions.sh
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#-----------------------------------------------------------------------
# absolute value function
#-----------------------------------------------------------------------
function abs() {
local in="${1}"
if [ $in -lt 0 ]; then
in=$(( -1*in ))
fi
echo $in
}
function test_abs() {
local rslt1=$(abs -5)
local rslt2=$(abs 5)
if [ $rslt1 -eq 5 ] && [ $rslt2 -eq 5 ]; then
echo "abs: Passed. Result=${rslt1}"
else
echo "abs: Failed. Result=${rslt1}"
fi
}
#-----------------------------------------------------------------------
# max function
#-----------------------------------------------------------------------
function max() {
local arg_list=("${@}")
local out=0
for elem in "${arg_list[@]}"; do
if [ $elem -gt $out ]; then
out=$elem
fi
done
echo $out
}
function test_max()
{
local a=(1 2 3 7 2 5)
local rslt=$(max "${a[@]}")
if [ $rslt -eq 7 ]; then
echo "max: Passed. Result=${rslt}"
else
echo "max: Failed. Result=${rslt}"
fi
}
#-----------------------------------------------------------------------
# min function
#-----------------------------------------------------------------------
function min() {
local arg_list=("${@}")
local out="${arg_list[0]}"
for elem in "${arg_list[@]}"; do
if [ $elem -lt $out ]; then
out=$elem
fi
done
echo $out
}
function test_min()
{
local a=(2 3 5 2 7 1)
local rslt=$(min "${a[@]}")
if [ $rslt -eq 1 ]; then
echo "min: Passed. Result=${rslt}"
else
echo "min: Failed. Result=${rslt}"
fi
}
#-----------------------------------------------------------------------
# get_cur_run_dir function
#-----------------------------------------------------------------------
function get_cur_run_dir()
{
local dir_list=($(ls | grep ${NB_RUN_DIR_PREFIX} 2> /dev/null))
if [ -z "${dir_list}" ]; then # no previous runs
echo "Error: No run directories found, exitting ..."
$NB_EXIT_CMD
else
local n=${#dir_list[@]}
echo ${dir_list[$((n - 1))]}
fi
}
#-----------------------------------------------------------------------
# create_next_run_dir function
#-----------------------------------------------------------------------
function create_next_run_dir()
{
local dir_list=($(ls | grep ${NB_RUN_DIR_PREFIX} 2> /dev/null))
local dir_name=""
if [ -z "${dir_list}" ]; then # no previous runs
printf -v dir_name "${NB_RUN_DIR_PREFIX}%0${NB_RUN_DIR_NUM_LEN}d" 0
else
dir_name=$(get_cur_run_dir)
local number=${dir_name: $(( -1*NB_RUN_DIR_NUM_LEN )) }
number=$(( number + 1 ))
printf -v dir_name "${NB_RUN_DIR_PREFIX}%0${NB_RUN_DIR_NUM_LEN}d" $number
fi
mkdir $dir_name
echo $dir_name
}
#-----------------------------------------------------------------------
# dump_metadata function
#-----------------------------------------------------------------------
function dump_metadata()
{
local git_sha=$(cd $nb_nek5_dir; git rev-parse HEAD; cd - >/dev/null)
echo "case: ${nb_case_basename}" >> $NB_README_FILE
echo "git_commit: ${git_sha[0]}" >> $NB_README_FILE
echo "lelt: ${nb_lelt_list[@]}" >> $NB_README_FILE
echo "lx1: ${nb_lx1_list[@]}" >> $NB_README_FILE
echo "np: ${nb_np_list[@]}" >> $NB_README_FILE
echo "ppn: ${nb_ppn_list[@]}" >> $NB_README_FILE
}
#-----------------------------------------------------------------------
# print function
#-----------------------------------------------------------------------
function iprint()
{
local msg=$1
local indntlvl=
if [ $# -eq 2 ]; then
indntlvl=$2
else
indntlvl=0
fi
if [ $indntlvl -eq 0 ]; then
echo $msg
elif [ $indntlvl -eq 1 ]; then
sed -e "s/^/ /g" <<< ${msg}
elif [ $indntlvl -eq 2 ]; then
sed -e "s/^/ /g" <<< ${msg}
elif [ $indntlvl -eq 3 ]; then
sed -e "s/^/ /g" <<< ${msg}
fi
}
#-----------------------------------------------------------------------
# Test functions
#-----------------------------------------------------------------------
if [ ${nb_test_functions} = true ]; then
test_abs
test_max
test_min
fi