Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash - parse argument list in a file

Tags:

linux

bash

shell

I have a script called foo.sh that contains something like

exec myapp -o size=100m -f 

Any idea how to create another script that parses foo.sh and retrieves the value of size? One can assume myapp only appears once in foo.sh, but the order of size argument can appear anywhere in the argument list

Thanks

like image 839
philipdotdev Avatar asked Aug 15 '26 15:08

philipdotdev


1 Answers

With grep in a shell :

$ grep -oP 'myapp.*?size=\K\d+m' foo.sh
100m

With awk in a shell :

$ awk -F'size='  '{sub(/ -f/, "");print $2}' foo.sh
100m

or

$ awk '{print gensub(/.*size=([0-9]+m).*/, "\\1", $0)}' foo.sh
100m

With perl in a shell :

$ perl -lne 'print $1 if /exec.*?size=(\d+m)/' foo.sh
100m

Or using a shell funny trick :

$ declare $(grep -oP "\b\w+=\w+\b" foo.sh)
$ echo $size
100m
like image 171
Gilles Quenot Avatar answered Aug 18 '26 08:08

Gilles Quenot