如何简化以下脚本。
a="/mappings/file_prod_name.txt";
if [[ ${a,,} =~ 'dev' ]]; then
ENV="dev"
echo "Environment is $ENV "
elif [[ ${a,,} =~ 'test' ]]; then
ENV="test"
echo "Environment is $ENV "
elif [[ ${a,,} =~ 'prod' ]]; then
ENV="prod"
echo "Environment is $ENV "
else echo 'Please enter existing environment file'; fi
答案 0 :(得分:0)
一个简单的代码版本可能如下所示:
a="/mappings/file_prod_name.txt"
ENV=
case ${a,,} in
*dev*) ENV=dev;;
*test*) ENV=test;;
*prod*) ENV=prod;;
esac
if [[ $ENV ]]; then
echo "Environment is $ENV"
else
echo "Please enter an environment file"
fi
删除重复可以简单地通过将内容移动到分配之外来完成。它不需要功能。