gpt4 book ai didi

linux - 验证输入的最后 4 个字符的 Bash 脚本

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:17:32 27 4
gpt4 key购买 nike

我想知道是否可以检查输入的最后 4 个字符,如果它与某个字符串不匹配,或者如果输入为空,则告诉用户错误并将它们返回到提示符?

我目前所做的只是检查空白输入,如果那里没有任何内容,它会将用户转储到提示符处,但我无法弄清楚如何做我需要它做的事情。

read -p "Please enter username ( i.e clientname-ssl ): " userInput
if [[ -z "$userInput" ]]; then
printf '%s\n' "No input entered"
exit 1
else
printf "You entered %s " "$userInput"
printf '\n'
ls -l /home/"$userInput"
fi

我需要确保用户始终输入用户名后跟“-ssl”

如有任何帮助,我们将不胜感激。

最佳答案

使用递归函数:

do_ask() {
read -p "Please enter username ( i.e clientname-ssl ): " userInput
if [[ -z "$userInput" ]]; then
printf '%s\n' "No input entered"
exit 1
else
printf "You entered %s " "$userInput"
if [[ $userInput == *-ssl ]]; then
printf '\n'
ls -l "/home/$userInput"
else
echo "ERROR: missing *-ssl"
do_ask
fi
fi
}

do_ask

关于linux - 验证输入的最后 4 个字符的 Bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48799133/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com