bash help setting variables

Steven W. Orr steveo at syslang.net
Sun Oct 18 04:15:35 UTC 2009


On 10/17/09 14:58, quoth Donald Russell:
> I'm trying to write a bash script which makes an inquiry to an http
> service that replies with a series of keyword=value plain text data,
> one pair per line.
> I want to extract only a few of the lines, and make variables of them
> available to rest of my bash script.
> 
> I have this so far, and it works fine in terms of selecting the
> correct data items... but how can I get them into $1 $2, or other
> symbolic names?
> 
> wget -O - http://.../cgi-bin/dirent?ntid=$MYVAR \
>         | egrep '^(LastName)|(FirstName)|(Email)=' \
>         | sort \
>         | sed s/.*=//
> 
> The idea of sorting was because I was anticipating putting the results
> in an array, then I'd know array[0] is email... etc
> 
> I also tried using sed to convert those lines into assignment
> statements and piping that to eval... nope
> 
> This sort of thing must be pretty common, but I'm just missing
> something fundamental.
> 
> I tried setting variable like this:
> myarray=$( <all that stuff above> )
> 
> but that just created a variable with all the item including their \n
> characters...
> I'm ALMOST there... I just have to add some sed stuff to escape
> quotes, and wrap quotes so I can use the pieces in other commands.
> But what's giving me trouble is how to get the individual parts into
> individual vars, or array elements.
> 
> Any assistance is appreciated.
> 
> Thanks
> 

If this produces the output you claim it does then the first run will do what
you want. The 2nd is just to remind people how elegant bash can be.

#! /bin/bash

wget_cmd()
{
    cat <<EOF
Somelastname
Somefirstname
Some at example.com
EOF
}

# or you could just do this

wget2_cmd()
{
    # Don't pipe to sort and sed
    cat <<EOF
LastName='Some von lastname'
FirstName='Somefirstname'
Email='Some at example.com'
EOF
}

# Example 1 using wget_cmd
myarray=($(wget_cmd))

echo "myarray[LastName]=${myarray[LastName]}"
echo "myarray[FirstName]=${myarray[FirstName]}"
echo "myarray[Email]=${myarray[Email]}"

# Example 2 using wget_cmd2
typeset oldIFS="${IFS}"
typeset line
IFS==

while read line
do
    set -- $line
    case $1 in
    LastName)
	myarray[LastName]="$2"
	;;
    FirstName)
	myarray[FirstName]="$2"
	;;
    Email)
	myarray[Email]="$2"
	;;
    *)
	echo "Dood you blue it:$1" 1>&2
	exit 1
	;;
    esac
done < <(wget2_cmd)

echo "myarray[LastName]=${myarray[LastName]}"
echo "myarray[FirstName]=${myarray[FirstName]}"
echo "myarray[Email]=${myarray[Email]}"





-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 261 bytes
Desc: OpenPGP digital signature
Url : http://lists.fedoraproject.org/pipermail/users/attachments/20091018/01a2e9d7/attachment-0001.bin 


More information about the users mailing list