Problem with Strings in for loop

Problem with Strings in for loop

am 25.01.2008 15:45:04 von NeOnD

Hi,

I have a weird problem with my sh script -
I have a function ListBooks() which returns an output like below when
being called -

Harry Potter and the Chamber of Secrets
Harry Potter and the Deathly Hallows
Harry Potter and the Goblet of Fire
Harry Potter and the Half-Blood Prince

But when try to use it's output in "for" loop it's all messed up -
for X in $(ListBooks); do
echo "--$X--"
done
- - - - output - - - -
--Harry--
--Potter--
--and--
--the--
--Chamber--
--of--
--Secrets
--Harry--
--Potter--
--and--
--the--
--Deathly--
--Hallows
--Harry--
--Potter--
--and--
--the--
--Goblet--
--of--
--Fire
--Harry--
--Potter--
--and--
--the--
--Half-Blood--
--Prince--

Whereas I'm expecting this output -

--Harry Potter and the Chamber of Secrets--
--Harry Potter and the Deathly Hallows--
--Harry Potter and the Goblet of Fire--
--Harry Potter and the Half-Blood Prince--

Any suggestions ?

I even tried with "$(ListBooks)" but it doesn't seem to help much -

--Harry Potter and the Chamber of Secrets
Harry Potter and the Deathly Hallows
Harry Potter and the Goblet of Fire
Harry Potter and the Half-Blood Prince--

thanks in advance !

Re: Problem with Strings in for loop

am 25.01.2008 15:54:11 von mallin.shetland

NeOnD scrisse:

> ...
> for X in $(ListBooks); do
> echo "--$X--"
> done
> ...

Command substitution in loops are *EVIL*


> ...
> I even tried with "$(ListBooks)" but it doesn't seem to help much -
> ...

No, it doesn't work


> Any suggestions ?


Avoid loop or, if a loop is necessary:

ListBooks |
while read X
echo "$X"
done

Re: Problem with Strings in for loop

am 25.01.2008 15:54:32 von Ed Morton

On 1/25/2008 8:45 AM, NeOnD wrote:
> Hi,
>
> I have a weird problem with my sh script -
> I have a function ListBooks() which returns an output like below when
> being called -
>
> Harry Potter and the Chamber of Secrets
> Harry Potter and the Deathly Hallows
> Harry Potter and the Goblet of Fire
> Harry Potter and the Half-Blood Prince
>
> But when try to use it's output in "for" loop it's all messed up -
> for X in $(ListBooks); do
> echo "--$X--"
> done

ListBooks |
while IFS= read -r X; do
echo "--$X--"
done

You should be using awk rather than a shell loop for this though:

ListBooks | awk '{print "--"$0"--"}'

I suspect "ListBooks" would be better off inside awk too.

Ed.

Re: Problem with Strings in for loop

am 25.01.2008 16:19:07 von NeOnD

On Jan 25, 7:54 pm, Ed Morton wrote:
> On 1/25/2008 8:45 AM, NeOnD wrote:
>
> > Hi,
>
> > I have a weird problem with my sh script -
> > I have a function ListBooks() which returns an output like below when
> > being called -
>
> > Harry Potter and the Chamber of Secrets
> > Harry Potter and the Deathly Hallows
> > Harry Potter and the Goblet of Fire
> > Harry Potter and the Half-Blood Prince
>
> > But when try to use it's output in "for" loop it's all messed up -
> > for X in $(ListBooks); do
> > echo "--$X--"
> > done
>
> ListBooks |
> while IFS= read -r X; do
> echo "--$X--"
> done
>
> You should be using awk rather than a shell loop for this though:
>
> ListBooks | awk '{print "--"$0"--"}'
>
> I suspect "ListBooks" would be better off inside awk too.
>
> Ed.

Thanks Guys That was really helpful.
I was actually intending to use the output of ListBooks() as the input
to "select" to get it as menu. No clue from where to start...
Something like...

ListBooks | select X in ; do echo $X; done

Please help !

Re: Problem with Strings in for loop

am 25.01.2008 16:22:45 von NeOnD

On Jan 25, 7:54 pm, Ed Morton wrote:
> On 1/25/2008 8:45 AM, NeOnD wrote:
>
> > Hi,
>
> > I have a weird problem with my sh script -
> > I have a function ListBooks() which returns an output like below when
> > being called -
>
> > Harry Potter and the Chamber of Secrets
> > Harry Potter and the Deathly Hallows
> > Harry Potter and the Goblet of Fire
> > Harry Potter and the Half-Blood Prince
>
> > But when try to use it's output in "for" loop it's all messed up -
> > for X in $(ListBooks); do
> > echo "--$X--"
> > done
>
> ListBooks |
> while IFS= read -r X; do
> echo "--$X--"
> done
>
> You should be using awk rather than a shell loop for this though:
>
> ListBooks | awk '{print "--"$0"--"}'
>
> I suspect "ListBooks" would be better off inside awk too.
>
> Ed.

Thanks Guys That was really helpful.
I was actually intending to use the output of ListBooks() as the input
to "select" to get it as menu. No clue from where to start...
Something like...

ListBooks | select X in ; do echo $X; done

Please help !

Re: Problem with Strings in for loop

am 25.01.2008 16:31:57 von Ed Morton

On 1/25/2008 9:22 AM, NeOnD wrote:
> On Jan 25, 7:54 pm, Ed Morton wrote:
>
>>On 1/25/2008 8:45 AM, NeOnD wrote:
>>
>>
>>>Hi,
>>
>>>I have a weird problem with my sh script -
>>>I have a function ListBooks() which returns an output like below when
>>>being called -
>>
>>>Harry Potter and the Chamber of Secrets
>>>Harry Potter and the Deathly Hallows
>>>Harry Potter and the Goblet of Fire
>>>Harry Potter and the Half-Blood Prince
>>
>>>But when try to use it's output in "for" loop it's all messed up -
>>>for X in $(ListBooks); do
>>> echo "--$X--"
>>>done
>>
>>ListBooks |
>>while IFS= read -r X; do
>> echo "--$X--"
>>done
>>
>>You should be using awk rather than a shell loop for this though:
>>
>>ListBooks | awk '{print "--"$0"--"}'
>>
>>I suspect "ListBooks" would be better off inside awk too.
>>
>> Ed.
>
>
> Thanks Guys That was really helpful.
> I was actually intending to use the output of ListBooks() as the input
> to "select" to get it as menu. No clue from where to start...
> Something like...
>
> ListBooks | select X in ; do echo $X; done

IFS="
"
select X in $(ListBooks) ; do
echo "$X"
done

Ed.

Re: Problem with Strings in for loop

am 25.01.2008 17:35:04 von Stephane CHAZELAS

On Fri, 25 Jan 2008 07:19:07 -0800 (PST), NeOnD wrote:
[...]
>> ListBooks | awk '{print "--"$0"--"}'
>>
>> I suspect "ListBooks" would be better off inside awk too.
>>
>> Ed.
>
> Thanks Guys That was really helpful.
> I was actually intending to use the output of ListBooks() as the input
> to "select" to get it as menu. No clue from where to start...
> Something like...
>
> ListBooks | select X in ; do echo $X; done
[...]

"select" is not sh.

selected_book=$(
ListBooks | awk '
{
printf "%4d - %s\n", NR, $0 > "/dev/tty"
choice[NR] = $0
}
END {
do {
printf "\nEnter your choice: " > "/dev/tty"
getline < "/dev/tty"
} while ($0 < 1 || $0 > NR)
print choice[$0]
}'
)

--
Stephane

Re: Problem with Strings in for loop

am 26.01.2008 03:47:49 von Icarus Sparry

On Fri, 25 Jan 2008 16:35:04 +0000, Stephane Chazelas wrote:

> On Fri, 25 Jan 2008 07:19:07 -0800 (PST), NeOnD wrote: [...]
>>> ListBooks | awk '{print "--"$0"--"}'
>>>
>>> I suspect "ListBooks" would be better off inside awk too.
>>>
>>> Ed.
>>
>> Thanks Guys That was really helpful.
>> I was actually intending to use the output of ListBooks() as the input
>> to "select" to get it as menu. No clue from where to start... Something
>> like...
>>
>> ListBooks | select X in ; do echo $X; done
> [...]
>
> "select" is not sh.
>
> selected_book=$(
> ListBooks | awk '
> {
> printf "%4d - %s\n", NR, $0 > "/dev/tty" choice[NR] = $0
> }
> END {
> do {
> printf "\nEnter your choice: " > "/dev/tty" getline < "/dev/tty"
> } while ($0 < 1 || $0 > NR)
> print choice[$0]
> }'
> )

No, but it is in ksh, bash, and zsh-beta at least.

IFS=$'\n' CHOICES=($(ListBooks))
select book in ${CHOICES[@]} ; do echo $book ; done

Re: Problem with Strings in for loop

am 26.01.2008 10:47:19 von Stephane CHAZELAS

On 26 Jan 2008 02:47:49 GMT, Icarus Sparry wrote:
[...]
> No, but it is in ksh, bash, and zsh-beta at least.
>
> IFS=$'\n' CHOICES=($(ListBooks))

You need to disable filename generation above (except for zsh
where that bug has been fixed)

set -f; IFS=$'\n'; choices=($(ListBooks))

Note that arrays are not sh either. $'\n' is not in ksh88 or
pdksh, neither is x=(...)

> select book in ${CHOICES[@]} ; do echo $book ; done

You forgot the quotes:

in "${CHOICES[@]}" ... "$book"

--
Stephane