Supressing an error message in bash script

Neil Cherry ncherry at linuxha.com
Sat Dec 12 12:41:26 UTC 2009


On 12/11/2009 10:23 PM, Ray Parrish wrote:
> Neil Cherry wrote:
>> On 12/11/2009 09:40 PM, Ray Parrish wrote:
>>   
>>> function ArrayLoad {
>>>       PackageData=`"dpkg -p gedit"` 2> /dev/null
>>>     
>>
>> Try it without the double quotes. Does that do what you want?
>>   
> Wirhout the double quotes nothing gets loaded into the array.. Also 
> nothing gets loaded into the array with my original code, as the output 
> thst I thought ewas being generated by that function, was actually being 
> output by the following function which works very nicely. I had 
> forgotten to comment out it's call in my test code so it was generating 
> the loop output before the only output of the first function I quoted 
> which was the error message.
> 
> # Test array assignment from variable line by line
> function ReadintoArray {
>      PackageData=`dpkg -p gedit`
>      LoopCount=0
>      while read ThisLine; do
>            Data[$LoopCount]="$ThisLine"
>            echo "$LoopCount - ${Data[$LoopCount]}"
>            (( LoopCount++ ))
>      done <<<"$PackageData"
> }
> 
> I guess I'll go with this second version which works great, unless 
> someone can tell me how to load the array with a single call like I was 
> trying in the other function. Just looking for the most efficient way to 
> get the data into the array line by line.
> 
> Later,Ray Parrish
> 
> 

function ReadintoArray {
    LoopCount=0
    dpkg -p gedit | while read line
    do
	echo "$LoopCount - $line"
	(( LoopCount++ ))
    done
}

How about the above? If I tried it your way I ended up with each
word as an array element. To fix that I could have changed the IFS
to use only a \n (but I was lazy). So went the read route.

-- 
Linux Home Automation         Neil Cherry       ncherry at linuxha.com
http://www.linuxha.com/                         Main site
http://linuxha.blogspot.com/                    My HA Blog
Author of:    	Linux Smart Homes For Dummies




More information about the ubuntu-users mailing list