This is a Bash script that runs commands which builds code using the parameters given.
Code
The task:
I have multiple instances of websites which need to be build individually using individual yarn commands and then copied out of a build folder and placed into their own individual folder. I found this very repetitive and so I wanted to solve the problem using a script. I thought about using Python but I ended up using Bash.
Pseudocode:
Inputs: folder where the code is built, folder where the output code is stored, list of languages to run yarn build on.
For each language in input:
Go to project/build
Run "yarn build_language"
Delete output/language folder
Copy project/build to output/language folder
Here is my Bash implementation:
#!/bin/bash
# PROC Build and Copy:
# Args:
# 1) Build directory
# 2) Output directory path (general)
# 3) List of language names
# For each lang in list of language names:
# Run appropriate build command
# Delete output directory
# Copy build directory to output directory/lang
if (( $# < 3 )); then
echo "Please use at least 3 parameters"
exit 2
fi
array=("$@")
readonly build_path=$1
readonly output_path=$2
languages=("${array[@]:2}")
build_command="yarn build_"
for lang in "${languages[@]}"
do
cd "$build_path" || exit
eval "${build_command}${lang}"
cd ~- || exit
output=${output_path}${lang}
rm -rf "$output"
mkdir -p "$output"
cp -r "$build_path" "$output"
done
Sample usage: ./build_all.sh my_project/build/. environments/ python lua
Discussion
I found a style guide on Github and tried to follow it but this is my first Bash script so I am not sure if I followed a good style. I am a Python developer and I fear I may have written a Python-script in Bash. I am also not sure if Bash is the best way to implement this type of script. I wanted to make this re-usable by pulling the directories out into parameters, but I decided not to have the yarn command as a parameter as I thought it could be confusing having lots of parameters. Are my assumptions justified, or do they make the code confusing?