LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2013, 03:05 PM   #1
darkangel29
Member
 
Registered: Nov 2004
Location: Puerto Rico
Distribution: Ubuntu 10.04
Posts: 121

Rep: Reputation: 15
Check bash array element for whitespace or empty


I have a script that separates the input string into an array my problem is that I want to check determine if the array element is whitepace or empty do something like a substitution. This is what I have and it works for any value except whitespace or empty.

Code:
echo -n "Enter string: "
read s

i=0
while [ $i -lt ${#s} ]; do y[$i]=${s:$i:1};  i=$((i+1));done

for (( k=0; k < $length; k++ ))
do
 if [ ${y[$k]} == " " ]  
  then
   y[$k]=32
 fi
done
But it doesn't substitute the whitespace and I have tried many other different combinations. If I try another value it works.

Code:
for (( k=0; k < $length; k++ ))
do
 if [ ${y[$k]} == "a" ]  
  then
   y[$k]=32
 fi
Thanks for the help!
 
Old 08-06-2013, 03:22 PM   #2
vmccord
Member
 
Registered: Jun 2012
Location: Topeka, KS
Distribution: Mostly AWS
Posts: 71
Blog Entries: 31

Rep: Reputation: Disabled
Your if statement is looking for a space, but you mention looking for empty which I think of as null. in bash it would be
Code:
 if [ -z "${y[$k]}" ]
 
Old 08-06-2013, 03:27 PM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Code:
if [ "${y[$k]}" == " " ]
you need quotes, else shell sees just
Code:
if [   == " " ]
 
Old 08-06-2013, 03:33 PM   #4
darkangel29
Member
 
Registered: Nov 2004
Location: Puerto Rico
Distribution: Ubuntu 10.04
Posts: 121

Original Poster
Rep: Reputation: 15
Thanks!! It was the quotes
Quote:
if [ "${y[$k]}" == " " ]
did the trick
 
Old 08-06-2013, 03:44 PM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
one other thing

in your script you have undeclared $length

I assume you already declared that length somewhere else

Code:
for (( k=0; k < ${#s}; k++ ))
is safer

only mention it, as it could be a 'bug' for you, ( since you have a new s, with potential for new length )

but I guess you were just using read s for purpose of example
 
Old 08-06-2013, 11:02 PM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
You could also check if an array element is empty but is still set through generation of indices:
Code:
function has_element {
    IFS=' ' eval "[[ \" \${!$1[*]} \" == *\" $2 \"* ]]"
}

A=([1]="")

has_element A 1 && echo a ## messages a
To check if a string has one or more spaces:
Code:
shopt -s extglob
if [[ $VAR == +([[:blank:]]) ]]; then ...
# or
if [[ $VAR =~ ^[[:blank:]]+$ ]]; then ...
To check if a string is empty, or has one or more spaces, use *:
Code:
shopt -s extglob
if [[ $VAR == *([[:blank:]]) ]]; then ...
# or
if [[ $VAR =~ ^[[:blank:]]*$ ]]; then ...
You could use [:space:] instead of [:blank:] but I don't know how they differ. Or should I say I forgot. But I had a reason to use [:blank:] over it.

Last edited by konsolebox; 08-06-2013 at 11:06 PM. Reason: ^$ for regex
 
Old 08-07-2013, 12:31 AM   #7
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
Quote:
You could use [:space:] instead of [:blank:] but I don't know how they differ. Or should I say I forgot. But I had a reason to use [:blank:] over it.
that's pretty easy:
:blank: > will work when there is no value supplies not even a space.
you can write simple program and test with using "" and " ";
 
Old 08-07-2013, 01:35 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by SAbhi View Post
that's pretty easy:
:blank: > will work when there is no value supplies not even a space.
Where did you get that from? [:blank:] despite the name doesn't represent empty values.
Code:
shopt -s extglob
[[ "" == +([[:blank:]]) ]] || echo "No it doesn't."
Anyway looking at gawk's manual, these are the differences:
Code:
[:blank:]  Space or tab characters.
[:space:]  Space characters (such as space, tab, and formfeed, to name a few).

Last edited by konsolebox; 08-07-2013 at 01:39 AM.
 
Old 08-07-2013, 02:12 AM   #9
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
AWWW sorry for this, its my mistake, what I was thinking when I posted that? :P

I by mistake compared it with ""
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
bash array substitute the value of nth element casperdaghost Linux - Newbie 11 03-28-2013 04:15 AM
[SOLVED] Compare array index number against element in bash rewtnull Programming 10 11-01-2011 02:53 PM
[SOLVED] BASH SCRIPTING: Help! Yet another check element in array Question kristo5747 Programming 2 07-29-2010 09:47 AM
get index of an element in array in bash mangatmodi Programming 4 11-20-2009 07:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration