您好,我正在尝试创建一个创建Certificat请求的脚本。这就是我到目前为止所得到的:
#/!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
该脚本可以工作,但是我需要做一些更改,而且我不知道这怎么工作,这就是为什么我问这个问题。 该脚本要求用户输入2个备用名称,但问题是他必须输入的2个名称不能多于两个。问用户要添加替代名称以及是否要添加替代名称的可能性如何?
我希望有人可以帮我解决这个问题,因为我不知道该如何使用Google。
答案 0 :(得分:1)
使用数组存储用户输入。在输入关键字之前,让用户输入输入,例如END
或仅输入空字符串:
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[@]}"
作为关键字的替代方法,您始终可以按 Ctrl + D 退出具有非零状态的read
。在这种情况下,您不需要&& [ ... != ... ]
部分。
要生成某种形式的……
DNS.1 = first input
DNS.2 = second input
⋮
DNS.n = last input
…使用循环。您甚至可以将循环嵌入到此处文档的$()
中。
cat <<END
[alt_names]
$(i=1
for alt in "${alts[@]}"; do
echo "DNS.$((i++)) = $alt"
done)
END