common_func.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/bash
  2. function func_parser_key() {
  3. strs=$1
  4. IFS=":"
  5. array=(${strs})
  6. tmp=${array[0]}
  7. echo ${tmp}
  8. }
  9. function func_parser_value() {
  10. strs=$1
  11. IFS=":"
  12. array=(${strs})
  13. tmp=${array[1]}
  14. echo ${tmp}
  15. }
  16. function func_parser_value_lite() {
  17. strs=$1
  18. IFS=$2
  19. array=(${strs})
  20. tmp=${array[1]}
  21. echo ${tmp}
  22. }
  23. function func_set_params() {
  24. key=$1
  25. 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. strs=$1
  36. IFS=":"
  37. array=(${strs})
  38. key=${array[0]}
  39. tmp=${array[1]}
  40. IFS="|"
  41. res=""
  42. for _params in ${tmp[*]}; do
  43. IFS="="
  44. array=(${_params})
  45. mode=${array[0]}
  46. value=${array[1]}
  47. if [[ ${mode} = ${MODE} ]]; then
  48. IFS="|"
  49. #echo $(func_set_params "${mode}" "${value}")
  50. echo $value
  51. break
  52. fi
  53. IFS="|"
  54. done
  55. echo ${res}
  56. }
  57. function status_check() {
  58. local last_status=$1 # the exit code
  59. local run_command=$2
  60. local run_log=$3
  61. local model_name=$4
  62. if [ $last_status -eq 0 ]; then
  63. echo -e "\033[33m Run successfully with command - ${model_name} - ${run_command}! \033[0m" | tee -a ${run_log}
  64. else
  65. echo -e "\033[33m Run failed with command - ${model_name} - ${run_command}! \033[0m" | tee -a ${run_log}
  66. fi
  67. }
  68. function download_and_unzip_dataset() {
  69. local ds_dir="$1"
  70. local ds_name="$2"
  71. local url="$3"
  72. local clear="${4-True}"
  73. local ds_path="${ds_dir}/${ds_name}"
  74. local zip_name="${url##*/}"
  75. if [ ${clear} = 'True' ]; then
  76. rm -rf "${ds_path}"
  77. fi
  78. wget -O "${ds_dir}/${zip_name}" "${url}" --no-check-certificate
  79. # The extracted file/directory must have the same name as the zip file.
  80. cd "${ds_dir}" && unzip "${zip_name}"
  81. if [ "${zip_name%.*}" != "${ds_name}" ]; then
  82. mv "${zip_name%.*}" "${ds_name}"
  83. fi
  84. cd -
  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. }