how to check if a string consist of any space?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to check if a string consist of any space?
# 1  
Old 07-29-2008
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
# 2  
Old 07-29-2008
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 04:12 AM.. Reason: Elaborate with an explicit while loop
# 3  
Old 07-29-2008
-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.
# 4  
Old 07-29-2008
if i am taking user input and the input contain space??
# 5  
Old 07-29-2008
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  
Old 07-29-2008
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.
# 7  
Old 07-29-2008
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check file for string existence before appending it with string

I want to append file with a string but before doing that i want to check if this string already exist in that file.I tried with grep on Solaris 10 but unsuccessful.Man pages from grep seems to suggest if the string is found command status will be 0 and if not 1.But i am not finding it.May be i... (2 Replies)
Discussion started by: sahil_shine
2 Replies

2. Homework & Coursework Questions

Looking to check disk space

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Need to check the disk space and if any portion disk space usage high then write to one file, later will... (5 Replies)
Discussion started by: Jasminshakoor
5 Replies

3. Shell Programming and Scripting

Check if line has a space

Hi, I want to check if the given line from a text file has a spaces in between. if it does, then I want to add '"' double quotes at the beginning and end of the line. Otherwise leave the line as it is. For example, below is the sample content from my file. $cat file.txt test1 test2... (6 Replies)
Discussion started by: svajhala
6 Replies

4. Programming

Value printed by gdb does not consist with the right value

Hello, I find the value printed by gdb does not consist with the right value.The following is the output. (gdb) 7 while ( ( optc = getopt(argc, argv, ":b:B:h" ) ) != -1 ) { (gdb) 8 printf( "%c %d %s\n", optc, optind, optarg); (gdb) B 5 1-2 7 while ( ( optc =... (1 Reply)
Discussion started by: 915086731
1 Replies

5. Shell Programming and Scripting

Need to build Shell Script to search content of a text file into a folder consist several files

Have to read one file say sourcefile containing several words and having another folder containing several files. Now read the first word of Sourcefile & search it into the folder consisting sevral files, and create another file with result. We hhave to pick the filename of the file in which... (3 Replies)
Discussion started by: mukesh.baranwal
3 Replies

6. Shell Programming and Scripting

how to check disk space

Hi All, pls go thru the below code and help me. when i check "df -k" in my solaris system .. it will show like below.. fd 0 0 0 0% /dev/fd /dev/dsk/c0t0d0s3 20171281 2319266 17650303 12% /var /dev/dsk/c0t0d0s4 10085260 443854... (15 Replies)
Discussion started by: steve2216
15 Replies

7. Solaris

Disk space check

Hi, I have a question regarding finding free space on the disk of a solaris machine. Many mount points are available in my machine. Right now i am using df -b option to get the free disk space available. I have an assignment to check free space on the disk. I pass the directory as a... (6 Replies)
Discussion started by: raghu.amilineni
6 Replies

8. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

9. Shell Programming and Scripting

check space !!!

The have written the below script :- ============================ SPACE=`bdf /DATA_TRANSFER|awk '{print $4}' |grep "%"` TEST="96%" if then echo "Continue ....." sleep 2 else echo " Current space for DATA_TRANSFER is less than 02 %" echo " Pls clear space and than continue ....."... (2 Replies)
Discussion started by: kamlesh_p
2 Replies

10. Shell Programming and Scripting

Check directory space?

Is there some command I can use to check to see if there is 2 Gig of space available in a directory before I created a 2 Gig file? (3 Replies)
Discussion started by: lesstjm
3 Replies
Login or Register to Ask a Question