Skip to main content
added 18 characters in body
Source Link
ilkkachu
  • 148.1k
  • 16
  • 268
  • 441

I don't think your current code is that bad. The only repeating part is read -p, which is just a couple of characters. You can't get rid of the variable names or prompts, anyway.

(Though, there's the thing that someone might prefer command-line arguments instead of scripts that ask stuff interactively, but that's a question of preference.)

Anyway, since I said I didn't particularly like the double-listing of variable names @Jesse_b's associative array requires, here's another alternative:

prompt_user() {
    queries=(
        IMAGE_NAME='Image Name'
        IP_ADDRESS='IP Address'
        PORT_ONE='Port 1'
        PORT_TWO='Port 2'
        CONTAINER_NAME='Container Name'
        NODE_NAME='Node Name'
        HOST_DIRECTORY="Host Directory (Can leave this blank if you're building a new image)"
        REMOTE_DIRECTORY="Remote Directory (Can leave this blank if you're building a new image)"
    )
    echo "Enter details for docker build! If it's a new build, you can leave Host Directory and Remote Directory blank."
    echo "If you've already assigned variables and are running the host you can leave the already filled vars blank if you entered them before"
    echo " "
    echo "Enter details:"
    for query in "${queries[@]}"; do
        read -rp "${query#*=}: " "${query%%=*}"
    done
}

"${query#*=}" and "${query%%=*}" effectively split the string in query on the first equal sign.

I don't think your current code is that bad. The only repeating part is read -p, which is just a couple of characters. You can't get rid of the variable names or prompts, anyway.

(Though, there's the thing that someone might prefer command-line arguments instead of scripts that ask stuff interactively, but that's a question of preference.)

Anyway, since I said I didn't particularly like the double-listing @Jesse_b's associative array requires, here's another alternative:

prompt_user() {
    queries=(
        IMAGE_NAME='Image Name'
        IP_ADDRESS='IP Address'
        PORT_ONE='Port 1'
        PORT_TWO='Port 2'
        CONTAINER_NAME='Container Name'
        NODE_NAME='Node Name'
        HOST_DIRECTORY="Host Directory (Can leave this blank if you're building a new image)"
        REMOTE_DIRECTORY="Remote Directory (Can leave this blank if you're building a new image)"
    )
    echo "Enter details for docker build! If it's a new build, you can leave Host Directory and Remote Directory blank."
    echo "If you've already assigned variables and are running the host you can leave the already filled vars blank if you entered them before"
    echo " "
    echo "Enter details:"
    for query in "${queries[@]}"; do
        read -rp "${query#*=}: " "${query%%=*}"
    done
}

"${query#*=}" and "${query%%=*}" effectively split the string in query on the first equal sign.

I don't think your current code is that bad. The only repeating part is read -p, which is just a couple of characters. You can't get rid of the variable names or prompts, anyway.

(Though, there's the thing that someone might prefer command-line arguments instead of scripts that ask stuff interactively, but that's a question of preference.)

Anyway, since I said I didn't particularly like the double-listing of variable names @Jesse_b's associative array requires, here's another alternative:

prompt_user() {
    queries=(
        IMAGE_NAME='Image Name'
        IP_ADDRESS='IP Address'
        PORT_ONE='Port 1'
        PORT_TWO='Port 2'
        CONTAINER_NAME='Container Name'
        NODE_NAME='Node Name'
        HOST_DIRECTORY="Host Directory (Can leave this blank if you're building a new image)"
        REMOTE_DIRECTORY="Remote Directory (Can leave this blank if you're building a new image)"
    )
    echo "Enter details for docker build! If it's a new build, you can leave Host Directory and Remote Directory blank."
    echo "If you've already assigned variables and are running the host you can leave the already filled vars blank if you entered them before"
    echo " "
    echo "Enter details:"
    for query in "${queries[@]}"; do
        read -rp "${query#*=}: " "${query%%=*}"
    done
}

"${query#*=}" and "${query%%=*}" effectively split the string in query on the first equal sign.

Source Link
ilkkachu
  • 148.1k
  • 16
  • 268
  • 441

I don't think your current code is that bad. The only repeating part is read -p, which is just a couple of characters. You can't get rid of the variable names or prompts, anyway.

(Though, there's the thing that someone might prefer command-line arguments instead of scripts that ask stuff interactively, but that's a question of preference.)

Anyway, since I said I didn't particularly like the double-listing @Jesse_b's associative array requires, here's another alternative:

prompt_user() {
    queries=(
        IMAGE_NAME='Image Name'
        IP_ADDRESS='IP Address'
        PORT_ONE='Port 1'
        PORT_TWO='Port 2'
        CONTAINER_NAME='Container Name'
        NODE_NAME='Node Name'
        HOST_DIRECTORY="Host Directory (Can leave this blank if you're building a new image)"
        REMOTE_DIRECTORY="Remote Directory (Can leave this blank if you're building a new image)"
    )
    echo "Enter details for docker build! If it's a new build, you can leave Host Directory and Remote Directory blank."
    echo "If you've already assigned variables and are running the host you can leave the already filled vars blank if you entered them before"
    echo " "
    echo "Enter details:"
    for query in "${queries[@]}"; do
        read -rp "${query#*=}: " "${query%%=*}"
    done
}

"${query#*=}" and "${query%%=*}" effectively split the string in query on the first equal sign.