0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
akshay
  • 29
  • 6

1 Answers1

1

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?

Community
  • 1
  • 1
fvu
  • 32,488
  • 6
  • 61
  • 79
  • In my config file the data is like this. – akshay May 08 '15 at 10:36
  • As I mentioned in my question.So how to seperate this data in variable Please help me – akshay May 08 '15 at 10:37
  • @akshay please refer to the article I linked to. I explained sourcing because the other article doesn't. – fvu May 08 '15 at 10:50
  • ,I want ssh.user in one variable and then this variable used for further process. – akshay May 08 '15 at 11:01
  • So how to use this as variable? var1=ssh.user nad then var1=root – akshay May 08 '15 at 11:03
  • That seems like a completely useless thing to do, but if you insist you could do `var1=$(awk -F "=" '/ssh.user/ {print $1}' configurationfile)` followed by `var1=$(awk -F "=" '/ssh.user/ {print $2}' configurationfile)`. But honestly, I seriously suspect you're just making your life more complicated than it should be, why not just store the configuration variables each in a separate shell variable and use these, as I showed? – fvu May 08 '15 at 11:31
  • You are right fvu,bt my prob. is that ,in my .conf file the data is like this as I mentioned above..So I want to seperate that in one variable like ssh.user in var1 and then var1=root – akshay May 09 '15 at 07:43
  • Well, that is exactly what I explain in my last comment, so your problem is solved, isn't it? Did you actually try to execute these 2 commands? – fvu May 09 '15 at 11:36
  • This is fine by awk command.Bt I want to generalize this all.No one can see what I am use in this variable or backend coding.I want to check that ssh.user line by line in my .conf file and then when this find out then assign root to new variable. – akshay May 09 '15 at 12:58
  • Another way I want is, to cut this line means left side of this '=' stored in one variable and right side of this '=' stored in one variable that means I want var1=ssh.user and var2=root and then I will possible to check if var1==var2 then echo "DONE"....thats it. Please help me for that. – akshay May 09 '15 at 13:01
  • Please answer me for m question. – akshay May 11 '15 at 11:55