For a pure Bash solution:
function assertNoSpaces {
if [[ "$1" != "${1/ /}" ]]
then
echo "YOUR ERROR MESSAGE" >&2
exit 1
fi
}
string1="askdjhaaskldjasd"
string2="asjkld askldja skd"
assertNoSpaces "$string1"
assertNoSpaces "$string2"
"${1/ /}" removes any spaces in the input line, and compared to the original line should be exactly the same if there are no spaces.
Pay attention to the quotes around "${1/ /}"- This ensures that leading / trailing spaces are taken into account.
, , - "${1/[ \\.]/}".
. , , .
function assertNoSpaces {
if [[ "$1" =~ '[\. ]' ]]
then
echo "YOUR ERROR MESSAGE" >&2
exit 1
fi
}
=~ . Bash Scripting.
Bash 3, , Bash.
2
:
, "asd \" ... ?
, , , , .
, read, , , escape-, , . .
read str
echo $str
assertNoSpaces "$str"
, -r . . MAN.
read -r str
echo $str
assertNoSpaces "$str"