Map hostnames to strings in config?

Earlier today I wrote a post about a Nex server I wrote in bash (it doesn't have networking, but it will take input and respond to it the same way a Nex server would respond if it had gotten that input via TCP, which means if you make it accessible over a network by running something like "nc -lk 1900 -e ./script" it'll work as a regular Nex server), and I thought maybe I could try modifying the code to support the Spartan protocol. The Spartan protocol includes hostnames in requests meaning you could have multiple different servers on the same system, each with its own root directory, and the system would know which server the client wants to talk to based on the hostname.

I learned about the source command and figured the user could map hostnames to directories in a config by writing something like:

example.com=/var/spartan/example

96.7.128.175=/var/spartan/IPv4

21e:e795:8e82:a9e2:ff48:952d:55f2:f0bb=/var/spartan/IPv6

Then, after receiving a request, the script would set a directory variable to be equal to the variable that is named the same as the hostname in the request.

However, it seems Bash doesn't want to put a dot in the name of a variable, and instead interprets $example.com as a variable, that is named "example", followed by the string ".com". Does anyone know how to do this?

Posted in: s/bash

๐Ÿš€ asdf

Feb 16 ยท 5 months ago ยท ๐Ÿ‘ norayr

1 Comment

๐Ÿฆ wasolili [...] ยท Feb 17 at 04:33:

bash doesn't allow . in variables, but you could parse the config up front to build an associative array.

โ€” https://www.gnu.org/software/bash/manual/html_node/Arrays.html

after you build the hostmap associative array and parse the hostname out of the request you'd be able to do something like ${hostmap["$hostname"]} to access it


Source