Shell Function

commons-utils-1.0.0.shfn

 

  1 #!/bin/sh
  2 
  3 echo_n(){
  4     echo -e "\033[30;32m$*\033[0m"
  5 }
  6 
  7 echo_e(){
  8     echo -e "\033[30;35m$*\033[0m"
  9 }
 10 
 11 trim(){
 12     echo $1
 13 }
 14 
 15 is_contains(){
 16     local ps=
 17     for ps in $2; do
 18         if [ "$ps" = "$1" ]; then
 19             echo true
 20             return 1
 21         fi
 22     done
 23     echo false
 24 }
 25 
 26 chk_positive_integer(){
 27     POS_INT=
 28     local nstr=`trim "$1"`
 29     echo "$nstr" | grep "[^0-9]" >/dev/null 2>&1
 30     if [ $? -eq 1 ]&&[ ! -z "$nstr" ]; then
 31         local t=$(expr $nstr + 0)
 32         if (( $t > 0 )); then
 33             POS_INT=$t
 34             return 1
 35         fi
 36     fi
 37 }
 38 
 39 to_positive_integer(){
 40     chk_positive_integer "$1"
 41     echo $POS_INT
 42 }
 43 
 44 one_slash(){
 45     local str="$1"
 46     while [[ "$src" == *//* ]]; do
 47         local regex="//" repl="/"
 48         str="${str//$regex/$repl}"
 49     done
 50     echo $str
 51 }
 52 
 53 to_canonical_path(){
 54     local path=`trim "$1"`
 55     if [ ! -z "$path" ]&&[ -e "$path" ]; then
 56         local result=
 57         if [ -d "$path" ]; then
 58             result=`cd "$path"; pwd`
 59         elif [ -f "$path" ]; then
 60             local directory=`dirname "$path"`
 61             directory=`cd "$directory"; pwd`
 62             local filename=`basename "$path"`
 63             result="$directory/$filename"
 64         fi
 65         result=`one_slash "$result"`
 66         echo $result
 67     fi
 68 }
 69 
 70 display_time_its(){
 71     local internal_time_stamp="$1" format="$2" utc="$3"
 72     if [ ! -z "$internal_time_stamp" ]; then
 73         if [ "$format" = "" ]; then
 74             format="+%Y%m%d_%H%M%S_%N"
 75         elif [ "$format" = "datetime" ]; then
 76             format="+%Y%m%d%H%M%S"
 77         elif [ "$format" = "clock" ]; then
 78             format="+%F %T"
 79         elif [ "$format" = "date" ]; then
 80             format="+%Y%m%d"
 81         elif [ "$format" = "date-" ]; then
 82             format="+%F"
 83         elif [ "$format" = "time" ]; then
 84             format="+%H%M%S"
 85         elif [ "$format" = "time:" ]; then
 86             format="+%T"
 87         elif [ "$format" = "time:.ns" ]; then
 88             format="+%T.%N"
 89         elif [ "$format" = "clock.ns" ]; then
 90             format="+%F %T.%N"
 91         elif [ "$format" = "table" ]; then
 92             format="+%F%t%T%t%N"
 93         fi
 94         if [ -z "$utc" ]; then
 95             date -d @$internal_time_stamp "$format"
 96         else
 97             date -u -d @$internal_time_stamp "$format"
 98         fi
 99     fi
100 }
101 
102 stopwatch(){
103     local cmd="$1" msg="$2"
104     if [ "$cmd" = "start" ]; then
105         if [ "$sw_state" = "" ]; then
106             unset sw_state sw_ref_arr sw_lap_begin sw_lap_end sw_lap_arr sw_pause_begin sw_pause_end sw_pause_arr sw_msg_idx sw_lap_msg_arr sw_pause_msg_idx sw_pause_msg_arr
107             sw_lap_begin="`date +%s.%N`"
108             sw_state="R"
109             sw_lap_msg_idx=0
110             sw_pause_msg_idx=0
111             sw_pause_msg_arr[$sw_pause_msg_idx]="$msg"
112         elif [ "$sw_state" = "H" ]; then
113             sw_pause_end="`date +%s.%N`"
114             sw_pause_arr[${#sw_pause_arr[*]}]="$sw_pause_begin-$sw_pause_end"
115             sw_pause_msg_arr[$sw_pause_msg_idx]="$msg"
116             sw_state="R"
117         else
118             echo_e "(Start) requires the state to be ''/'H'."
119         fi
120     elif [ "$cmd" = "lap" ]; then
121         if [ "$sw_state" = "R" ]; then
122             sw_lap_end="`date +%s.%N`"
123             sw_lap_arr[${#sw_lap_arr[*]}]="$sw_lap_begin $sw_lap_end"
124             sw_lap_begin="$sw_lap_end"
125             sw_ref_arr[$(expr ${#sw_lap_arr[*]} - 1)]=${sw_pause_arr[*]}
126             sw_pause_arr=()
127             sw_lap_msg_arr[$sw_lap_msg_idx]="$msg"
128             let "sw_lap_msg_idx+=1"
129         else
130             echo_e "(Lap) requires the state to be 'R'."
131         fi
132     elif [ "$cmd" = "stop" ]; then
133         if [ "$sw_state" = "R" ]; then
134             sw_pause_begin="`date +%s.%N`"
135             sw_pause_msg_arr[$sw_pause_msg_idx]="'${sw_pause_msg_arr[$sw_pause_msg_idx]}' -> '$msg'"
136             let "sw_pause_msg_idx+=1"
137             sw_state="H"
138         else
139             echo_e "(Stop) requires the state to be 'R'."
140         fi
141     elif [ "$cmd" = "reset" ]; then
142         if [ "$sw_state" = "H" ]; then
143             stopwatch start "* Last start"
144             stopwatch lap "* Last lap"
145             stopwatch stop "* Last stop"
146             sw_state=
147         else
148             echo_e "(Reset) requires the state to be 'H'."
149         fi
150     else
151         if [ ! "$sw_state" = "" ]; then
152             echo_e "(*) requires the state to be ''."
153         elif [ ${#sw_lap_arr[*]} != 0 ]; then
154             printf "%4s %18s %18s %15s %18s %s\n" "Lap" "Begin" "End" "Duration" "Duration (HRF)" "Message"
155             local i= summary_exp=0 pause_msg_idx=0 lap_msg_idx=0
156             for((i=0;i<${#sw_lap_arr[*]};i++)); do
157                 local lap_item=(${sw_lap_arr[$i]})
158                 local lap_dur_exp="${lap_item[1]} - ${lap_item[0]}"
159                 local total_duration="`echo "$lap_dur_exp" | bc | awk '{ printf "%.9f\n", $1 }'`"
160                 local pause_arr=(${sw_ref_arr[$i]})
161                 local pause_dur_arr=()
162                 local j=
163                 for((j=0;j<${#pause_arr[*]};j++)); do
164                     local pause_item="${pause_arr[$j]}"
165                     pause_item=(${pause_item//-/ })
166                     pause_dur_arr[j]="`echo "${pause_item[1]} - ${pause_item[0]}" | bc | awk '{ printf "%.9f\n", $1 }'`"
167                     lap_dur_exp="$lap_dur_exp - ${pause_dur_arr[j]}"
168                 done
169                 local real_duration="`echo "$lap_dur_exp" | bc | awk '{ printf "%.9f\n", $1 }'`"
170                 printf "%4s %18s %18s %15s %18s %s\n" "$(expr $i + 1)" \
171                 "`display_time_its ${lap_item[0]} time:.ns`" \
172                 "`display_time_its ${lap_item[1]} time:.ns`" \
173                 "$real_duration" \
174                 "`display_time_its $real_duration time:.ns *`" \
175                 "${sw_lap_msg_arr[$lap_msg_idx]}"
176                 let "lap_msg_idx+=1"
177                 for((j=0;j<${#pause_arr[*]};j++)); do
178                     local pause_item="${pause_arr[$j]}"
179                     pause_item=(${pause_item//-/ })
180                     printf "%4s %18s %18s %15s %18s %s\n" "\\" \
181                     "`display_time_its ${pause_item[0]} time:.ns`" \
182                     "`display_time_its ${pause_item[1]} time:.ns`" \
183                     "${pause_dur_arr[j]}" \
184                     "`display_time_its ${pause_dur_arr[j]} time:.ns *`" \
185                     "${sw_pause_msg_arr[$pause_msg_idx]}"
186                     let "pause_msg_idx+=1"
187                 done
188                 if [ $j != 0 ]; then
189                     printf "%42s %15s %18s\n" "Total:" "$total_duration" "`display_time_its $total_duration time:.ns *`"
190                     summary_exp="$summary_exp + $total_duration"
191                 else
192                     summary_exp="$summary_exp + $real_duration"
193                 fi
194             done
195             local m=0
196             while((m < 77)); do
197                 printf "%s" "-"
198                 let "m+=1"
199             done
200             printf "\n"
201             local summary_val="`echo "$summary_exp" | bc | awk '{ printf "%.9f\n", $1 }'`"
202             printf "%42s %15s %18s\n" "Summary:" "$summary_val" "`display_time_its $summary_val time:.ns *`"
203         else
204             echo_e No data.
205         fi
206     fi
207 }
208 
209 chk_widest(){
210     local i= max=0 arr=($1)
211     for((i=0;i<${#arr[*]};i++)); do
212         if [ ${#arr[$i]} -gt $max ]; then
213             max=${#arr[$i]}
214         fi
215     done
216     WIDEST=$max
217 }
218 
219 load_config(){
220     local cfg=`to_canonical_path "$0"`
221     cfg="$cfg.ini"
222     if [ ! -f "$cfg" ]; then
223         echo_e "No such file. '$cfg'"
224         exit
225     else
226         local line=
227         while read line; do
228             eval "$line"
229         done < "$cfg"
230     fi
231 }
232 
233 :<<EOF
234 TODO: 
235 is_positive_number()
236 is_positive_integer()
237 is_negative_integer()
238 is_negative_number()
239 is_numeric()
240 is_integer()
241 # greater than 0
242 is_gt0()
243 to_numeric()
244 to_integer()
245 getopt/getopts
246 EOF

 

附件1

 

posted @ 2019-08-27 11:11  RMS365  阅读(242)  评论(0编辑  收藏  举报