0

I trying to use regex expression for a bash if statement.

if [[ "$@" =~ blah blah ]]; then … ; fi

`result expected:
-a              (not match: as standalone)
-auto           (not match: as standalone)
-lv             (not match: no "a" or "--auto")
-l --verbose    (not match: no "a" or "--auto")
-v --log        (not match: no "a" or "--auto")
-n --make-auto  (not match: no "a" or "--auto")
-g --auto-gen   (not match: no "a" or "--auto")
-a -l           (match: "-a" used with other)
-l -a -v        (match: "-a" used with other)
-l -s -a        (match: "-a" used with other)
-ls -a          (match: "-a" used with other)
-va -s          (match: "-a" used with other)
-lav            (match: a used with other)
-alva           (match: a used with other)
-l --auto       (match: "--auto" used with other)
-l   --auto     (match: "--auto" used with other)
--auto -lv      (match: "--auto" used with other)
--auto --log    (match: "--auto" used with other)
--auto   --log  (match: "--auto" used with other)`

I have made so many test and I think that I don't understand anything to regex I have read many tutorials and links found here but if I understand well there is no AND but only OR

`(^[^--auto]$|[[:space:]]--auto[[:space:]]{0,}|^--auto[[:space:]]{1,}[[:graph:]]{1,})
"--log --auto"              => OK 
"--log --auto --verbose"    => OK
"--log --auto-gen"          => KO match but need to not match no --auto`

`(^[^--auto]$|[[:space:]]--auto[[:space:]]{1,}|^--auto[[:space:]]{1,}[[:graph:]]{1,})
"--log --auto --verbose"    => OK
"--log --verbose --auto"    -> KO`

`(^[^--auto]$|[[:space:]]--auto[[:space:]]{1,}$|^--auto[[:space:]]{1,}[[:graph:]]{1,})`

and so on …

with short param now … match anything even if "-a" is alone !

`((^-([a-z0-9])*a{1,}.*)|([[:space:]]-([a-z0-9])*a{1,}.*))
"-a"                => KO match and do not need
"-l -a"             => OK
"-a -l"             => OK
"-la"               => OK
"-lv"               => OK
"-a -l -v"          => OK
"-v -z    -a"       => OK`

if I add (^-[^a]$) at the front … no effect

`(^-[^a]$)|((^-([a-z0-9])*a{1,}.*)|([[:space:]]-([a-z0-9])*a{1,}.*))`

and if I mix short and long badaboum !

I may have a workaround by testing params separately but i'm trying to test the whole line.

Is it possible to do this in regex and bash ?

Thanks in advance for your advices and help.

moocan
  • 111
  • 2
  • 11
  • Regex might not be the best approach for parsing parameters. Take a look at this question: https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash – p4sh4 Feb 07 '18 at 02:49
  • 2
    Any reason why you don't want to use `getopt`? – JawguyChooser Feb 07 '18 at 03:41
  • Hello, thanks the link. I already use a getopts with long and sort arguments inspired from this link https://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options/7680682#7680682 – moocan Feb 07 '18 at 03:49
  • I need to check (before the getops) if some args are specified. if -a or --auto is specified all other args are removed from $@ to keep all default options values. in the same way if --dry-run is specified --write-change is deleted, ... – moocan Feb 07 '18 at 03:52
  • 2
    @moocan You'd be much better off running through everything with `getopts`, setting a flag if `-a` or `--auto` is one of the options, and then after parsing all options check that flag & if it's set, reset everything else to default. – Gordon Davisson Feb 07 '18 at 05:55
  • You're mixing up figuring out which args are present with what to do if specific combinations of args are present. Keep it simple and use getopts to figure out what args you have and then write code to test for whatever combinations of args you need to do something based on. For example lets say getopts finds `--dry-run` and `-write-change` - just set flags `gotDryRun` and `gotWriteChange` at that point and then after the getopts code block write `if $gotDryRun; then gotWriteChange=""; fi` or similar. Then the rest of your code uses the flags (which hopefully is how it's already structured). – Ed Morton Jul 04 '18 at 11:42

0 Answers0