这是一个示例 bash ,您可以通过两种方式看到:
1)第一
#!/bin/bash
number=0
echo "Your number is: $number"
IFS=' ' read -t 2 -p "Press space to add one to your number: " input
if [ "$input" -eq $IFS ]; then #OR ==> if [ "$input" -eq ' ' ]; then
let number=number+1
echo $number
else
echo wrong
fi
2)第二:
#!/bin/bash
number=0
echo "Your number is: $number"
read -t 2 -p "Press space to add one to your number: " input
case "$input" in
* * )
let number=$((number+1))
echo $number
;;
*)
echo "no match"
;;
esac
现在的问题:
通过这两种方法,如何检查输入参数是white space
还是null
?
我想在 bash 中检查white space
或null
。
谢谢
你可以尝试这样的事情:
#!/bin/bash
number=0
echo "Your number is: $number"
IFS= read -t 2 -p "Press space to add one to your number: " input
# Check for Space
if [[ $input =~ + ]]; then
echo "space found"
let number=number+1
echo "$number"
# Check if input is NULL
elif [[ -z "$input" ]]; then
echo "input is NULL"
fi
此部分检查输入字符串是否为 NULL
if [ "##"${input}"##" = "####" ]
then
echo "You have input a NULL string"
fi
这部分检查是否输入了一个或多个空白字符
newinput=$(echo ${input} | tr -s " ") # There was a typo on thjis line. should be fixed now
if [ "##"${newinput}"##" = "## ##" ]
then
echo "You have input one (or more) whitespace character(s)"
fi
按照您认为合适的顺序将它们组合在一起,并在 if -- fi 块中设置标志,以便在完成所有比较后进行评估。