common_func.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/bash
  2. function func_parser_key() {
  3. local strs=$1
  4. local IFS=':'
  5. local array=(${strs})
  6. local tmp=${array[0]}
  7. echo ${tmp}
  8. }
  9. function func_parser_value() {
  10. local strs=$1
  11. local IFS=':'
  12. local array=(${strs})
  13. local tmp=${array[1]}
  14. echo ${tmp}
  15. }
  16. function func_parser_value_lite() {
  17. local strs=$1
  18. local IFS=$2
  19. local array=(${strs})
  20. local tmp=${array[1]}
  21. echo ${tmp}
  22. }
  23. function func_set_params() {
  24. local key=$1
  25. local value=$2
  26. if [ ${key}x = 'null'x ];then
  27. echo " "
  28. elif [[ ${value} = 'null' ]] || [[ ${value} = ' ' ]] || [ ${#value} -le 0 ];then
  29. echo " "
  30. else
  31. echo "${key}=${value}"
  32. fi
  33. }
  34. function func_parser_params() {
  35. local strs=$1
  36. local IFS=':'
  37. local array=(${strs})
  38. local key=${array[0]}
  39. local tmp=${array[1]}
  40. local IFS='|'
  41. local res=''
  42. for _params in ${tmp[*]}; do
  43. local IFS='='
  44. local array=(${_params})
  45. local mode=${array[0]}
  46. local value=${array[1]}
  47. if [[ ${mode} = ${MODE} ]]; then
  48. local IFS='|'
  49. echo $value
  50. break
  51. fi
  52. IFS="|"
  53. done
  54. echo ${res}
  55. }
  56. function status_check() {
  57. local last_status=$1 # the exit code
  58. local run_command=$2
  59. local run_log=$3
  60. local model_name=$4
  61. if [ $last_status -eq 0 ]; then
  62. echo -e "\033[33m Run successfully with command - ${model_name} - ${run_command}! \033[0m" | tee -a ${run_log}
  63. else
  64. echo -e "\033[33m Run failed with command - ${model_name} - ${run_command}! \033[0m" | tee -a ${run_log}
  65. fi
  66. }
  67. function download_and_unzip_dataset() {
  68. local ds_dir="$1"
  69. local ds_name="$2"
  70. local url="$3"
  71. local clear="${4-True}"
  72. local ds_path="${ds_dir}/${ds_name}"
  73. local zip_name="${url##*/}"
  74. local zip_path="${ds_dir}/${zip_name}"
  75. if [ ${clear} = 'True' ]; then
  76. rm -rf "${ds_path}"
  77. fi
  78. wget -O "${zip_path}" "${url}" --no-check-certificate
  79. unzip "${zip_path}" -d "${ds_dir}"
  80. # The extracted file/directory must have the same name as the zip file.
  81. local extd_path="${ds_dir}/${zip_name%.*}"
  82. if [ ! "${extd_path}" -ef "${ds_path}" ]; then
  83. mv "${extd_path}" "${ds_path}"
  84. fi
  85. }
  86. function parse_extra_args() {
  87. local lines=("$@")
  88. local last_idx=$((${#lines[@]}-1))
  89. local IFS=';'
  90. extra_args=(${lines[last_idx]})
  91. }
  92. function add_suffix() {
  93. local ori_path="$1"
  94. local suffix=$2
  95. local ext="${ori_path##*.}"
  96. echo "${ori_path%.*}${suffix}.${ext}"
  97. }
  98. function parse_first_value() {
  99. local key_values=$1
  100. local IFS=':'
  101. local arr=(${key_values})
  102. echo ${arr[1]}
  103. }
  104. function parse_second_value() {
  105. local key_values=$1
  106. local IFS=':'
  107. local arr=(${key_values})
  108. echo ${arr[2]}
  109. }
  110. function run_command() {
  111. local cmd="$1"
  112. local log_path="$2"
  113. if [ -n "${log_path}" ]; then
  114. eval ${cmd} | tee "${log_path}"
  115. test ${PIPESTATUS[0]} -eq 0
  116. else
  117. eval ${cmd}
  118. fi
  119. }