The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Check space utilization in recursive mode sureshg_sampat Shell Programming and Scripting 1 06-02-2008 10:56 AM
Please help - disk space check script maddhadder71 Shell Programming and Scripting 0 05-08-2008 05:16 AM
read string, check string length and cut ozzy80 Shell Programming and Scripting 9 03-21-2007 02:56 PM
check space !!! kamlesh_p Shell Programming and Scripting 2 11-09-2005 11:01 AM
Check directory space? lesstjm Shell Programming and Scripting 3 04-19-2002 06:10 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-28-2008
Registered User
 

Join Date: Jul 2008
Posts: 15
how to check if a string consist of any space?

hi all
how do i check if a string consist of any space by using shell script.
i have the following code
while test -z $string
do
//prompting for another string if string is length 0
done

when i type "a b" it give me an error

test: a: binary operator expected

thanks
Reply With Quote
Forum Sponsor
  #2  
Old 07-29-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
You need to quote "$string" inside double quotes if it contains any whitespace. Aside from that, it might be more elegant to use case instead of if, especially for coping with all-whitespace input as well as blank inputs. Also, with case, there is no need to quote the variable you are testing:

Code:
while true; do
  read string
  case $string in *[^\ ]*) break;; *) echo try again;; esac
done
The above will pass if there is any non-whitespace character in the input.

Last edited by era; 07-29-2008 at 12:12 AM. Reason: Elaborate with an explicit while loop
Reply With Quote
  #3  
Old 07-29-2008
Moderator
 

Join Date: Sep 2007
Location: Germany
Posts: 1,031
-z tests, if the string is empty, not if it contains a blank.

This checks for any blanks and/or tabs
Code:
egrep -q "[[:space:]]" infile
echo $?
You can check $? if it found something.
Reply With Quote
  #4  
Old 07-29-2008
Registered User
 

Join Date: Jul 2008
Posts: 15
if i am taking user input and the input contain space??
Reply With Quote
  #5  
Old 07-29-2008
Moderator
 

Join Date: Sep 2007
Location: Germany
Posts: 1,031
Just as era showed already. You should try it a bit yourself, example on the prompt:

Code:
root@isau02:/data/tmp/testfeld> while true; do read a; echo ${a}| egrep -q "[[:space:]]"; if [[ $? = 0 ]]; then echo blank found; fi; done     jajaja
jaja jaja
blank found
lala            lala
blank found
Reply With Quote
  #6  
Old 07-29-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
The following will fail if there is a space anywhere in the input.

Code:
case $string in *\ *) echo try again;;  *) break;; esac
If you want to use egrep, you can avoid the Useless Use of Test $? with this:

Code:
if echo "$a" | egrep -q "[[:space:]]" ; then ...
The code above fails to quote $a correctly, but in this particular case, it's harmless. Still, a single case is cheaper and simpler than echo + grep and IMHO more elegant.
Reply With Quote
  #7  
Old 07-29-2008
Registered User
 

Join Date: Jul 2008
Posts: 15
sorry..i tried using all these code..but it dun work
i have the following code

while test -z $username
do
echo -n "enter username: " ;read username
done

this code gives me an error if i enter something like this
a b
test: a: binary operator expected

thanks a lot
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
spacing, string

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 08:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0