Bash - saving string to separate variable after custom delimiter -
i'm new bash please forgive me, i'm building bash script can accept user input command can have lot of switches. need write function pulls specific data user provided command.
i can't seem find way pull each word after "-mode" , push array or list of sort.
here example of user input command might like:
/home/custom/function/that/accepts/a/billion/switches random_info random_info2 -worker vendor_schmo -switch_one -anotherone -more_switches -optiontwo -mode foo -mode bar -mode baz -mode bag -mode dat -mode rar
i've tried fiddling awk -f "-mode" '{ print $1 }' , didn't work. advice appreciated!!
here snippet script pulls info:
manualrunmain(){ local command manualrunheaderprint echo echo echo echo linebreakprint read -p "enter command here: " command sleep .25 manualrun "$command" } manualrun(){ manualrunheaderprint jobrunsubheaderprint pullmodenames "$1" } pullmodenames(){ }
you can use rs
variable in awk tell split data separate records when -mode
:
s='/home/custom/function/that/accepts/a/billion/switches random_info random_info2 -worker vendor_schmo -switch_one -anotherone -more_switches -optiontwo -mode foo -mode bar -mode baz -mode bag -mode dat -mode rar' printf '%s' "$s" | awk -v rs="-mode[[:space:]]*" 'nr>1' foo bar baz bag dat rar
to populate array:
mapfile -t arr < <(printf '%s' "$s" | awk -v rs="-mode[[:space:]]*" 'nr>1') # examine array content declare -p arr declare -a arr=([0]="foo" [1]="bar" [2]="baz" [3]="bag" [4]="dat" [5]="rar")
Comments
Post a Comment