我要檢查,如果一個變量通過正則表達式[a-z\-]+
,我的嘗試是如何使用正則表達式和否定一個類內部的if else
NAME_A="something"
NAME_B="Something_Wrong"
VALID_CHARACTERS="[a-z\-]+"
if [[ ! $NAME_A =~ $VALID_CHARACTERS ]]; then
echo "NAME_A failed";
exit 1
else
echo "NAME_A passed"
fi
if [[ ! $NAME_B =~ $VALID_CHARACTERS ]]; then
echo "NAME_B failed";
exit 1
else
echo "NAME_B passed"
fi
但如果條件總是如此。
(online bash fiddle)
謝謝,這使的感覺。 – Rikard