Isspace


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Isspace
# 1  
Old 06-07-2004
Isspace

Hey gurus..
I am still struggling with unix..

whats is the equivalent of isspace function in Unix shell scripting.
I came across situation where a variable is a fixed lenth. And if it contains only spaces, then i should flag it as error.

for eg..

vartemp=" "

if isspace(vartemp) then
flag error

Any help ??

Thanks
Dilip
# 2  
Old 06-07-2004
Are you expecting any whacky characters?

It sounds like what you want to do is check for characters other than a space, but that is my guess.

"man grep"
# 3  
Old 06-08-2004
If you assume it can be a known number of spaces try something like this:

Code:
# this checks one space 

if [ $var = " " ] ; then
       echo "error..."
fi

If you assume it wil be more than one space, but an unknown number of spaces try something like this:

Code:
#!/bin/ksh

tmp=`echo $var | tr -d [:space:]`
if [ ${#tmp} -eq 0 ] ; then
     echo "error "
fi

# 4  
Old 06-08-2004
Jim,
its the 2nd case-- Unknown number of spaces.
I tried
tmp=`echo $var | tr -d [:space:]`
if [ ${#tmp} -eq 0 ] ; then
echo "error "
fi

But if there is a value in 'var', it did not work!.

for eg..
here's the detail requirement.----

typeset -L var ##Variable needs to be left justified, remove blank
var = "abcd1234"
echo ${#var} ==> this will show 8.
var= `get_value` ==> var gets assigned by some logic.
echo ${#var} ==> this will still show 8, irrespective of what assigned.

Here, i want to find out, if var is assigned empty, then get another value for var.
something like .. If isspace(var) then var = `get_value` else do_something_else ??

Thanks
Smilie Smilie
# 5  
Old 06-08-2004
I'm taking this to mean there can be no space or spaces in var anywhere. That's the only choice remaining - that I can see.

Code:
tmp=" "
isspace=`expr index $var $tmp`
if [ isspace -gt 0 ] ; then
     echo "error"
fi

If this isn't it - I don't get what you need.
# 6  
Old 06-10-2004
Got it worked out.. I have to use sed.
Following is the final code

isp=`echo $var | sed 's/ //g'`
if [ ${#isp} -eq 0 ] ; then
echo "error"
fi


Thanks
# 7  
Old 06-10-2004
An artiificial requirement like using sed = homework. I think there are rules about homework posts.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. Programming

isspace?

Hello, Does somebody know what is happening here? This piece of code should skip leading spaces (and others). If isspace encounters a non space character, it doesn't return false. If we analyze the " isspace(*cs_str);" in the debugger, it returns 0. If we check the value in b_space after... (4 Replies)
Discussion started by: Micky
4 Replies
Login or Register to Ask a Question