Manuel
Manuel

Reputation: 29

Linux Bash ask if user want to execute input

Hello im trying to create a script which creates an Certificat request. This ist what i got so far:

#/!usr/bin/env bash


DOMAIN=$1
ALTNAME=$2
ALTNAME2=$3


if [ -z "$DOMAIN" ]; then
  echo -n 'Enter root domain (no www): '
  read input_d
  DOMAIN=$input_d
fi


if [ -z "$ALTNAME" ]; then
  echo -n 'Enter first AltName: '
  read input_d
  ALTNAME=$input_d
fi

if [ -z "$ALTNAME1" ]; then
  echo -n 'Enter second AltName: '
  read input_d
  ALTNAME1=$input_d
fi


[ -d certs ] || mkdir certs

# Easiest to generate conf file for each
# certificate creation process
OpenSSLConf="$DOMAIN"-openssl.cnf

cat >"$OpenSSLConf" <<EOL
[req]
req_extensions = v3_req


distinguished_name = req_distinguished_name


[ req_distinguished_name ]

countryName                     = Country
countryName_default             = example
stateOrProvinceName             = State
stateOrProvinceName_default     = example
localityName                    = City
localityName_default            = example
0.organizationName              = Organization Name (eg, company)
0.organizationName_default      = example
organizationalUnitName          = Organizational Unit Name (eg, section)
organizationalUnitName_default  = example
commonName                      = Common Name
commonName_default              = $DOMAIN
emailAddress                    = Email Address
emailAddress_max                = 64
emailAddress_default            = example


[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = $ALTNAME
DNS.2 = $ALTNAME1
EOL

The script works but i need to do some changes and i have no clue how this could work, thats why im asking this. The script asks the user for 2 Alternative Names but the problem is that he has to enter exactly 2 names not more and not less. How could it be possieble to ask the user wheter he wants to add Alternative names and if he wants to add them how many he wants to add.

I hope someone can help me with this cause i dont know how to google for that.

Upvotes: 1

Views: 68

Answers (1)

Socowi
Socowi

Reputation: 27370

Use an array to store the user inputs. Let the user enter inputs until a keyword is entered, for instance END or just the empty string:

alts=()
end=""
while read -p 'Enter an (additional) alternative or "$end" to exit: ' input &&
      [ "$input" != "$end" ]
do
    alts+=("$input")
done
echo "There are ${#alts[@]} alternatives".
echo "The alternatives are:"
printf %s\\n "${alts[@]}"

As an alternative to the keyword, you can always press Ctrl+D to exit read with non-zero status. In this case, you don't need the && [ ... != ... ] part.

To generate something of the form …

DNS.1 = first input
DNS.2 = second input
⋮
DNS.n = last input

… use a loop. You can even embed the loop inside $() inside a here doc.

cat <<END
[alt_names]
$(i=1
for alt in "${alts[@]}"; do
    echo "DNS.$((i++)) = $alt"
done)
END

Upvotes: 1

Related Questions