Change Environment File Using Shell Script.

Pasindu Thejan
3 min readOct 24, 2020

Think you have a project with huge number of configuration files. Also you have different configurations files for different environment like “production(live)”, “test-bed”, “development”. Then time to time you required to change those configurations. Yes of course, you can change it manually. But if you have 100… files. Then it easy to write script to change that file.

Think you have front-end project which have different configs like “database path”, “api base url”, “api keys”, etc… Then this configs are lie on environment. As a example,

on development environment (config.yml)
on test-bed environment (config.yml)

when you deploy project into test-bed, you need change this development config to test-bed config. The easiest way is, create configurations directory and your project configure with that directory.

directory structure

In my project, normally what I’m doing is, create configurations directory and configure app with that directory. After that, only I need to change content of that directory when I need to change environment.

Lets Go To Work

lets start from very easy shell script and expand it …

when executing shell script we need to pass one argument that is name of the directory (live, dev, testbed).

Basic Shell Script

this script only copy two files to the configurations directory. lets look the code.

my_dir=“$1”

this code assign directory name to the variable which passed by user. If user pass “./change-environment.sh live” , Then live will be the my_dir value. Wait.., what happen If user wont pass the argument. Then following code will handle it.

if [ -z “$my_dir” ]
then
my_dir=“dev”
fi

If user wont pass any argument to the script, then it automatically assign “dev” directory to the my_dir. If there is no such a directory, then…

if [ -d $my_dir ]
then

else
echo “Directory not found..!”
fi

this will handle it. Now most Interesting part,

if [ -f “$my_dir/application.properties” ] && [ -f “$my_dir/db-config.yml” ]
then

else
echo “one or more file not found..!”
fi

this will check whether that config files are in the directory. If not, then give a error message.

`cp $my_dir/application.properties configurations`
`cp $my_dir/db-config.yml configurations`

this cp command copy config files From-Directory to To-Directory. In my case, live directory to configurations directory.

lets expand it …

Expanded Shell Script

In here, I have modified that code. This code can move any number of file into from-directory to to-directory. In here, I have used two functions called delete() and copy(). delete() function clean the configurations directory and copy() function copy file from live to configurations directory.

for file in configurations/*

for file in $my_dir/*

this two for use to iterate over files and

for file in configurations/*
do
`rm -r $file`
echo “$file deleted”
done

this for loop used to delete file from configurations directory,

for file in $my_dir/*
do
`cp $file configurations`
echo “$file copied”
done

this for loop used to copy files from From-directory to To-Directory.

you can clone full code in my github repo.

--

--