Reading a variable line by line with while loop
Ray Parrish
crp at cmc.net
Wed Dec 2 00:45:31 UTC 2009
Ray Parrish wrote:
> 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
>
Hello again,
I have found an if statement that reduces the duplicates to only two
pairs for the entire file. That's pretty good considering the number of
actual duplicates there were in the file to begin with. Here is the code
for that one -
function CompressHistory {
BashHistory=`cat ~/.bash_history`
while read ThisCommand; do
ThisCommand=${ThisCommand// /__};
if [[ "$History" =~ "$ThisCommand" ]]
then
echo "nothing" >/dev/null
else
History="$History $ThisCommand";
fi
done <<< "$BashHistory"
echo "$History"
}
This next bit of code has me frazzled trying to figure out the proper
regular expression to say "if this string exists within the larger
string". Nothing I have tried so far has worked, so here is the code,
and maybe someone can correct my regular expression so it works to weed
out duplicates as well.
BashHistory=`cat ~/.bash_history`
while read ThisCommand; do
ThisCommand=${ThisCommand// /__};
if [[ "$History" == [.]*$ThisCommand[.]* ]]
then
echo "nothing" >/dev/null
else
History="$History $ThisCommand";
fi
done <<< "$BashHistory"
From what I have read the dot is supposed to match any character, and
then the * specifies that any number of characters can appear in that
position. I'm just realizing that would specify perhaps one character
only, repeated to the ends of the large string before it would match.
I do not know how to specify that the whole front, and the whole back of
the variable should be the same with the matching string in the middle
of them.
Thanks again for any help you can be. Later, 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