1

This is a sample bash as you see in two ways:

1)First

#!/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)Second:

#!/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

Now the question:

With these two ways, how can I check if the input parameter is white space or null?

I want to check both white space or null in bash.

Thanks

MLSC
  • 5,872
  • 8
  • 55
  • 89
  • Space is not an integer. Change `-eq` with `=` to compare variable with space to `$IFS`. – jaypal singh Feb 22 '14 at 06:02
  • You mean `==` OR `=`? – MLSC Feb 22 '14 at 06:03
  • 1
    http://stackoverflow.com/questions/13509508/check-if-string-is-neither-empty-not-space-in-shell-script – Shivam Feb 22 '14 at 06:03
  • I want to know if it is null number doesn't change the value..but if it is white space number increases – MLSC Feb 22 '14 at 06:05
  • @MortezaLSC There's no difference for string comparisons. Bourne shell supports `=` and Bash supports `==`. But they are synonymous in bash. – jaypal singh Feb 22 '14 at 06:06
  • Ok I tried it ... but I want to check if it is null, number doesnt increase the value..and if it is white space, number increase its value – MLSC Feb 22 '14 at 06:08

2 Answers2

2

You can try something like this:

#!/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
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • 1
    Can you elaborate as to what didn't work? I tested it on my system. – jaypal singh Feb 22 '14 at 07:28
  • why not just ``((number++))`` ? Also you are not checking for all whitespace characters, only the regular space. You can use this ``[[ $input = *+([[:space:]])* ]]`` check with ``extglob`` instead. Or this regex ``[[ $input =~ [[:space:]]+ ]]``. But it also could be that you only want regular space. – Aleks-Daniel Jakimenko-A. Feb 22 '14 at 11:27
0

This portion checks if the input string is NULL

if [ "##"${input}"##" = "####" ]
then
echo "You have input a NULL string" 
fi

and this portion checks if one or more white space caharacters were input

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

combine them in aorder you see fit and set flags in if -- fi blocks to evaluate after you complete all comparisons.

MelBurslan
  • 2,383
  • 4
  • 18
  • 26
  • I tested it, but the error has occured:unexpected EOF while looking for matching `}' ./j.sh: line 15: syntax error: unexpected end of file – MLSC Feb 22 '14 at 06:41
  • sorry.. I made a typographical error. Please see the first line, marked in the 2nd portion. – MelBurslan Feb 22 '14 at 06:43
  • It has no error but doesn't work properly..let me update my post by your solution..the see it – MLSC Feb 22 '14 at 06:49
  • when you run my version of the script and hit space then hit enter, what do you get? "1" or "wrong" – MelBurslan Feb 22 '14 at 06:54
  • I noticed, you are specifying a 2 second time interval for the user to enter his/her input, but no string length. try `echo "Press space to add one to your number: "; read -n 1 input` instead of your input line – MelBurslan Feb 22 '14 at 07:15