Cor
Cor

Reputation: 35

bash read variable based on value of a default variable in config.ini file using source

I would like to read my config file and get the value of the default printer. So in this example I would expect to return the result Zebra_GK420D.

My config.ini file as follows:

enable_printing=yes
default_printer=printer1
printer1=Zebra_GK420D
printer2=DYMO_LabelWriter_4XL
create_proof=yes

I use the following bash script:

  1 #!/usr/bin/env bash
  2
  3 #Define filename
  4 fConfig=config.ini
  5 
  6 #If file exists, read in variables.
  7 if test -f $fConfig ; then
  8         source $fConfig
  9 fi
 10 
 11 echo The default_printer is: ${default_printer%?}
 12 
 13 echo The name of the default_printer is: $(${default_printer%?})

When I run the script, it returns:

The default_printer is: printer1
./test.sh: line 13: printer1: command not found

How can I fix my bash script so that it returns the following:

The default_printer is: printer1
The name of the default_printer is: Zebra_GK420D

As a side note, since the config.ini file is hosted on a Windows drive each line is returned with \r at the end, so I'm using the %? to remove the last character of each line. The danger here is that the final line in the file does not have \r at the end. What can I use instead of %? to remove only \r as opposed to blindly removing the last character?

Thank you.

Upvotes: 0

Views: 219

Answers (1)

Ljm Dullaart
Ljm Dullaart

Reputation: 4979

The answer is in the error message: command not found. Look at your line 13:

echo The name of the default_printer is: $(${default_printer%?})

$(command) is used in bash to get the output of a command. So you explicitly asked to execute the printer name as a command, and, predictably, the command does not exist. Try:

echo The name of the default_printer is: ${default_printer%?}

Upvotes: 1

Related Questions