pharma_joe
pharma_joe

Reputation: 631

How to parse variables from a parameter file in a K Shell script

I have a shell script I wish to read parameters from an external file, to get files via FTP:

parameters.txt:

FTP_SERVER=ftpserer.foo.org
FTP_USER_NAME=user
FTP_USER_PASSWORD=pass
FTP_SOURCE_DIRECTORY="/data/secondary/"
FTP_FILE_NAME="core.lst"

I cannot find how to read these variables into my FTP_GET.sh script, I have tried using read but it just echoed the vars and doesn't store them as required.

Upvotes: 0

Views: 1409

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 754710

Assuming that 'K Shell' is Korn Shell, and that you are willing to trust the contents of the file, then you can use the dot command '.':

. parameters.txt

This will read and interpret the file in the current shell. The feature has been in Bourne shell since it was first released, and is in the Korn Shell and Bash too. The C Shell equivalent is source, which Bash also treats as a synonym for dot.

If you don't trust the file then you can read the values with read, validate the values, and then use eval to set the variables:

 while read line
 do
     # Check - which is HARD!
     eval $line
 done

Upvotes: 1

Related Questions