LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to check for blank spaces in a string? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-check-for-blank-spaces-in-a-string-931443/)

Micky12345 02-26-2012 07:52 PM

How to check for blank spaces in a string?
 
my code for checking blank space in a string is as follows
Code:

if [ $temp ==[:blank:] ]
then
echo "hi"
fi

where $temp consists of each character of a string

but i am getting error as [: ==: unary operator expected
how i can rectify this

weibullguy 02-26-2012 08:10 PM

Use double quotes like this
Code:

if [ "$temp" == "[:blank:]" ]
then
    echo "hi"
fi


Micky12345 02-27-2012 05:51 AM

but still i m getting error as
[: =: unary operator expected

Cedrik 02-27-2012 06:09 AM

Code:

if [[ $temp =~ [:blank:] ]]
I would use [:space:] but YMMV

Micky12345 02-27-2012 06:20 AM

when i tried with blank
i got ans 5 times but in my string there is only one space

Micky12345 02-27-2012 07:10 AM

WHEN I triede with space its working but even if there is only one space many times "hi" gets displayed

catkin 02-27-2012 07:26 AM

Quote:

Originally Posted by Micky12345 (Post 4613120)
WHEN I triede with space its working but even if there is only one space many times "hi" gets displayed

Can you post more of the script, including how $temp is set and post the actual output?

David the H. 02-27-2012 10:05 AM

There are several problems going on here.

First, you need to include a space between the operator and the expression. This is what's giving you the "unary operator" error.

Second, [ is the old-style traditional test operator, which only does literal string matching. It cannot match substrings. You have to use the newer [[ test instead for that (or use a different type of evaluation entirely, such as a case statement*).

Third, inside [[, the "==" operator applies either a literal or a globbing pattern match, both of which must still must match the string as a whole, not a substring. Add "*" globs to either end, and do not quote it (or else it will be treated as a literal string to match).

So to test for the existence of a space or tab character inside the variable temp:
Code:

if [[ $temp == *[:blank:]* ]]; then
Alternately, [[ also allows full regular expression matching, using the =~ operator. Regex allows you to match substrings. Cedrik's version above almost does it, but he made one small mistake. The [:blank:] character class now has to be enclosed in a regex "[]" character range expression. So:

Code:

if [[ $temp =~ [[:blank:]] ]]; then
Also, particularly for complex pattern matches, you should store the expression in a separate variable first. Otherwise you may have problems trying to escape everything properly

Code:

re='[[:blank:]]'
if [[ $temp =~ $re ]]: then


* (Note) A case statement can be used to apply globbing patterns to variables even in shells that don't support "[[".

Code:

case $temp in
        *[:blank:]*)        <commands>        ;;
esac


Cedrik 02-27-2012 10:34 AM

Quote:

Originally Posted by David the H. (Post 4613240)
Alternately, [[ also allows full regular expression matching, using the =~ operator. Regex allows you to match substrings. Cedrik's version above almost does it, but he made one small mistake. The [:blank:] character class now has to be enclosed in a regex "[]" character range expression. So:

Code:

if [[ $temp =~ [[:blank:]] ]]; then

Thanks for the correction


All times are GMT -5. The time now is 04:03 PM.