这是一个示例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
。
由于
答案 0 :(得分:1)
您可以尝试这样的事情:
#!/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
答案 1 :(得分:0)
此部分检查输入字符串是否为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块中设置标志,以便在完成所有比较后进行评估。