Reading a variable line by line with while loop

Ray Parrish crp at cmc.net
Tue Dec 1 23:29:33 UTC 2009


PleegWat wrote:
> Alan McKay wrote:
>   
>>> echo "$Variable" | while read
>>> ...
>>> done
>>>
>>>    has better chances of working.
>>>       
>> This method has gotchas though, and I try to avoid it.  The pipe
>> causes the while to execute in a sub shell and so any variable changes
>> you make inside the loop will be lost when you get out of the loop.
>> For this reason I often actually create a file and do the redirect as
>> per the original post
>>
>> e.g.
>>
>> while stuff
>> do
>> done < file
>>     
>
> Another (bash-specific) solution:
>
> while condition
> do stuff
> done < <(command)
>
> The <(command) construct runs `command`, redirects stdout to a pipe, and 
> returns the name of the named pipe.
>
> This construct has the disadvantages of being bash-specific and 
> resulting in hard-to-read code.
>
> Two more solutions specifically for input from a variable: Heredoc
>
> while condition
> do stuff
> done <<EOF
> $variable
> EOF
>
> Or herestring (bash-specific)
>
> while condition
> do stuff
> done <<< "$variable"
>
> PleegWat
>   
Hello again,

Well, I'm making some progress. I have the following code which works to 
a point.

     while read ThisCommand; do
           ThisCommand=${ThisCommand// /__};
           if `echo "${History}" | grep "${ThisCommand}" 1>/dev/null 2>&1`
               then
                   echo "nothing" >/dev/null
               else
                   History="$History $ThisCommand";
           fi
     done < <(cat ~/.bash_history)
     Command=`Xdialog --stdout --title "Ray's Bash Manager - Select 
Command to Copy" --cancel-label "Exit" --combobox "Select a Command to 
Copy to the Clipboard." 0 60 $History`

The if statement is meant to catch duplicate lines, and it does, unless 
they contain quotes in them, and then they get duplicated in the output. 
Is there some way to trap even the lines with quotes in them?

Thanks again, Ray Parrish
-- 

The Future of Technology.
http://www.rayslinks.com/The%20Future%20of%20Technology.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com






More information about the ubuntu-users mailing list