Computing desk | ||
---|---|---|
< June 27 | << May | June | Jul >> | June 29 > |
Welcome to the Wikipedia Computing Reference Desk Archives |
---|
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages. |
Has anyone heard of IPv10? I'm guessing it's a joke, that needs to be AFD'd. —scs (talk) 14:17, 28 June 2023 (UTC)
function read_and_verify {
read -p "$1:" tmp1
read -p "$2:" tmp2
if [ "$tmp1" != "$tmp2" ]; then
echo "Values unmatched. Please try again."; return 2
else
read "$1" <<< "$tmp1"
fi
}
read_and_verify domain "Please enter the domain of your web application twice"
read_and_verify dbrootp "Please enter the app DB root password twice"
read_and_verify dbuserp "Please enter the app DB user password twice"
I understand that read gets input and store it in memory and this happens each time the function, which is named read_and_verify
is running.
But what's goging on here wholesomely and is there a simpler way to do store data from the user? 2A10:8012:1:A85B:748D:C39A:189B:D08B (talk) 21:22, 28 June 2023 (UTC)
"$1:"
in the first line of the function body should be "$2:"
, while the next line should be read -p "Again, please:" tmp2
– assuming the -p
argument stands for a prompt to the user. Expanding the first two limes for the first function call then results in
read -p "Please enter the domain of your web application twice1:" tmp1
read -p "Again, please:" tmp2
read
gets input (a string) from a channel and assigns it to a variable. Internally, it uses the variables tmp1
and tmp2
, and if both inputs are the same it then sets the variable that is the first argeument with which it is called – domain
, dbrootp
or dbuserp
– to that common string. I'm not quite sure of the level at which this should be explained, since the code appears fairly self-explanatory to me. --Lambiam 00:40, 29 June 2023 (UTC)read -p email_1
read -p email_2
if [ "$email_1" = "$email_2" ]; then
echo $email_1.
else
echo Mismatch.
fi
read
command, the -p
option writes a prompt. So read -p abc xyz
means more or less the same as echo -n abc; read xyz
. The variable names email_1
and email_2
are confusing; they are meant to contain passwords entered by the user through the keyboard. Above, the effect of echo $email_1.
is that the password the user has just entered twice is echoed back to them (which might surprise and confuse them). I do not see that it is further used --Lambiam 10:17, 29 June 2023 (UTC)
#
# Define a function to obtain a string and check it
# by asking the user to type it twice.
#
# Parameter: the name of the string to return.
#
# Returns: string to stdout
#
# Side effects: uses stderr to contact the user.
#
function read_and_verify {
local tmp1 tmp2
read -p "Please enter the $1:" tmp1
read -p "Again please to check:" tmp2
if [ "$tmp1" != "$tmp2" ]; then
echo "Values unmatched. Please try again." >&2
return 2
fi
echo "$tmp1"
}
#
# Get the domain, dbrootp and dbuserp from the user.
#
domain=$( read_and_verify domain )
dbrootp=$( read_and_verify dbrootp )
dbuserp=$( read_and_verify dbuserp )
#
# For test purposes:
#
echo "domain: $domain"
echo "dbrootp: $dbrootp"
echo "dbuserp: $dbuserp"