验证空字符串,包括空格linux

时间:2015-03-24 16:44:32

标签: bash

DIALOG=${DIALOG=dialog}

tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/test$$
trap "rm -f $tempfile" 0 1 2 5 15                   

$DIALOG --backtitle "Search Forename" --inputbox \
    "Please enter your Forename?" 0 0 2> /tmp/inputbox.tmp.$$

retval=$?

Forename=`cat /tmp/inputbox.tmp.$$`

case $retval in
0) 
    while [[ $Forename = "" ]]; do
        $DIALOG --msgbox "Forename Cannot be left blank" 10 40;
        $DIALOG --backtitle "Search Forename" --inputbox \
           "Please enter your Forename?" 0 0 2> /tmp/inputbox.tmp.$$
        retval=$?
        Forename=`cat /tmp/inputbox.tmp.$$`
        rm -f /tmp/inputbox.tmp.$$

    done
    Forename=$(echo $Forename | tr 'a-z' 'A-Z');
    echo;
    if ! grep -Fq "Forename: $Forename" $Filename ;then
        $DIALOG --msgbox "$Forename was not found in the File" 10 40;
    else
        $DIALOG --title "Forename Results" --infobox "`grep -n "Forename: $Forename" $Filename | sort ;`" 90 120 ;
        read enterKey;
    fi
    ;;
1)
    echo "Cancel pressed.";;
esac
;;

我遇到的问题是,如果用户输入空格,则forename仅验证为空,它显示文件中的所有数据。任何人都可以建议一种解决方法。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您可以使用bash的正则表达式语法。

while [[ -z $Forename || $Forename =~ ^\ +$ ]]; do
    # if forename is empty or contains only whitespace
    ...
done