├── README.md ├── async.bash └── test ├── async.bash ├── my_file.html ├── newfile.txt ├── paralle.sh ├── setintervalbash └── settimeout.bash /README.md: -------------------------------------------------------------------------------- 1 | # async-bash 2 | 3 | ***async-bash*** is a bash script that implements few asynchronous functions 4 | 5 | This script was created to be compatible with bash versions that does not support `coproc` 6 | 7 | ## functions 8 | 9 | 1. setTimeout ( excute a function after a particular time has elapsed ) 10 | 2. setInterval ( execute a function continously after waiting for a particular number of time ) 11 | 3. async ( execute a function asynchronously ) 12 | 4. parallel ( execute bunch of functions asynchronously ); 13 | 5. KillJob ( kills a particular job ); 14 | 15 | 16 | **each of this functions returns a job id , which can be sent a kill signal** 17 | 18 | 19 | ## Usage 20 | 21 | ***setTimeout*** excute a function after a particular time has elapsed. setTimeout takes a command as the first argument and the number of milliseconds/seconds/minutes/hours that will elapse before the command is executed 22 | 23 | 24 | 25 | ```bash 26 | writeFile() { 27 | local _f="$1" 28 | 29 | [[ -f ${_f} ]] && { 30 | while read line;do 31 | echo $line >> my_syslog.txt 32 | done 33 | } 34 | } 35 | 36 | setTimeout "readFile /var/log/syslog" 2 37 | 38 | setTimeout "dmesg" 2 39 | ``` 40 | 41 | ***setInterval*** execute a function continously after waiting for a particular number of time. This functions takes the same argument as setTimeout 42 | 43 | 44 | ```bash 45 | 46 | setInterval "uptime --pretty" 10 47 | 48 | ``` 49 | 50 | 51 | ***async*** executes a function asynchronously.This command takes 3 arguments, the first argument is the command or function to execute asynchronously, the second argument is a function to invoke when the command has been succesfully excuted, and the third function is invoked when an error occurs. The second argument is been passed the result of the command, while the third argument is passed the return code of the command when it fails 52 | 53 | ```bash 54 | 55 | success() { 56 | local _content="$1" 57 | 58 | echo ${_content} > my_file.html 59 | } 60 | 61 | error() { 62 | local _err="$1" 63 | 64 | echo ${_err} 65 | } 66 | 67 | async "curl -s http://google.com/" success error 68 | 69 | async "curl -s http://googlejajajaj.com/" success error 70 | 71 | ``` 72 | 73 | ***parallel*** execute an array of functions asynchronously. This command takes three arguments. The first argument is the main function or command to execute , the result of this command if it is successful will be passed as an argument to the second argument which is an array of functions. The third argument is the final function to execute. If the main function or array of functions fails this third argument is invoked and exits with the exit status of the failed function. The final function ( third argument ) is called last if all the functions executed successfully 74 | 75 | 76 | ```bash 77 | 78 | fArray=( "readFile" "removeSpace" ) 79 | 80 | mainFunc() { 81 | local file="${1}" 82 | 83 | if [[ -f $file ]]; then 84 | echo "$file" 85 | else 86 | echo "no such file $file" 87 | return 1; 88 | fi 89 | } 90 | 91 | readFile() { 92 | local file="$1" 93 | while read line;do 94 | echo "$line" 95 | done <${file} 96 | } 97 | 98 | removeSpace() { 99 | local line="$1" 100 | line=$(sed -n 's/\s\+//pg' <<<"${line}") 101 | echo $line 102 | } 103 | 104 | finalFunc() { 105 | local succ="$1" 106 | local error="$2" 107 | 108 | if [[ -z ${succ} ]];then 109 | echo "$error has occured" 110 | else 111 | echo "${succ}" >> newfile.txt 112 | fi 113 | 114 | } 115 | parallel "mainFunc /var/log/syslog" "${fArray[*]}" finalFunc 116 | 117 | 118 | ``` 119 | 120 | 121 | ***killJob*** This command kills a job returned by any of the asynchronous function. It takes 2 arguments. The first argument is the job id , while the second argument is the signal to send to the job. If the signal is not specified, the job with the specified id is sent a SIGTERM signal 122 | 123 | 124 | ## license 125 | 126 | This program is free software; you can redistribute it andor modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 127 | -------------------------------------------------------------------------------- /async.bash: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | 4 | # This script implements 3 asynchronous function 5 | # setTimeout 6 | # setInterval 7 | # async 8 | # killJob function is not asynchronous 9 | 10 | # check the README.md for information on how to use this script 11 | 12 | declare -a JOB_IDS 13 | 14 | declare -i JOBS=1; 15 | 16 | #source ./functions.sh; 17 | 18 | setTimeout() { 19 | 20 | local command="$1" 21 | local after="$2" 22 | 23 | read -d " " comm <<<"${command}" 24 | 25 | #declare -F $comm &>/dev/null 26 | 27 | local _isDef=$(type -t ${comm}) 28 | 29 | if [[ -z "${_isDef}" ]];then 30 | printf "%s\n" "\"${command}\" is not of type { function, command} " 31 | 32 | return 1; 33 | fi 34 | 35 | 36 | [[ ! $after =~ ^[[:digit:]]+$ ]] && { 37 | printf "%s\n" "require an integer as the second argument but got \"$after\" " 38 | 39 | return 1; 40 | } 41 | 42 | { 43 | sleep ${after} 44 | $command 45 | } & 46 | 47 | JOB_IDS+=( "${JOBS} ${command}" ) 48 | 49 | 50 | 51 | read -d " " -a __kunk__ <<< "${JOB_IDS[$(( ${#JOB_IDS[@]} - 1))]}" 52 | 53 | echo ${__kunk__} 54 | 55 | : $(( JOBS++ )) 56 | 57 | } 58 | 59 | setInterval() { 60 | 61 | local command="$1" 62 | local after="$2" 63 | 64 | read -d " " comm <<<"${command}" 65 | 66 | 67 | 68 | local _isDef=$(type -t ${comm}) 69 | 70 | if [[ -z "${_isDef}" ]];then 71 | printf "%s\n" "\"${command}\" is not of type { function, command} " 72 | 73 | return 1; 74 | fi 75 | 76 | 77 | [[ ! $after =~ ^[[:digit:]]+$ ]] && { 78 | printf "%s\n" "require an integer as the second argument but got \"$after\" " 79 | 80 | return 1; 81 | } 82 | 83 | { 84 | while sleep ${after};do 85 | $command 86 | done 87 | } & 88 | 89 | JOB_IDS+=( "${JOBS} ${command}" ) 90 | 91 | read -d " " -a __kunk__ <<< "${JOB_IDS[$(( ${#JOB_IDS[@]} - 1))]}" 92 | 93 | echo ${__kunk__} 94 | 95 | : $(( JOBS++ )) 96 | } 97 | 98 | killJob() { 99 | 100 | local jobToKill="$1" 101 | local signal="$2" 102 | 103 | signal=${signal^^} 104 | 105 | [[ ! $jobToKill =~ ^[[:digit:]]+$ ]] && { 106 | 107 | printf "%s\n" "\"$jobToKill\" should be an integer "; 108 | 109 | return 1; 110 | } 111 | 112 | 113 | { 114 | [[ -z "$signal" ]] && { 115 | signal="SIGTERM" 116 | } 117 | } || { 118 | # for loop worked better than read line in this case 119 | local __al__signals=$(kill -l); 120 | local isSig=0; 121 | for sig in ${__al__signals};do 122 | 123 | [[ ! $sig =~ ^[[:digit:]]+\)$ ]] && { 124 | [[ $signal == $sig ]] && { 125 | isSig=1; 126 | break; 127 | } 128 | } 129 | done 130 | 131 | (( isSig != 1 )) && { 132 | signal="SIGTERM" 133 | } 134 | 135 | } 136 | 137 | 138 | 139 | for job in ${JOB_IDS[@]};do 140 | 141 | # increment job to 1 since array index starts from 0 142 | read -d " " -a __kunk__ <<< "${JOB_IDS[$job]}" 143 | 144 | (( __kunk__ == jobToKill )) && { 145 | 146 | 147 | read -d " " -a __kunk__ <<< "${JOB_IDS[$job]}" 148 | 149 | kill -${signal} %${__kunk__} 150 | 151 | local status=$? 152 | 153 | (( status != 0 )) && { 154 | 155 | 156 | printf "cannot kill %s %d\n" "${JOB_IDS[$job]}" "${__kunk__}" 157 | 158 | return 1; 159 | } 160 | 161 | printf "%d killed with %s\n" "${__kunk__}" "${signal}" 162 | 163 | return 0; 164 | } 165 | 166 | done 167 | } 168 | 169 | async() { 170 | 171 | local commandToExec="$1" 172 | local resolve="$2" 173 | local reject="$3" 174 | 175 | [[ -z "$commandToExec" ]] || [[ -z "$reject" ]] || [[ -z "$resolve" ]] && { 176 | printf "%s\n" "Insufficient number of arguments"; 177 | return 1; 178 | } 179 | 180 | 181 | 182 | local __temp=( "$commandToExec" "$reject" "$resolve" ) 183 | 184 | 185 | for _c in "${__temp[@]}";do 186 | 187 | 188 | read -d " " comm <<<"${_c}" 189 | 190 | type "${comm}" &>/dev/null 191 | 192 | local status=$? 193 | 194 | (( status != 0 )) && { 195 | printf "\"%s\" is neither a function nor a recognized command\n" "${_c}"; 196 | unset _c 197 | return 1; 198 | } 199 | 200 | done 201 | 202 | unset __temp ; unset _c 203 | 204 | { 205 | 206 | __result=$($commandToExec) 207 | 208 | status=$? 209 | 210 | (( status == 0 )) && { 211 | $resolve "${__result}" 212 | 213 | } || { 214 | $reject "${status}" 215 | } 216 | unset __result 217 | } & 218 | 219 | 220 | 221 | JOB_IDS+=( "${JOBS} ${command}" ) 222 | 223 | read -d " " -a __kunk__ <<< "${JOB_IDS[$(( ${#JOB_IDS[@]} - 1))]}" 224 | 225 | echo ${__kunk__} 226 | 227 | 228 | : $(( JOBS++ )) 229 | 230 | } 231 | 232 | 233 | parallel() { 234 | 235 | #local funcArray="${@:1:$(( ${#@} - 1 ))}" 236 | 237 | local mainFunc="${1}" 238 | local funcArray="${2}" 239 | local finalFunc="${3}" 240 | 241 | local totalArgs=${#@} 242 | 243 | (( totalArgs < 3 )) && { 244 | printf "%s\n" "Insufficient number of argument" 245 | return 1; 246 | } 247 | 248 | read -d " " __cmd <<<"${mainFunc}" 249 | 250 | local _isDef=$(type -t ${__cmd}) 251 | 252 | 253 | [[ -z $_isDef ]] && { 254 | printf "%s\n" "${__cmd} is not of type { function , alias , builtin or file }" 255 | return 1; 256 | } 257 | 258 | [[ "$(type -t $finalFunc)" != "function" ]] && { 259 | printf "%s\n" "${finalFunc} is not of type { function }" 260 | return 1; 261 | } 262 | 263 | for __arr in ${funcArray};do 264 | 265 | local __isfunc=$(type -t ${__arr}) 266 | 267 | [[ $__isfunc != "function" ]] && { 268 | 269 | printf "%s\n" "${__arr} is not of type { function }" 270 | return 1; 271 | 272 | } 273 | 274 | declare __fArray+=( ${__arr} ) 275 | done 276 | 277 | unset __arr 278 | 279 | { 280 | __result=$($mainFunc) 281 | 282 | status=$? 283 | 284 | (( status != 0 )) && { 285 | $finalFunc "" "${__result}" 286 | return $?; 287 | } 288 | 289 | local _t=0 290 | 291 | for __async in "${__fArray[@]}";do 292 | 293 | __result=$(${__async} "${__result}") 294 | 295 | status=$? 296 | 297 | (( status != 0 )) && { 298 | $finalFunc "" "${__result}" 299 | 300 | # _t has no use here, since we will be returning from this function 301 | # it was only use for clarity 302 | 303 | _t=1 304 | return $? 305 | } 306 | 307 | done 308 | 309 | (( _t == 0 )) && { 310 | $finalFunc "${__result}" "" 311 | } 312 | } & 313 | 314 | JOB_IDS+=( "${JOBS} ${command}" ) 315 | 316 | read -d " " -a __kunk__ <<< "${JOB_IDS[$(( ${#JOB_IDS[@]} - 1))]}" 317 | 318 | echo ${__kunk__} 319 | 320 | 321 | : $(( JOBS++ )) 322 | } 323 | -------------------------------------------------------------------------------- /test/async.bash: -------------------------------------------------------------------------------- 1 | source ../async.sh 2 | success() { 3 | local _content="$1" 4 | 5 | echo ${_content} > my_file.html 6 | } 7 | 8 | error() { 9 | local _err="$1" 10 | 11 | echo ${_err} 12 | } 13 | 14 | async "curl -s http://google.com/" success error 15 | async "curl -s http://googlejajajaj.com/" success error 16 | -------------------------------------------------------------------------------- /test/my_file.html: -------------------------------------------------------------------------------- 1 |