Bash 如何优化以下验证?
我正在编写一个脚本来执行一些验证。为了验证每个参数是否正确存储,我尝试了多次发出ifs。 我想知道这是一个好办法,还是有其他策略来实现这一点。此实现的另一个问题是,当用户未键入所有参数时,我的脚本仅打印第一个参数,如果是:Bash 如何优化以下验证?,bash,Bash,我正在编写一个脚本来执行一些验证。为了验证每个参数是否正确存储,我尝试了多次发出ifs。 我想知道这是一个好办法,还是有其他策略来实现这一点。此实现的另一个问题是,当用户未键入所有参数时,我的脚本仅打印第一个参数,如果是: Missing supplierCode 我的完整代码如下所示。我想感谢任何想法或建议,以便更好地实施 #!/bin/bash function menu () { while (($# >= 1)) do key="$
Missing supplierCode
我的完整代码如下所示。我想感谢任何想法或建议,以便更好地实施
#!/bin/bash
function menu () {
while (($# >= 1))
do
key="$1"
case $key in
--supplierCode)
supplier_code=$2
shift
;;
--vanID)
van_id=$2
shift
;;
--vanIDp)
van_idp=$2
shift
;;
--tgoID)
tgo_id=$2
shift
;;
--tgoIDp)
tgo_idp=$2
shift
;;
--company_name)
company_name=$2
shift
;;
--ediFact)
file_type=E
;;
--webEdi)
file_type=P
;;
--PV)
pv=1
;;
--PT)
pt=1
;;
--help)
exit
;;
esac
shift
done
}
function error () {
#Validations:
if [ -z "$supplier_code" ]
then
echo "Missing supplierCode"
exit 1
fi
if [ -z "$van_id" ]
then
echo "Missing vanID"
exit 1
fi
if [ -z "$van_idp" ]
then
echo "Missing vanIDp"
exit 1
fi
if [ -z "$tgo_id" ]
then
echo "Missing tgoID"
exit 1
fi
if [ -z "$tgo_idp" ]
then
echo "Missing tgoIDp"
exit 1
fi
if [ -z "$company_name" ]
then
echo "Missing company_name"
exit 1
fi
if [ -z "$file_type" ]
then
echo "Missing ediFact or webEdi"
exit 1
fi
if [ "$pv" -eq "0" ] && [ "$pt" -eq "0" ]
then
echo "Error: You have to use at least one of those flags --PV , --PT "
exit 1
fi
}
menu "$@"
error
试试这个:
#!/bin/bash
function menu () {
while (($# >= 1))
do
key="$1"
case $key in
--supplierCode)
supplier_code=$2
shift
;;
--vanID)
van_id=$2
shift
;;
--vanIDp)
van_idp=$2
shift
;;
--tgoID)
tgo_id=$2
shift
;;
--tgoIDp)
tgo_idp=$2
shift
;;
--company_name)
company_name=$2
shift
;;
--ediFact)
file_type=E
;;
--webEdi)
file_type=P
;;
--PV)
pv=1
;;
--PT)
pt=1
;;
--help)
exit
;;
esac
shift
done
}
function error () {
#Validations:
is_valid=1
if [ -z "$supplier_code" ]
then
echo "Missing supplierCode"
is_valid=0
fi
if [ -z "$van_id" ]
then
echo "Missing vanID"
is_valid=0
fi
if [ -z "$van_idp" ]
then
echo "Missing vanIDp"
is_valid=0
fi
if [ -z "$tgo_id" ]
then
echo "Missing tgoID"
is_valid=0
fi
if [ -z "$tgo_idp" ]
then
echo "Missing tgoIDp"
is_valid=0
fi
if [ -z "$company_name" ]
then
echo "Missing company_name"
is_valid=0
fi
if [ -z "$file_type" ]
then
echo "Missing ediFact or webEdi"
is_valid=0
fi
if [ "$pv" -eq "0" ] && [ "$pt" -eq "0" ]
then
echo "Error: You have to use at least one of the flags --PV , --PT "
is_valid=0
fi
if [ "$is_valid" -eq 1 ]
then
printf "Here you could finally exit with code 2 (for user error), which we do.\n"
exit 2
fi
}
menu "$@"
error
这不是我的最佳编辑,但它应该按照您在问题中指出的那样做,即不是在第一个错误出现时提前退出,而是检查所有测试,然后退出(这里的约定是将2作为使用错误代码返回)
PS:我的手指也经常打字echo
,但在大多数情况下,我的心思都集中在printf
上,因为它更通用,更便于携带,可以同时打印“东西”。它有格式字符串,回送选项在不同平台上有所不同。尝试以下方法:
#!/bin/bash
function menu () {
while (($# >= 1))
do
key="$1"
case $key in
--supplierCode)
supplier_code=$2
shift
;;
--vanID)
van_id=$2
shift
;;
--vanIDp)
van_idp=$2
shift
;;
--tgoID)
tgo_id=$2
shift
;;
--tgoIDp)
tgo_idp=$2
shift
;;
--company_name)
company_name=$2
shift
;;
--ediFact)
file_type=E
;;
--webEdi)
file_type=P
;;
--PV)
pv=1
;;
--PT)
pt=1
;;
--help)
exit
;;
esac
shift
done
}
function error () {
#Validations:
is_valid=1
if [ -z "$supplier_code" ]
then
echo "Missing supplierCode"
is_valid=0
fi
if [ -z "$van_id" ]
then
echo "Missing vanID"
is_valid=0
fi
if [ -z "$van_idp" ]
then
echo "Missing vanIDp"
is_valid=0
fi
if [ -z "$tgo_id" ]
then
echo "Missing tgoID"
is_valid=0
fi
if [ -z "$tgo_idp" ]
then
echo "Missing tgoIDp"
is_valid=0
fi
if [ -z "$company_name" ]
then
echo "Missing company_name"
is_valid=0
fi
if [ -z "$file_type" ]
then
echo "Missing ediFact or webEdi"
is_valid=0
fi
if [ "$pv" -eq "0" ] && [ "$pt" -eq "0" ]
then
echo "Error: You have to use at least one of the flags --PV , --PT "
is_valid=0
fi
if [ "$is_valid" -eq 1 ]
then
printf "Here you could finally exit with code 2 (for user error), which we do.\n"
exit 2
fi
}
menu "$@"
error
这不是我的最佳编辑,但它应该按照您在问题中指出的那样做,即不是在第一个错误出现时提前退出,而是检查所有测试,然后退出(这里的约定是将2作为使用错误代码返回)
PS:我的手指也经常打字
echo
,但在大多数情况下,我的心思都集中在printf
上,因为它更通用,更便于携带,可以同时打印“东西”。它有格式字符串,不同平台的回音选项也不同。仍在尝试探索您的问题;-)同时,您应该添加一个p
,这样您就不会忽略error函数中的$vanu idp
。然后,我将在菜单函数中消除echo语句的噪声。当您在线执行此操作时,shellcheck.net之类的bash linter就可以使用您的脚本了。现在是真正的问题。。。啊简单。删除“如果检查”部分中的退出调用。。。请参阅我的答案。您是否使用此函数获取脚本参数?如果是这样,您应该使用getopts,这非常有用。-我决定这么做是因为getopts不允许由两个或更多字符组成的标志,但感谢您的建议。仍在尝试探索您的问题;-)同时,您应该添加一个p
,这样您就不会忽略error函数中的$vanu idp
。然后,我将在菜单函数中消除echo语句的噪声。当您在线执行此操作时,shellcheck.net之类的bash linter就可以使用您的脚本了。现在是真正的问题。。。啊简单。删除“如果检查”部分中的退出调用。。。请参阅我的答案。您是否使用此函数获取脚本参数?如果是这样,您应该使用getopts,这非常有用。-我决定这样做是因为getopts不允许由两个或更多字符组成的标志,但感谢您的建议。感谢这是一个真正有创意的解决方案,我将构造一个由所有缺少的参数组成的字符串,我感谢您的幽默感和支持,致以最诚挚的问候。可能值得记住的是,错误消息应该在echo
或printf
命令之后写入标准错误-重定向&2
。感谢这是一个非常有创意的解决方案,我将构造一个由所有缺少的参数组成的字符串,我感谢您的幽默感和支持,并致以最诚挚的问候。可能值得记住的是,错误消息应该在echo
或printf
命令之后写入标准错误-重定向&2
。