I have an .xcconfig file that I want to access via the terminal to access variables in the file. Is there a command or is there some way to do this? For example I have a variable called Build_Code = 1234. How would I access that?
Asked
Active
Viewed 260 times
1 Answers
2
Create a script to read the value of a variable.
Ex: .xconfig
var1 = value1
var2 = value2
get_value.bash
#!/bin/bash
#
# get_value.bash <file> <variable>
#
usage()
{
echo "Usage: get_value.bash <file> <variable>"
exit 1
}
#################################################
# Arguments
if [[ $# -eq 2 ]]
then
file="$1"
var="$2"
else
usage
fi
# Check if the file exists
if [[ ! -f "$file" ]]
then
echo "ERROR: file $file does not exist."
exit 2
fi
# Get the variable's value
grep -w "$var" "$file" | cut -d'=' -f2 | tr -d ' '
- This simple version assumes the format of the lines is
VARIABLE\s*=\s*VALUE
. - The
tr
is to remove spaces around the value. - The VALUE cannot contain spaces.
- The
<file>
argument could be hard coded if you will only ever check.xconfig
Many other solutions could be conceived, depending on the exact requirements, but this does the basic need you put in your question.

Nic3500
- 8,144
- 10
- 29
- 40