linux shell 脚本使用函数实现用户自定义输入0或者1输入错误提示用户重新输入给出实现。
以下是一个示例的Linux shell脚本,使用函数实现用户自定义输入0或者1,如果用户输入错误,则提示用户重新输入。
#!/bin/bash
# 定义函数,获取用户输入并验证是否为0或1
get_user_input() {
read -p "请输入0或1: " input
while [[ "$input" != "0" && "$input" != "1" ]]; do
read -p "输入错误,请重新输入0或1: " input
done
echo "您输入的值为: $input"
}
# 调用函数
get_user_input
在上面的脚本中,get_user_input函数用于获取用户输入并验证是否为0或1。如果不是0或1,则提示用户重新输入,直到用户输入正确为止。最后,函数会打印出用户输入的值。
你可以将这个脚本保存为一个文件,例如user_input.sh,然后在终端中运行该脚本:
chmod +x user_input.sh
./user_input.sh
脚本会提示用户输入0或1,并根据用户的输入进行相应的处理
原文地址: https://www.cveoy.top/t/topic/ijuS 著作权归作者所有。请勿转载和采集!