Created
January 7, 2020 12:32
-
-
Save f-steff/1c1fd5be8911feea835701b659121118 to your computer and use it in GitHub Desktop.
Bash script to replace multiple predefined variables in a template file to create a new file. Test files included.
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 | |
| echo "Replacing variables in file." | |
| InFile="${1:-Test.txt}" | |
| InFileName="${InFile%.*}" | |
| InFileExt=${InFile#"$InFileName"} | |
| OutFile=${2:-"${InFileName}_out${InFileExt}"} | |
| InVar=${3:-VarValues.txt} | |
| if [ ! -f $InFile ] || [ ! -f $InVar ] ; then | |
| [ ! -f $InFile ] && echo "Error: InFile '$InFile' not found!" | |
| [ ! -f $InVar ] && echo "Error: VarFile '$InVar' not found!" | |
| echo "" | |
| echo " Usage: ${0} InFile [ OutFile [VarFile] ]" | |
| echo " InFile defaults to 'Test.txt'" | |
| echo " OutFile defaults to appends '_out' to Infile, like this 'Test_old.txt'" | |
| echo " VarFile defaults to 'VarValues.txt'" | |
| echo " VarFile must contain set of 'CurrentVar=NewValue', #comments are allowed" | |
| echo " InFile must contain '$CurrentVar$' strings, which will be substituted with 'NewValue' strings" | |
| echo " Suggested usage:" | |
| echo " ${0} version.tbl version.h version.vars" | |
| exit 1 | |
| fi | |
| # Cleanup | |
| rm -f ${OutFile} | |
| # Import known variables | |
| VarCounter=0 | |
| while IFS= read -r line || [ -n "$line" ]; do | |
| var=${line%%=*} | |
| val=${line#*=} | |
| if [[ ${var} == \#* ]]; then | |
| # Skip comments. | |
| continue | |
| fi | |
| if [[ -n $val ]]; then # Variable Value must be non-empty. | |
| #Split line up into two variable arrays. | |
| eval ReplaceVar[VarCounter]="${var}" | |
| eval ReplaceVal[VarCounter]="${val}" | |
| fi | |
| VarCounter=$((VarCounter+1)) | |
| done < ${InVar} | |
| echo "Read $((VarCounter)) variables" | |
| # Copy source to destination if no variables were found to replace. | |
| if [[ ${VarCounter} == 0 ]]; then | |
| # No variables to replace. | |
| cp ${InFile} ${OutFile} | |
| echo "Copied ${InFile} to ${OutFile}" | |
| echo "Done" | |
| exit 0 | |
| fi | |
| # Read input, replacing the known variables, and output to OutFile | |
| LineCounter=0 | |
| while IFS= read -r line || [ -n "$line" ]; do | |
| for i in "${!ReplaceVar[@]}"; do | |
| line=${line//\$${ReplaceVar[i]}\$/${ReplaceVal[i]}} | |
| done | |
| echo "${line}" >> ${OutFile} | |
| # echo "${LineCounter}:${line}" | |
| LineCounter=$((LineCounter+1)) | |
| done < ${InFile} | |
| echo "Processed ${LineCounter} lines in ${InFile}, and created ${OutFile}." | |
| echo "Done." | |
| exit 0 |
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
| dsxzczxc $test1$ lasdfhlashdklas <aLÆSKJLÆSAHJ | |
| $test2$ lasdfhlashdklas >aLÆSKJLÆSAHJ | |
| lasdfhlashdklas >aLÆSKJLÆSAHJ$test3$$lasdfhlashdklas >aLÆSKJLÆSAHJ | |
| lasdfhlashdklas >aLÆSKJLÆSAHJ$test4$ |
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
| dsxzczxc _____test1_string___ lasdfhlashdklas <aLÆSKJLÆSAHJ | |
| _____test2_string___ lasdfhlashdklas >aLÆSKJLÆSAHJ | |
| lasdfhlashdklas >aLÆSKJLÆSAHJ_____test3_string___$lasdfhlashdklas >aLÆSKJLÆSAHJ | |
| lasdfhlashdklas >aLÆSKJLÆSAHJ_____test4_string___ |
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
| #List of variables to replace. | |
| test1=_____test_one_string___ | |
| test2=_____test_two_string___ | |
| test3=_____test_three_string___ | |
| test4=_____test_four_string___ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment