On Thu, Nov 9, 2023, 8:24 PM Greg Wooledge <g...@wooledge.org> wrote:
> On Thu, Nov 09, 2023 at 08:09:23PM +0100, Steffen Nurpmeso wrote: > > j() { > > local j= a=${AWK:-awk} > > [ $# -gt 0 ] && j='&& $2 !~ /(^| )('$(echo "$@" | tr ' ' '|')')( > |$)/' > > j=$(jobs -l | $a -F '[][]' '/^[[]/'"$j"'{print "%" $2}{next}') > > echo $j > > } > > Classic code injection vulnerability. > > What are we even parsing? Start with the input: > > unicorn:~$ sleep 5 & > [1] 849028 > unicorn:~$ jobs -l > [1]+ 849028 Running sleep 5 & > > OK, so you wanted to strip the "1" from "[1]" and turn that into "%1", > yes? That shouldn't be terribly hard in pure bash. > > re='^\[([0-9]+)\]' > jobspecs=() > while IFS= read -r line; do > if [[ $line =~ $re ]]; then > jobspecs+=( "%${BASH_REMATCH[1]}" ) > fi > done < <(jobs -l) > sleep 500h & sleep 400h & ~ $ s=7s jid=( ) jpid=( ) ; while IFS=$'\n' j=( $( jobs -l ) ) je=${#j[*]} IFS=$' \t\n' ; do ji=-1 ; while (( ++ji < je )) ; do t=( ${j[ji]} ) jid+=( "${t//[^0-9]}" ) jpid+=( "${t[1]}" ) ; done ; declare -p j{e,{,p}id} ; sleep "$s" ; done declare -- je="2" declare -a jid=([0]="1" [1]="2") declare -a jpid=([0]="21003" [1]="21009") Wrap that in a function with local declarations, etc. > >