|
1 | | -## |
| 1 | +# async-bash |
| 2 | + |
| 3 | +**async-bash** is a bash script that implements few asyncrhonous 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 asyncrhonously ) |
| 12 | +4. parallel ( execute bunch of functions asyncrhonously ); |
| 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 | + ecoh ${_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. |
0 commit comments