I am trying to write a Bash script that will process all files in a folder structure that match a pattern (e.g. *.properties or *.conf). My use case is replacing a server name (server-xx) with another server name (server-yy) in a myriad of application configuration files.
Basically, it's all about finding all files in the folder structure starting from a root, and applying the same processing to all files.
I'm slowly building the script and it's getting much more difficult than my initial intention hinted to. It has four parameters:
- root path
- file pattern
- string to replace
- replacement string
Here it is in its current state:
# The echoing of usage notes is omitted here.
nbArgs=$#
if [[ $nbArgs -ne 4 ]]
then
    echo "Four parameters are required."
else
    echo "Running..."
    filelist=`find $1 -name "$2" 2> /dev/null`
    printf "Files: $filelist"
    echo
    while read filename ; do
    if [[ -e $filename ]]; then
        line="sed -i 's/$3/$4/g' $filename"
        echo "Executing $line"
    fi
    done <<< "$filelist"
    echo "Done. Exiting."
fi
Example run: ./adapt-properties-files.sh /mnt/c/Users/avoglozin/Apps/test-adapt/ "*.properties" server-xx.domain.com server-yy.domain.com
The target "production" environment for this script is an Ubuntu proper machine, but I'm using the Ubuntu Bash under Windows 10 for development.
The problem is that when I run the find command, e.g. find ./test-adapt/ -name "*.properties" 2> /dev/null in the shell, I get the correct list of files shown in the terminal. However, when I run the script, the filelist variable shows only one file. Some possibly obvious piece of knowledge is evading me.
 
     
    