![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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 Last edited by era; 07-29-2008 at 12:12 AM. Reason: Elaborate with an explicit while loop |
|
#3
|
|||
|
|||
|
-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 $? |
|
#4
|
|||
|
|||
|
if i am taking user input and the input contain space??
|
|
#5
|
|||
|
|||
|
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
|
|
#6
|
|||
|
|||
|
The following will fail if there is a space anywhere in the input.
Code:
case $string in *\ *) echo try again;; *) break;; esac Code:
if echo "$a" | egrep -q "[[:space:]]" ; then ... |
|
#7
|
|||
|
|||
|
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 |
|||
| Google The UNIX and Linux Forums |
| Tags |
| spacing, string |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|