0

I try to use the values of my config file into my .sh file but it seems it's considered as command.

Here is my test_config.sh:

source pathTo/config.cnf
echo "Name=$user"
echo "Surname=$host"

config.cnf

[client]
user = root
password = pwd
host = localhost

The error appeared is:

 $ ./test_config.sh

config.cnf: line 1: user: command not found
config.cnf: line 2: password: command not found
config.cnf: line 3: host: command not found

melpomene
  • 84,125
  • 8
  • 85
  • 148
zackzulg
  • 599
  • 2
  • 5
  • 21
  • What tool are you expecting to read your config file? You need to run that tool, not source. – Gem Taylor Aug 09 '18 at 10:10
  • I didn't understand your question Gem. Can you please give me more explication ? – zackzulg Aug 09 '18 at 10:30
  • As @Matteo says, bash does not understand .inf files. Specific applications understand specific .inf files. You didn't really say why you wanted to run this .inf file, so I suggested you run the application. I don't think that is what you wanted. – Gem Taylor Aug 09 '18 at 11:03
  • Related: https://stackoverflow.com/questions/25290018/how-to-read-a-value-from-cnf-file?s=1|101.9239 – Barmar Aug 09 '18 at 11:31
  • if you want to `.` source the file, you'd have to get rid of those `[section]` headers and replace `=` with `=` – Martin Zeitler Aug 09 '18 at 12:56
  • Thanks Gem for your reply, so what I want to do is to use parameters (user, password, host) into my sh file to avoid writing the same values in many sh files in the future I prefer to have one config file that contains my parameters and use them wherever I want. – zackzulg Aug 09 '18 at 16:01
  • @MartinZeitler there is spaces between arguments and values. I also try to replace source by . / but it doesn't work too. – zackzulg Aug 09 '18 at 16:03
  • @zackzulg the below answer merely does what I've suggested... here is a better matching dupe: https://stackoverflow.com/questions/6318809/how-do-i-grab-an-ini-value-within-a-shell-script ...including parsing the sections, hence what you suffixed `.cfg` there equals the classical `.ini` format. – Martin Zeitler Aug 09 '18 at 16:39
  • one can use just any scripting-language available to the command prompt, translating the input (as shown above) into the output (as shown below), in order to initialize the variables, by sourcing a shell-script - or other script. adding arguments to the shell-script and then using `$1`, `$2`, `$3` might be worth a consideration. – Martin Zeitler Aug 09 '18 at 16:48

1 Answers1

0

sourceis a bash shell built-in command that executes the content of the file passed as argument, in the current shell. It has a synonym in . (period).

This means that pathTo/config.cnf is executed as a bash script.

You could rewrite your configuration file as a shell script

# client
user=root
password=pwd
host=localhost

and then source it

Matteo
  • 14,696
  • 9
  • 68
  • 106
  • Thanks Matteo for your explications, but the config file is used in many places in project so is there a way to use parameters into my sh file ? – zackzulg Aug 09 '18 at 10:38
  • `bash` does not understand this format and you cannot use it directly. You will have either to convert it or to write some routines to parse it. – Matteo Aug 09 '18 at 10:39
  • Ok thank you @Matteo – zackzulg Aug 09 '18 at 10:40