bash - why this shell script could not work? -


my script this:

#!/bin/env bash  monitor_sock_raw1=socket,id=hmqmondev,port=55919,host=127.0.0.1,nodelay,server,nowait  msock=${monitor_sock_raw1##,port=} msock=${msock%%,host=}  echo $msock 

i expect '55919', result is:

socket,id=hmqmondev,port=55919,host=127.0.0.1,nodelay,server,nowait 

why , how fix bug?

for simple requirement this, bash supports regex (see bash ere support) approach using ~ operator can use match port string , match digits after it.

#!/bin/env bash  var='monitor_sock_raw1=socket,id=hmqmondev,port=55919,host=127.0.0.1,nodelay'  if [[ $var =~ ^.*port=([[:digit:]]+).*$ ]];      printf "%s\n" "${bash_rematch[1]}" fi 

the captured group regex stored in array bash_rematch first element after index 0 i.e. index 1 contains value of 1st captured group.

regex demo


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -