Created
August 25, 2016 11:12
-
-
Save tarex/ef8932c6d80a07e921545278ca41c2ad to your computer and use it in GitHub Desktop.
Shell script to replace variables docker-compose.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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