Skip to content

Instantly share code, notes, and snippets.

@arvind-india
Created October 11, 2017 05:35
Show Gist options
  • Save arvind-india/132febe9b6897f33a188b405dd95f5ff to your computer and use it in GitHub Desktop.
Save arvind-india/132febe9b6897f33a188b405dd95f5ff to your computer and use it in GitHub Desktop.
Shell script to replace variables docker-compose.yml
#!/bin/bash
set -eu
main() {
local args=("$@")
local inFile=$(pwd)/docker-compose.yml
local outFile=$(pwd)/$1
if [[ $# -lt 2 ]]; then
usage
fi
cp $inFile $outFile
for ((i=1; i<${#args[@]}; i++)); do
local pair=(${args[i]//=/ }) # tokenize by =
pair[1]="${pair[1]//\//\\/}" # escape paths
sed -i -e "s/\${${pair[0]}}/${pair[1]}/g" $outFile
echo "Replacing: $i \${${pair[0]}} with ${pair[1]}"
done
}
usage(){
echo "replace-var 1.0"
echo "Substitutes variables of type \${VAR1} in docker-compose.yml and writes them into the file defined as the first argument"
echo "Usage: replace-var result.yml VAR1=value1 VAR2=value2 ..."
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment