Skip to main content
added 196 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60
$ cat script.sh
#!/usr/bin/env bash

while getopts cnBba opt; do
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        B)  gotB=1;;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

# mapfile and it's associated loop would go here

(( gotB )) && options+=(BMC)

for opt in "${options[@]}"; do
    echo "option is $opt"
done
$ cat script.sh
#!/usr/bin/env bash

declare -A opts

while getopts cnBba opt; do
    opts["$opt"]=''
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

# mapfile and it's associated loop would go here

[[ -v opts[B] ]] && options+=(BMC)

for opt in "${options[@]}"; do
    echo "option is $opt"
done
$ cat script.sh
#!/usr/bin/env bash

while getopts cnBba opt; do
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        B)  deferred+=(BMC);;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

# mapfile and it's associated loop would go here

for opt in "${deferred[@]}"; do
    options+=("$opt")
done

for opt in "${options[@]}"; do
    echo "option is $opt"
done

Add the mapfile and associated loop between the while getopts and where I add B to options[] in any of the above scripts as mentioned in the comments.

$ cat script.sh
#!/usr/bin/env bash

while getopts cnBba opt; do
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        B)  gotB=1;;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

(( gotB )) && options+=(BMC)

for opt in "${options[@]}"; do
    echo "option is $opt"
done
$ cat script.sh
#!/usr/bin/env bash

declare -A opts

while getopts cnBba opt; do
    opts["$opt"]=''
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

[[ -v opts[B] ]] && options+=(BMC)

for opt in "${options[@]}"; do
    echo "option is $opt"
done
$ cat script.sh
#!/usr/bin/env bash

while getopts cnBba opt; do
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        B)  deferred+=(BMC);;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

for opt in "${deferred[@]}"; do
    options+=("$opt")
done

for opt in "${options[@]}"; do
    echo "option is $opt"
done

Add the mapfile and associated loop between the while getopts and where I add B to options[] in any of the above scripts.

$ cat script.sh
#!/usr/bin/env bash

while getopts cnBba opt; do
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        B)  gotB=1;;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

# mapfile and it's associated loop would go here

(( gotB )) && options+=(BMC)

for opt in "${options[@]}"; do
    echo "option is $opt"
done
$ cat script.sh
#!/usr/bin/env bash

declare -A opts

while getopts cnBba opt; do
    opts["$opt"]=''
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

# mapfile and it's associated loop would go here

[[ -v opts[B] ]] && options+=(BMC)

for opt in "${options[@]}"; do
    echo "option is $opt"
done
$ cat script.sh
#!/usr/bin/env bash

while getopts cnBba opt; do
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        B)  deferred+=(BMC);;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

# mapfile and it's associated loop would go here

for opt in "${deferred[@]}"; do
    options+=("$opt")
done

for opt in "${options[@]}"; do
    echo "option is $opt"
done

Add the mapfile and associated loop between the while getopts and where I add B to options[] in any of the above scripts as mentioned in the comments.

added 567 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60

Just remember you got B when looping through the options then populate options[] later, e.g. using a scalar variable:

$ cat script.sh
#!/usr/bin/env bash

while getopts cnBba opt; do
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        B)  gotB=1;;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

(( gotB )) && options+=(BMC)

for opt in "${options[@]}"; do
    echo "option is $opt"
done

$ ./script.sh -Bbcn
option is BIOS
option is CPLD
option is NIC
option is BMC

$ ./script.sh -bcn
option is BIOS
option is CPLD
option is NIC

or if your bash version supports -v you could just keep a separate array of options as you getopts them and later test for B being an index in that:

$ cat script.sh
#!/usr/bin/env bash

declare -A opts

while getopts cnBba opt; do
    opts["$opt"]=''
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

[[ -v opts[B] ]] && options+=(BMC)

for opt in "${options[@]}"; do
    echo "option is $opt"
done

or save any options you want to add on at the end in a different array first then populate options[] from that array later:

$ cat script.sh
#!/usr/bin/env bash

while getopts cnBba opt; do
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        B)  deferred+=(BMC);;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

for opt in "${deferred[@]}"; do
    options+=("$opt")
done

for opt in "${options[@]}"; do
    echo "option is $opt"
done

Add the mapfile and associated loop between the while getopts and where I add B to options[] in any of the above scripts.

Just remember you got B when looping through the options then populate options[] later, e.g. using a scalar variable:

$ cat script.sh
#!/usr/bin/env bash

while getopts cnBba opt; do
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        B)  gotB=1;;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

(( gotB )) && options+=(BMC)

for opt in "${options[@]}"; do
    echo "option is $opt"
done

$ ./script.sh -Bbcn
option is BIOS
option is CPLD
option is NIC
option is BMC

$ ./script.sh -bcn
option is BIOS
option is CPLD
option is NIC

or if your bash version supports -v you could just keep a separate array of options and test for B being an index in that:

$ cat script.sh
#!/usr/bin/env bash

declare -A opts

while getopts cnBba opt; do
    opts["$opt"]=''
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

[[ -v opts[B] ]] && options+=(BMC)

for opt in "${options[@]}"; do
    echo "option is $opt"
done

or save any options you want to add on at the end in a different array first:

$ cat script.sh
#!/usr/bin/env bash

while getopts cnBba opt; do
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        B)  deferred+=(BMC);;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

for opt in "${deferred[@]}"; do
    options+=("$opt")
done

for opt in "${options[@]}"; do
    echo "option is $opt"
done

Just remember you got B when looping through the options then populate options[] later, e.g. using a scalar variable:

$ cat script.sh
#!/usr/bin/env bash

while getopts cnBba opt; do
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        B)  gotB=1;;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

(( gotB )) && options+=(BMC)

for opt in "${options[@]}"; do
    echo "option is $opt"
done

$ ./script.sh -Bbcn
option is BIOS
option is CPLD
option is NIC
option is BMC

$ ./script.sh -bcn
option is BIOS
option is CPLD
option is NIC

or if your bash version supports -v you could keep a separate array of options as you getopts them and later test for B being an index in that:

$ cat script.sh
#!/usr/bin/env bash

declare -A opts

while getopts cnBba opt; do
    opts["$opt"]=''
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

[[ -v opts[B] ]] && options+=(BMC)

for opt in "${options[@]}"; do
    echo "option is $opt"
done

or save any options you want to add on at the end in a different array first then populate options[] from that array later:

$ cat script.sh
#!/usr/bin/env bash

while getopts cnBba opt; do
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        B)  deferred+=(BMC);;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

for opt in "${deferred[@]}"; do
    options+=("$opt")
done

for opt in "${options[@]}"; do
    echo "option is $opt"
done

Add the mapfile and associated loop between the while getopts and where I add B to options[] in any of the above scripts.

added 567 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60

Just remember you got B when looping through the options then populate options[] later, e.g. using a scalar variable:

$ cat tstscript.sh
#!/usr/bin/env bash

while getopts cnBba opt; do
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        B)  gotB=true;;gotB=1;;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

[[(( gotB ]])) && options+=(BMC)

for opt in "${options[@]}"; do
    echo "option is $opt"
done

$ ./script.sh -Bbcn
option is BIOS
option is CPLD
option is NIC
option is BMC

$ ./script.sh -bcn
option is BIOS
option is CPLD
option is NIC

or if your bash version supports -v you could just keep a separate array of options and test for B being an index in that:

$ cat script.sh
#!/usr/bin/env bash

declare -A opts

while getopts cnBba opt; do
    opts["$opt"]=''
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

[[ -v opts[B] ]] && options+=(BMC)

for opt in "${options[@]}"; do
    echo "option is $opt"
done

or save any options you want to add on at the end in a different array first:

$ cat script.sh
#!/usr/bin/env bash

while getopts cnBba opt; do
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        B)  deferred+=(BMC);;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

for opt in "${deferred[@]}"; do
    options+=("$opt")
done

for opt in "${options[@]}"; do
    echo "option is $opt"
done

Just remember you got B when looping through the options then populate options[] later:

$ cat tst.sh
#!/usr/bin/env bash

while getopts cnBba opt; do
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        B)  gotB=true;;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

[[ gotB ]] && options+=(BMC)

for opt in "${options[@]}"; do
    echo "option is $opt"
done

$ ./script.sh -Bbcn
option is BIOS
option is CPLD
option is NIC
option is BMC

$ ./script.sh -bcn
option is BIOS
option is CPLD
option is NIC

or if your bash version supports -v you could just keep a separate array of options and test for B being an index in that:

$ cat script.sh
#!/usr/bin/env bash

declare -A opts

while getopts cnBba opt; do
    opts["$opt"]=''
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

[[ -v opts[B] ]] && options+=(BMC)

for opt in "${options[@]}"; do
    echo "option is $opt"
done

Just remember you got B when looping through the options then populate options[] later, e.g. using a scalar variable:

$ cat script.sh
#!/usr/bin/env bash

while getopts cnBba opt; do
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        B)  gotB=1;;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

(( gotB )) && options+=(BMC)

for opt in "${options[@]}"; do
    echo "option is $opt"
done

$ ./script.sh -Bbcn
option is BIOS
option is CPLD
option is NIC
option is BMC

$ ./script.sh -bcn
option is BIOS
option is CPLD
option is NIC

or if your bash version supports -v you could just keep a separate array of options and test for B being an index in that:

$ cat script.sh
#!/usr/bin/env bash

declare -A opts

while getopts cnBba opt; do
    opts["$opt"]=''
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

[[ -v opts[B] ]] && options+=(BMC)

for opt in "${options[@]}"; do
    echo "option is $opt"
done

or save any options you want to add on at the end in a different array first:

$ cat script.sh
#!/usr/bin/env bash

while getopts cnBba opt; do
    case $opt in
        c)  options+=(CPLD);;
        n)  options+=(NIC);;
        B)  deferred+=(BMC);;
        b)  options+=(BIOS);;
        a)  all=true;;
    esac
done

for opt in "${deferred[@]}"; do
    options+=("$opt")
done

for opt in "${options[@]}"; do
    echo "option is $opt"
done
added 567 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60
Loading
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60
Loading