如何检查用户输入的日期格式是否正确
How to check if user input date is correct format
我正在尝试在 shell 中结合命令最后编写一个 if 语句
if 语句应最后检查命令的输出并仅打印用户选择的月份的行。例如用户输入是:Mar January
结果应为:“Mar Good format”
并打印所有最后以 Mar 为月份的命令行。
对于 1 月,它应该说:“January Bad format”并且不打印任何其他内容。
这是目前的代码:
echo "Enter the month name (acording to last command results format):"
read month month1
if [[ $month == [aA-zZ][aA-zZ][aA-zZ] ]]
then
echo "$month Good format"
else
echo "$month Bad format"
fi
它只检查其中一个输入并只打印一个输入。
我不知道如何检查一个 if 语句中的两个字符串并打印两者的结果,如果一个失败而另一个正确。
您可以这样做的一种方法是:
#!/bin/bash
check_month()
{
checkmonth=""
valid_values=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
if [[ " ${valid_values[*]} " =~ " $checkmonth " && "$checkmonth" != '' ]]
then
echo "$checkmonth Good format"
else
echo "$checkmonth Bad format"
fi
}
echo "Enter the month name (acording to last command results format):"
read -r month month1
check_month "$month"
check_month "$month1"
- 函数 check_month 验证参数中收到的月份。
valid_values
是一组好的值。检查月份值是否为三个字符是不够的,因为 abc
不是有效的月份值。
if
检查函数接收到的值是否在数组中的某处。
if
还会验证月份值是否为空,以防万一您的用户仅键入 1 个值(或根本没有值)。
@Nic3500 提供的解决方案的一个变体,它分离空字符串测试,然后使用 [[ ... ]]
测试来匹配传递给函数的月份的子字符串可以是:
#!/bin/bash
check_month()
{
[ -z "" ] && { echo "'' Bad format"; return 1; }
valid_values=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
if [[ ${valid_values[@]} =~ ]]
then
echo " Good format"
else
echo " Bad format"
fi
}
for i in "$@"; do
check_month "$i"
done
当使用 [[ ... ]]
时,变量引用不是必需的,但会影响右侧正则表达式的计算方式。
例子Use/Output
上面修改后的脚本将脚本的command-line参数传递给要检查的函数,例如
bash chkmonths.sh Apr May June Jun July july Jul Dec foo
Apr Good format
May Good format
June Bad format
Jun Good format
July Bad format
july Bad format
Jul Good format
Dec Good format
foo Bad format
@Nic3500 提出的数组作为查找是一个很好的方法。虽然当与 [[ ... ]]
一起使用时,一个简单的 space 分隔字符串也可以使用。
我正在尝试在 shell 中结合命令最后编写一个 if 语句 if 语句应最后检查命令的输出并仅打印用户选择的月份的行。例如用户输入是:Mar January
结果应为:“Mar Good format” 并打印所有最后以 Mar 为月份的命令行。
对于 1 月,它应该说:“January Bad format”并且不打印任何其他内容。
这是目前的代码:
echo "Enter the month name (acording to last command results format):"
read month month1
if [[ $month == [aA-zZ][aA-zZ][aA-zZ] ]]
then
echo "$month Good format"
else
echo "$month Bad format"
fi
它只检查其中一个输入并只打印一个输入。 我不知道如何检查一个 if 语句中的两个字符串并打印两者的结果,如果一个失败而另一个正确。
您可以这样做的一种方法是:
#!/bin/bash
check_month()
{
checkmonth=""
valid_values=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
if [[ " ${valid_values[*]} " =~ " $checkmonth " && "$checkmonth" != '' ]]
then
echo "$checkmonth Good format"
else
echo "$checkmonth Bad format"
fi
}
echo "Enter the month name (acording to last command results format):"
read -r month month1
check_month "$month"
check_month "$month1"
- 函数 check_month 验证参数中收到的月份。
valid_values
是一组好的值。检查月份值是否为三个字符是不够的,因为abc
不是有效的月份值。if
检查函数接收到的值是否在数组中的某处。if
还会验证月份值是否为空,以防万一您的用户仅键入 1 个值(或根本没有值)。
@Nic3500 提供的解决方案的一个变体,它分离空字符串测试,然后使用 [[ ... ]]
测试来匹配传递给函数的月份的子字符串可以是:
#!/bin/bash
check_month()
{
[ -z "" ] && { echo "'' Bad format"; return 1; }
valid_values=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
if [[ ${valid_values[@]} =~ ]]
then
echo " Good format"
else
echo " Bad format"
fi
}
for i in "$@"; do
check_month "$i"
done
当使用 [[ ... ]]
时,变量引用不是必需的,但会影响右侧正则表达式的计算方式。
例子Use/Output
上面修改后的脚本将脚本的command-line参数传递给要检查的函数,例如
bash chkmonths.sh Apr May June Jun July july Jul Dec foo
Apr Good format
May Good format
June Bad format
Jun Good format
July Bad format
july Bad format
Jul Good format
Dec Good format
foo Bad format
@Nic3500 提出的数组作为查找是一个很好的方法。虽然当与 [[ ... ]]
一起使用时,一个简单的 space 分隔字符串也可以使用。