0

I need to read environment variables from app.config file using shell script and need to set in constant.cs file.

Here is link which I am following :

https://github.com/microsoft/appcenter/blob/master/sample-build-scripts/xamarin/app-constants/appcenter-pre-build.sh

I have app.config file from xamarin.form in following format :

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
    <add key="appId" value="12974" />
    <add key="url" value="https://abc.xyz.com" />
</appSettings>

Here is code which I am using to read config.file and set to constant.cs file. But I ma not sure which one will work.

if [ -e "$APP_CONSTANT_FILE" ]
then
    if [ -e "$ENV_FILE" ]
    then
        echo "Both Config files are available"
        #name=applicationID
        #$ awk -F\" -v n="$name" '/<ATTRIBUTE NAME="/ && $2==n {print $4}' data 
        #awk -F"\"" ' /AppId/ {print $4}' = -F"\"" ' /appID/ {print $4}' $APP_CONSTANT_FILE $ENV_FILE
        awk 'FNR==NR{a[FNR]=$0;next}{$NF=gensub(/value=".*"\/>/,"value=\""a[FNR]"\"\/>","g",$NF);print}' $APP_CONSTANT_FILE $ENV_FILE
        #sed -i '' 's#ApiUrl = "[-A-Za-z0-9:_./]*"#ApiUrl = "'$API_URL'"#' $APP_CONSTANT_FILE

        echo "File content:"
        cat $APP_CONSTANT_FILE
    else
        echo "Can not locate $ENV_FILE file"
        exit
    fi
else
    echo "Can not locate $APP_CONSTANT_FILE file"
    exit
fi

Please provide me reference or hint to read value by key and set it to .cs file

Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160
  • 3
    You should provide more info about output .cs file structure. Also a broader picture why you need to do such a strange transformation would be handy I guess. In C# you can easily access app.config keys :-). So why to set it somehow into .cs file ... – Honza P. Jun 13 '19 at 12:48
  • If you're going ot be doing this more than one time, you'll have to spend the time to master `xmllint` or another `xml` aware tool. They are very picky, so be prepared to spend more than a 1/2 hour learning to use. Good luck. – shellter Jun 13 '19 at 12:50
  • 1
    Shell is not a good language for processing XML, or conversely, XML is not a good choice for configuring a shell script. – chepner Jun 13 '19 at 13:31
  • What chepner said. But you will need external tools so why not use Python to parse the XML and return it as something that the shell can iterate? Take a look at [getopt](https://stackoverflow.com/questions/16483119/an-example-of-how-to-use-getopts-in-bash) and perhaps adjust that idea to parsing your XML? – Jens Jun 15 '19 at 12:38
  • See: https://stackoverflow.com/questions/6751105/why-its-not-possible-to-use-regex-to-parse-html-xml-a-formal-explanation-in-la – Cyrus Jun 15 '19 at 16:46
  • [edit] your question to show the expected output given your posted sample input. Clarify which file is APP_CONSTANT_FILE and which ENV_FILE – Ed Morton Jun 17 '19 at 22:26

2 Answers2

2

You didn't provide the format of the file constant.cs but you can start with this one liner:

cat config.xml | grep '<add ' | sed -E 's/.*key="?([^ "]*)"? value=([^ ]*) .*/\1 = \2;/'

The output will be:

appId = "12974";
url = "https://abc.xyz.com";

If you want a different output, just change this part of the code: \1 = \2; where \1 is the key and \2 is the value.

rom
  • 1,009
  • 9
  • 16
0

As pointed out in the comments, it is a bad idea to parse generic XML with bash scripts. Also, you didn't specify the output format, which might help to provide better alternatives.

Anyway, if you're absolutely sure that the input file is always as pretty-printed as in your example, and always uses double quotes, and never contains XML-escaped characters in the keys and values, and <appSettings> is the only place where <add> elements appear, and a bunch of other ifs, here's a bash loop that extracts the keys and values:

while read addkeyval; do
    key=$(echo "$addkeyval" | sed 's#.* key="\([^"]*\)".*#\1#')
    value=$(echo "$addkeyval" | sed 's#.* value="\([^"]*\)".*#\1#')
    echo "key='$key' value='$value'"
done < <(grep "<add " "$ENV_FILE")

This code is inefficient, and fragile, and I'm not going to explain it. It's up to you whether you want to figure out how to integrate it into your script, or whether you'd rather spend the time on following some of the good advice in the comments. I was just in the mood for a little bash programming exercise ;-)

Roland Weber
  • 3,395
  • 12
  • 26