Skip to main content
English
Source Link
fra-san
  • 10.9k
  • 2
  • 27
  • 45

Since you are using Bash, you may leveragemake use of associative arrays (introduced in bash-4.0-alpha1).

You can define the script:

#!/bin/bash

declare -A urls

urls[dev]=dev.example.com
urls[tst]=test.example.com

request="$(curl ...  https://"${urls["$1"]}"/foo/bar ...)"

And invoke it as:

script dev

The first positional parameter ($1) will be used as the key for retrieving a URL from the urls array.

As a (more user friendly) variation, as suggested by DopeGhoti, script may be:

#!/bin/bash

declare -A urls

urls[dev]=dev.example.com
urls[tst]=test.example.com

select opt in "${!urls[@]}"; do
    [ ! -z "$opt" ] && break
done

request="$(curl ... https://"${urls["$opt"]}"/foo/bar ...)"

Which is meant to be invoked as:

script

The select command presents the user with the list of the keys defined for the urls array and ensures that a valid key is chosen.

Finally, as a way to separate concerns, you may store your URLs in a plain text file, here named urlfile, formatted as:

dev dev.example.com
tst test.example.com

and load its content into the script's urls array with a read loop:

#!/bin/bash

declare -A urls

while read -r label url; do
    urls["$label"]="$url"
done <urlfile

select opt in "${!urls[@]}"; do
    [ ! -z "$opt" ] && break
done

request="$(curl ... https://"${urls["$opt"]}"/foo/bar ...)"

Note: even if not necessary, I chose the $(...) command substitution syntax (instead of ` `) because it is generally more versatile.


1 With this commit from 2011. Bash 4.0+ is now widely available.

Since you are using Bash, you may leverage associative arrays (introduced in bash-4.0-alpha1).

You can define the script:

#!/bin/bash

declare -A urls

urls[dev]=dev.example.com
urls[tst]=test.example.com

request="$(curl ...  https://"${urls["$1"]}"/foo/bar ...)"

And invoke it as:

script dev

The first positional parameter ($1) will be used as the key for retrieving a URL from the urls array.

As a (more user friendly) variation, as suggested by DopeGhoti, script may be:

#!/bin/bash

declare -A urls

urls[dev]=dev.example.com
urls[tst]=test.example.com

select opt in "${!urls[@]}"; do
    [ ! -z "$opt" ] && break
done

request="$(curl ... https://"${urls["$opt"]}"/foo/bar ...)"

Which is meant to be invoked as:

script

The select command presents the user with the list of the keys defined for the urls array and ensures that a valid key is chosen.

Finally, as a way to separate concerns, you may store your URLs in a plain text file, here named urlfile, formatted as:

dev dev.example.com
tst test.example.com

and load its content into the script's urls array with a read loop:

#!/bin/bash

declare -A urls

while read -r label url; do
    urls["$label"]="$url"
done <urlfile

select opt in "${!urls[@]}"; do
    [ ! -z "$opt" ] && break
done

request="$(curl ... https://"${urls["$opt"]}"/foo/bar ...)"

Note: even if not necessary, I chose the $(...) command substitution syntax (instead of ` `) because it is generally more versatile.


1 With this commit from 2011. Bash 4.0+ is now widely available.

Since you are using Bash, you may make use of associative arrays (introduced in bash-4.0-alpha1).

You can define the script:

#!/bin/bash

declare -A urls

urls[dev]=dev.example.com
urls[tst]=test.example.com

request="$(curl ...  https://"${urls["$1"]}"/foo/bar ...)"

And invoke it as:

script dev

The first positional parameter ($1) will be used as the key for retrieving a URL from the urls array.

As a (more user friendly) variation, as suggested by DopeGhoti, script may be:

#!/bin/bash

declare -A urls

urls[dev]=dev.example.com
urls[tst]=test.example.com

select opt in "${!urls[@]}"; do
    [ ! -z "$opt" ] && break
done

request="$(curl ... https://"${urls["$opt"]}"/foo/bar ...)"

Which is meant to be invoked as:

script

The select command presents the user with the list of the keys defined for the urls array and ensures that a valid key is chosen.

Finally, as a way to separate concerns, you may store your URLs in a plain text file, here named urlfile, formatted as:

dev dev.example.com
tst test.example.com

and load its content into the script's urls array with a read loop:

#!/bin/bash

declare -A urls

while read -r label url; do
    urls["$label"]="$url"
done <urlfile

select opt in "${!urls[@]}"; do
    [ ! -z "$opt" ] && break
done

request="$(curl ... https://"${urls["$opt"]}"/foo/bar ...)"

Note: even if not necessary, I chose the $(...) command substitution syntax (instead of ` `) because it is generally more versatile.


1 With this commit from 2011. Bash 4.0+ is now widely available.

Removed confusing IFS and comment from last code snippet
Source Link
fra-san
  • 10.9k
  • 2
  • 27
  • 45

Since you are using Bash, you may leverage associative arrays (introduced in bash-4.0-alpha1).

You can define the script:

#!/bin/bash

declare -A urls

urls[dev]=dev.example.com
urls[tst]=test.example.com

request="$(curl ...  https://"${urls["$1"]}"/foo/bar ...)"

And invoke it as:

script dev

The first positional parameter ($1) will be used as the key for retrieving a URL from the urls array.

As a (more user friendly) variation, as suggested by DopeGhoti, script may be:

#!/bin/bash

declare -A urls

urls[dev]=dev.example.com
urls[tst]=test.example.com

select opt in "${!urls[@]}"; do
    [ ! -z "$opt" ] && break
done

request="$(curl ... https://"${urls["$opt"]}"/foo/bar ...)"

Which is meant to be invoked as:

script

The select command presents the user with the list of the keys defined for the urls array and ensures that a valid key is chosen.

Finally, as a way to separate concerns, you may store your URLs in a plain text file, here named urlfile, formatted as:

dev dev.example.com
tst test.example.com

and load its content into the script's urls array with a read loop:

#!/bin/bash

declare -A urls

while IFS=' ' read -r label url; do    # Only split lines on whitespace
    urls["$label"]="$url"
done <urlfile

select opt in "${!urls[@]}"; do
    [ ! -z "$opt" ] && break
done

request="$(curl ... https://"${urls["$opt"]}"/foo/bar ...)"

Note: even if not necessary, I chose the $(...) command substitution syntax (instead of ` `) because it is generally more versatile.


1 With this commit from 2011. Bash 4.0+ is now widely available.

Since you are using Bash, you may leverage associative arrays (introduced in bash-4.0-alpha1).

You can define the script:

#!/bin/bash

declare -A urls

urls[dev]=dev.example.com
urls[tst]=test.example.com

request="$(curl ...  https://"${urls["$1"]}"/foo/bar ...)"

And invoke it as:

script dev

The first positional parameter ($1) will be used as the key for retrieving a URL from the urls array.

As a (more user friendly) variation, as suggested by DopeGhoti, script may be:

#!/bin/bash

declare -A urls

urls[dev]=dev.example.com
urls[tst]=test.example.com

select opt in "${!urls[@]}"; do
    [ ! -z "$opt" ] && break
done

request="$(curl ... https://"${urls["$opt"]}"/foo/bar ...)"

Which is meant to be invoked as:

script

The select command presents the user with the list of the keys defined for the urls array and ensures that a valid key is chosen.

Finally, as a way to separate concerns, you may store your URLs in a plain text file, here named urlfile, formatted as:

dev dev.example.com
tst test.example.com

and load its content into the script's urls array with a read loop:

#!/bin/bash

declare -A urls

while IFS=' ' read label url; do    # Only split lines on whitespace
    urls["$label"]="$url"
done <urlfile

select opt in "${!urls[@]}"; do
    [ ! -z "$opt" ] && break
done

request="$(curl ... https://"${urls["$opt"]}"/foo/bar ...)"

Note: even if not necessary, I chose the $(...) command substitution syntax (instead of ` `) because it is generally more versatile.


1 With this commit from 2011. Bash 4.0+ is now widely available.

Since you are using Bash, you may leverage associative arrays (introduced in bash-4.0-alpha1).

You can define the script:

#!/bin/bash

declare -A urls

urls[dev]=dev.example.com
urls[tst]=test.example.com

request="$(curl ...  https://"${urls["$1"]}"/foo/bar ...)"

And invoke it as:

script dev

The first positional parameter ($1) will be used as the key for retrieving a URL from the urls array.

As a (more user friendly) variation, as suggested by DopeGhoti, script may be:

#!/bin/bash

declare -A urls

urls[dev]=dev.example.com
urls[tst]=test.example.com

select opt in "${!urls[@]}"; do
    [ ! -z "$opt" ] && break
done

request="$(curl ... https://"${urls["$opt"]}"/foo/bar ...)"

Which is meant to be invoked as:

script

The select command presents the user with the list of the keys defined for the urls array and ensures that a valid key is chosen.

Finally, as a way to separate concerns, you may store your URLs in a plain text file, here named urlfile, formatted as:

dev dev.example.com
tst test.example.com

and load its content into the script's urls array with a read loop:

#!/bin/bash

declare -A urls

while read -r label url; do
    urls["$label"]="$url"
done <urlfile

select opt in "${!urls[@]}"; do
    [ ! -z "$opt" ] && break
done

request="$(curl ... https://"${urls["$opt"]}"/foo/bar ...)"

Note: even if not necessary, I chose the $(...) command substitution syntax (instead of ` `) because it is generally more versatile.


1 With this commit from 2011. Bash 4.0+ is now widely available.

Minor addition
Source Link
fra-san
  • 10.9k
  • 2
  • 27
  • 45

Since you are using Bash, you may leverage associative arrays (introduced in bash-4.0-alpha1).

You can define the script:

#!/bin/bash

declare -A urls

urls[dev]=dev.example.com
urls[tst]=test.example.com

request="$(curl ...  https://"${urls["$1"]}"/foo/bar ...)"

And invoke it as:

script dev

The first positional parameter ($1) will be used as the key for retrieving a URL from the urls array.

As a (more user friendly) variation, as suggested by DopeGhoti, script may be:

#!/bin/bash

declare -A urls

urls[dev]=dev.example.com
urls[tst]=test.example.com

select opt in "${!urls[@]}"; do
    [ ! -z "$opt" ] && break
done

request="$(curl ... https://"${urls["$opt"]}"/foo/bar ...)"

Which is meant to be invoked as:

script

The select command presents the user with the list of the keys defined for the urls array and ensures that a valid key is chosen.

Finally, as a way to separate concerns, you may store your URLs in a plain text file, here named urlfile, formatted as:

dev dev.example.com
tst test.example.com

and load its content into the script's urls array with a read loop:

#!/bin/bash

declare -A urls

while IFS=' ' read label url; do    # Only split lines on whitespace
    urls["$label"]="$url"
done <urlfile

select opt in "${!urls[@]}"; do
    [ ! -z "$opt" ] && break
done

request="$(curl ... https://"${urls["$opt"]}"/foo/bar ...)"

Note: even if not necessary, I chose the $(...) command substitution syntax (instead of ` `) because it is generally more versatile.

 

1 With this commit from 2011. Bash 4.0+ is now widely available.

Since you are using Bash, you may leverage associative arrays (introduced in bash-4.0-alpha1).

You can define the script:

#!/bin/bash

declare -A urls

urls[dev]=dev.example.com
urls[tst]=test.example.com

request="$(curl ...  https://"${urls["$1"]}"/foo/bar ...)"

And invoke it as:

script dev

The first positional parameter ($1) will be used as the key for retrieving a URL from the urls array.

As a (more user friendly) variation, as suggested by DopeGhoti, script may be:

#!/bin/bash

declare -A urls

urls[dev]=dev.example.com
urls[tst]=test.example.com

select opt in "${!urls[@]}"; do
    [ ! -z "$opt" ] && break
done

request="$(curl ... https://"${urls["$opt"]}"/foo/bar ...)"

Which is meant to be invoked as:

script

The select command presents the user with the list of the keys defined for the urls array and ensures that a valid key is chosen.

Finally, as a way to separate concerns, you may store your URLs in a plain text file, here named urlfile, formatted as:

dev dev.example.com
tst test.example.com

and load its content into the script's urls array with a read loop:

#!/bin/bash

declare -A urls

while IFS=' ' read label url; do    # Only split lines on whitespace
    urls["$label"]="$url"
done <urlfile

select opt in "${!urls[@]}"; do
    [ ! -z "$opt" ] && break
done

request="$(curl ... https://"${urls["$opt"]}"/foo/bar ...)"

Note: even if not necessary, I chose the $(...) command substitution syntax (instead of ` `) because it is generally more versatile.

1 With this commit. Bash 4.0+ is now widely available.

Since you are using Bash, you may leverage associative arrays (introduced in bash-4.0-alpha1).

You can define the script:

#!/bin/bash

declare -A urls

urls[dev]=dev.example.com
urls[tst]=test.example.com

request="$(curl ...  https://"${urls["$1"]}"/foo/bar ...)"

And invoke it as:

script dev

The first positional parameter ($1) will be used as the key for retrieving a URL from the urls array.

As a (more user friendly) variation, as suggested by DopeGhoti, script may be:

#!/bin/bash

declare -A urls

urls[dev]=dev.example.com
urls[tst]=test.example.com

select opt in "${!urls[@]}"; do
    [ ! -z "$opt" ] && break
done

request="$(curl ... https://"${urls["$opt"]}"/foo/bar ...)"

Which is meant to be invoked as:

script

The select command presents the user with the list of the keys defined for the urls array and ensures that a valid key is chosen.

Finally, as a way to separate concerns, you may store your URLs in a plain text file, here named urlfile, formatted as:

dev dev.example.com
tst test.example.com

and load its content into the script's urls array with a read loop:

#!/bin/bash

declare -A urls

while IFS=' ' read label url; do    # Only split lines on whitespace
    urls["$label"]="$url"
done <urlfile

select opt in "${!urls[@]}"; do
    [ ! -z "$opt" ] && break
done

request="$(curl ... https://"${urls["$opt"]}"/foo/bar ...)"

Note: even if not necessary, I chose the $(...) command substitution syntax (instead of ` `) because it is generally more versatile.

 

1 With this commit from 2011. Bash 4.0+ is now widely available.

Added reference to Bash version
Source Link
fra-san
  • 10.9k
  • 2
  • 27
  • 45
Loading
Added variation with text file for URLs
Source Link
fra-san
  • 10.9k
  • 2
  • 27
  • 45
Loading
Minor rewording
Source Link
fra-san
  • 10.9k
  • 2
  • 27
  • 45
Loading
Source Link
fra-san
  • 10.9k
  • 2
  • 27
  • 45
Loading