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

6
  • Space is not an integer. Change -eq with = to compare variable with space to $IFS. Commented Feb 22, 2014 at 6:02
  • You mean == OR =? Commented Feb 22, 2014 at 6:03
  • 1
    stackoverflow.com/questions/13509508/… Commented Feb 22, 2014 at 6:03
  • I want to know if it is null number doesn't change the value..but if it is white space number increases Commented Feb 22, 2014 at 6:05
  • @MortezaLSC There's no difference for string comparisons. Bourne shell supports = and Bash supports ==. But they are synonymous in bash. Commented Feb 22, 2014 at 6:06

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Can you elaborate as to what didn't work? I tested it on my system.
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.
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.

5 Comments

I tested it, but the error has occured:unexpected EOF while looking for matching `}' ./j.sh: line 15: syntax error: unexpected end of file
sorry.. I made a typographical error. Please see the first line, marked in the 2nd portion.
It has no error but doesn't work properly..let me update my post by your solution..the see it
when you run my version of the script and hit space then hit enter, what do you get? "1" or "wrong"
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.