akshay
akshay

Reputation: 29

How to split the variable or name into an variable?

Using shell scripting, I want to split the name into an variable. Suppose in my .conf file the data is like this:

ssh.user = root
ssh.server = localhost

then I want this ssh.user in one variable and root in another variable? So what should I do?

Upvotes: 0

Views: 123

Answers (1)

fvu
fvu

Reputation: 32953

If you can live with a solution that doesn't use dots in the variable names, you could just use source (source will execute the file given as argument as a script):

A file called config

sshuser = root
sshserver = localhost

`And then the script using that configuration:

#!/bin/bash
source config
echo $sshuser

will output

root

Several techniques other than sourcing are explained right here on StackOverflow Reading a config file from a shell script


Now, the fact that your variables contain a dot is an issue, but perhaps another technique (using awk) explained in yet another SO question could help: How do I grab an INI value within a shell script?

Applied to your case that will give something like

ssshuser=$(awk -F "=" '/ssh.user/ {print $2}' configurationfile)

Last potential issue, the whitespaces. See here How to trim whitespace from a Bash variable?

Upvotes: 1

Related Questions