Re: bash help setting variables

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 10/17/2009 02:58 PM, Donald Russell wrote:
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?


You can use bash variable matching. I'll suppose we know that $data is something like:
    key1=value1 key2=value2 key3=value3
where the pairs are separated by whitespace of some kind. If not, then set IFS and make sure the pairs are separated by that.

Then:
    for pair in $data; do
        key=${pair%%=*};
        val=${pair#*=};
        ...now do something with $key and $val...
    done
It'll be more complicated if there's whitespace in the values, but you get the idea.

See: http://www.linuxjournal.com/article/8919.

Richard

--
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
Guidelines: http://fedoraproject.org/wiki/Communicate/MailingListGuidelines
[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux