gpt4 book ai didi

linux - 用于检查电话号码的函数内的 Unix KornShell 脚本 bool 值

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:38:00 25 4
gpt4 key购买 nike

我在 Unix 中写了这个脚本:

function addinfo {
echo "Please enter your LAST Name: "
read last
echo "Please enter your FIRST Name: "
read first
echo "Please enter your PHONE number as XXX-XXX-XXXX: "
read phone
if [ $phone != ???-???-??? ]; then
echo "Please only enter your as XXX-XXX-XXXX"
read phone
else
echo $last + $first + $phone >> asg7phonebook
if
}

我收到与最后一个括号相关的错误代码。

  1. bool 条件可以在 Unix 函数中工作吗?
  2. 如何让我的功能检查用户是否输入了 XXX-XXX-XXXX(其中“X”是他们的电话号码)作为有效号码输入?我想阻止用户输入 23435-34222 作为他们的号码或 1234567890 作为他们的号码。
  3. 我的语法正确吗?

最佳答案

我发现您的方法存在一些问题,

#!/usr/bin/env ksh

addinfo() {
echo "Please enter your LAST Name: "
read last
echo "Please enter your FIRST Name: "
read first
echo "Please enter your PHONE number as XXX-XXX-XXXX: "
read phone
# Using a while loop on the correct pattern check.
while [[ "$phone" != [0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9] ]]
do
echo "Please enter your PHONE number as XXX-XXX-XXXX (only): "
read phone
done
echo $last + $first + $phone >> asg7phonebook
}

addinfo

关于linux - 用于检查电话号码的函数内的 Unix KornShell 脚本 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23507194/

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