使用仅限 bash 的正则表达式语法:
#!/usr/bin/env bash
if [[ $1 =~ ^[[:upper:]]{3}$ ]]; then
echo "The first argument is three upper-case characters"
else
echo "The first argument is _not_ three upper-case characters
fi
Run Code Online (Sandbox Code Playgroud)
...或者,为了与所有 POSIX shell 兼容,可以使用以下case语句:
#!/bin/sh
case $1 in
[[:upper:]][[:upper:]][[:upper:]])
echo "The first argument is three upper-case characters";;
*)
echo "The first argument is _not_ three upper-case characters";;
esac
Run Code Online (Sandbox Code Playgroud)