Operating System - HP-UX
1834149 Members
2491 Online
110064 Solutions
New Discussion

Re: Shell programming: Search a blank

 
Miguel Cuesta
Advisor

Shell programming: Search a blank

Hi!

I'm creating a shell script, and I want to execute a command only if there is a blank in ${FILE}.

How could I carry it out?

if _?_?_?_ then
[command]
fi

Thank you very much.
15 REPLIES 15
Robin Wakefield
Honored Contributor

Re: Shell programming: Search a blank

If, by blank, you mean the file is empty, then:

if ! [ -s $FILE ] ; then
...
fi

but if you mean a single space as the only line, then:

if [ `cat $FILE | wc -w` -eq 0 ] ; then
...
fi

could be one way to do it.

Robin
Stefan Schulz
Honored Contributor

Re: Shell programming: Search a blank

If you are really looking for a blank (" ") and not a blank file or a blank line in this file you could use:

if [ `grep -c " " $FILE`] ; then
...
fi

Hope this helps

Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.
Praveen Bezawada
Respected Contributor

Re: Shell programming: Search a blank

Hi
If you are looking for a blank line in a file

while read each_line;do
echo $each_line | grep " " > /dev/null
if [ $? -eq 0 ];then
echo $each_line
.
.
.
fi
done
where a.lst in the input file

..PRB...
Uffe Gavnholt_1
Honored Contributor

Re: Shell programming: Search a blank

And finally if you need to search for a blank in the file name, try this:

if
echo $FILE | grep " "
then
...
fi
Miguel Cuesta
Advisor

Re: Shell programming: Search a blank

I meant 'a blank in the __file name__'

Thank you.
Miguel Cuesta
Advisor

Re: Shell programming: Search a blank

Using Uffe's script, I get this message:

./myscript.sh[43]: test: A ] character is missing.
grep: can't open ]
Dave Kelly_1
Respected Contributor

Re: Shell programming: Search a blank

You can use:

if [ `echo $FILE | wc -w` -gt 1 ]
then
commands
fi
Joseph Chakkery
Valued Contributor

Re: Shell programming: Search a blank

Hello,

Try this one.

FILE="File Name" #Let's say var is like this.
echo $FILE|grep " " >/dev/null
if [ $? -ne 0 ]
then
echo " There is sapce" #U can run ur script here.
else
echo "No space"
fi

Hope this may help u.
Knowledge is wealth
Wodisch
Honored Contributor

Re: Shell programming: Search a blank

Hello Miguel,

first you will have to *quote* anyway ;-)
Then, if you are looking for the values of "${FILE}" as
a variable, test it like this:
if [ "${FILE}" = " " ]
then echo "Bingo - a space only"
fi

Or you can insert some (almost) arbitrary characters
for readability:
if [ "x${FILE}x" = "x x" ]
then echo "Bingo - a space only"
fi

But if you are checking wether there is file with the name
in "${FILE}", and this only contains a single space, you
will have to do something like:
if [ "$(wc -c ${FILE})" -eq 1 ]
then if [ "$(cat ${FILE})" = " " ]
then echo Bingo - the files contains only a space
else echo file is only one char in size but not a space
fi
fi

HTH,
Wodisch

PS: You could use "perl", where you have direct access
to the file's attributes, like size, and where file-
handling is pretty simple...
Magdi KAMAL
Respected Contributor

Re: Shell programming: Search a blank

Hi Miguel,

# The argument for the grep is one blank
echo $fileName ? grep " " > /dev/null
if [[ $? = 0 ]]
then
echo " File name contains at least one blank"
else
echo " File name does not contains at least one blank"
fi

Magdi
Curtis Larson_1
Valued Contributor

Re: Shell programming: Search a blank

in the ksh:

if [[ "$file" = *+( )* ]] then

or for other white space character also

if [[ "$file" = *+([:space:])* ]] then
Phil Squire
Advisor

Re: Shell programming: Search a blank

You're always going to stuggle testing for spaces in shell because it's a delimeter. What about awk?

echo "$file" | awk 'BEGIN {FS=""} / / {print "*" }'

Will print an asterix if $file contains one or more spaces, including leading and trailing spaces.

Hope this helps.
Phil
Unix? Is that like Windows? Where's the mouse?
David G Ledger
Occasional Advisor

Re: Shell programming: Search a blank

If the variable FILE is to be tested for an embedded blank:

set -A temp $FILE
[ ${#temp[*]} -gt 0 ] && echo "$FILE has a blank"

All shell builtin commands - no fork/exec

David

Re: Shell programming: Search a blank

case "$FILE" in
*' '*) ... ;;
esac

Simple, efficient, works in every sh-type shell from original Bourne up.
David G Ledger
Occasional Advisor

Re: Shell programming: Search a blank

Of course I meant

[ ${#temp[*]} -gt 1 ] && echo "$FILE has a blank"