Shell 用于检查正确大小写句子的脚本

Shell Script to Check for Proper Case Sentence

谁能帮我用正则表达式检查句子中的所有单词是否大小写正确,即是否以大写字母开头?

#!/bin/sh

SENTENCE="Blah blah blah"

PROPER_CASE_REGEX="\b^AZ*\b"  # <---- Here's where I need help please

if [[ ${SENTENCE} =~ $PROPER_CASE_REGEX ]];
then
    echo "Sentence IS proper case"
elif
then
    echo "Sentence is NOT proper case"
fi

您可以在 BASH(不是在 bourne shell)中使用这个正则表达式来检查句子中是否所有单词都以大写字母开头:

^([A-Z][[:alnum:]]*[[:blank:]]*)+$

测试:

re='^([A-Z][^[:blank:]]*[[:blank:]]*)+$'

sentence="Blah Blah Blah"    
[[ "$sentence" =~ $re ]] && echo "matched" || echo "no match"
matched

sentence="Blah Blah blah"
[[ "$sentence" =~ $re ]] && echo "matched" || echo "no match"
no match

RegEx Demo