Reputation: 83
I have a file like
name1=value1
name2=value2
I need to read this file using shell script and set variables
$name1=value1
$name2=value2
Please provide a script that can do this.
I tried the first answer below, i.e. sourcing the properties file but I'm getting a problem if the value contains spaces. It gets interpreted as a new command after the space. How can I get it to work in the presence of spaces?
Upvotes: 35
Views: 68959
Reputation: 11
filename="config.properties"
# Read the file and extract key-value pairs
while IFS='=' read -r key value; do
# Skip empty lines or lines starting with #
if [[ -z $key || $key == \#* ]]; then
continue
fi
# Trim leading/trailing whitespace from key
key=$(echo "$key" | awk '{gsub(/^ +| +$/,"")} {print $0}')
# Extract the value after the first equals sign
value="${line#*=}"
# Assign value to the variable dynamically
declare "$key"="$value"
done < "$filename"
# Print the variables
echo "key1: $key1"
echo "key2: $key2"
echo "key3: $key3"
Upvotes: 1
Reputation: 959
dotenv support for shell and POSIX-compliant .env syntax specification https://github.com/ko1nksm/shdotenv
Usage: shdotenv [OPTION]... [--] [COMMAND [ARG]...]
-d, --dialect DIALECT Specify the .env dialect [default: posix]
(posix, ruby, node, python, php, go, rust, docker)
-s, --shell SHELL Output in the specified shell format [default: posix]
(posix, fish)
-e, --env ENV_PATH Location of the .env file [default: .env]
Multiple -e options are allowed
-o, --overload Overload predefined environment variables
-n, --noexport Do not export keys without export prefix
-g, --grep PATTERN Output only those that match the regexp pattern
-k, --keyonly Output only variable names
-q, --quiet Suppress all output
-v, --version Show the version and exit
-h, --help Show this message and exit
Load the .env file into your shell script.
eval "$(shdotenv [OPTION]...)"
Upvotes: 0
Reputation: 335
if your file location is /location/to/file
and the key is mykey
:
grep mykey $"/location/to/file" | awk -F= '{print $2}'
Upvotes: 20
Reputation: 5382
Improved version of @robinst
read_properties()
{
file="$1"
while IFS="=" read -r key value; do
case "$key" in
'#'*) ;;
*)
eval "$key=\"$value\""
esac
done < "$file"
}
Changes:
A nice one is also the solution of @kurumi, but it isn't supported in busybox
And here a completely different variant:
eval "`sed -r -e "s/'/'\\"'\\"'/g" -e "s/^(.+)=(.+)\$/\1='\2'/" $filename`"
(i tried to do best with escaping, but I'm not sure if that's enough)
Upvotes: 7
Reputation: 31427
Sourcing the file using .
or source
has the problem that you can also put commands in there that are executed. If the input is not absolutely trusted, that's a problem (hello rm -rf /
).
You can use read
to read key value pairs like this if there's only a limited known amount of keys:
read_properties()
{
file="$1"
while IFS="=" read -r key value; do
case "$key" in
"name1") name1="$value" ;;
"name2") name2="$value" ;;
esac
done < "$file"
}
Upvotes: 21
Reputation: 9174
suppose the name of your file is some.properties
#!/bin/sh
# Sample shell script to read and act on properties
# source the properties:
. some.properties
# Then reference then:
echo "name1 is $name1 and name2 is $name2"
Upvotes: 6
Reputation: 308141
If all lines in the input file are of this format, then simply sourcing it will set the variables:
source nameOfFileWithKeyValuePairs
or
. nameOfFileWithKeyValuePairs
Upvotes: 38