LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-26-2012, 08:52 PM   #1
Micky12345
Member
 
Registered: Feb 2012
Posts: 58

Rep: Reputation: Disabled
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

Last edited by colucix; 02-27-2012 at 09:48 AM. Reason: Fixed CODE tags
 
Old 02-26-2012, 09:10 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,812
Blog Entries: 1

Rep: Reputation: 259Reputation: 259Reputation: 259
Use double quotes like this
Code:
if [ "$temp" == "[:blank:]" ]
then
    echo "hi"
fi
 
Old 02-27-2012, 06:51 AM   #3
Micky12345
Member
 
Registered: Feb 2012
Posts: 58

Original Poster
Rep: Reputation: Disabled
but still i m getting error as
[: =: unary operator expected

Last edited by Micky12345; 02-27-2012 at 06:55 AM.
 
Old 02-27-2012, 07:09 AM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Code:
if [[ $temp =~ [:blank:] ]]
I would use [:space:] but YMMV
 
Old 02-27-2012, 07:20 AM   #5
Micky12345
Member
 
Registered: Feb 2012
Posts: 58

Original Poster
Rep: Reputation: Disabled
when i tried with blank
i got ans 5 times but in my string there is only one space

Last edited by Micky12345; 02-27-2012 at 07:27 AM.
 
Old 02-27-2012, 08:10 AM   #6
Micky12345
Member
 
Registered: Feb 2012
Posts: 58

Original Poster
Rep: Reputation: Disabled
WHEN I triede with space its working but even if there is only one space many times "hi" gets displayed
 
Old 02-27-2012, 08:26 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by Micky12345 View Post
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?
 
Old 02-27-2012, 11:05 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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

Last edited by David the H.; 02-27-2012 at 11:10 AM. Reason: minor rewording and fixes
 
1 members found this post helpful.
Old 02-27-2012, 11:34 AM   #9
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Thumbs up

Quote:
Originally Posted by David the H. View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
xine and blank spaces almcneill Linux - Software 1 10-20-2011 07:51 PM
[SOLVED] SED - how to remove blank spaces carolflb Linux - Newbie 2 01-30-2010 07:02 AM
reg expr: match a string A-Z, a-z, * or a blank ( but string can not be all blanks) matt007 Programming 4 12-22-2009 09:55 AM
selectively remove blank spaces from string Cpt_Cell Linux - Newbie 2 10-08-2009 03:41 AM
spaces in a string Longinus Programming 18 03-08-2004 07:02 PM


All times are GMT -5. The time now is 10:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration