Baby's first Bash script
Despite my saying I was too lazy to get into Bash scripting I actually moved away from Batch and rewrote the script I use to build and deploy this website:
#!/bin/bash
workDir="/path/to/working/directory"
hugoDir="/path/to/working/directory/hugo"
publicDir="/path/to/working/directory/public"
checkFail() {
if [ $? -eq 0 ]; then
echo -e "\u2713 OK"
else
echo -e "\u26a0 There was an error"
read -n1 -r -p "Press any key to exit..."
exit 1
fi
}
cd "$workDir" || exit
if [[ -d "$publicDir" ]]; then
echo -e "\u2605 Removing public folder"
rm -rf "public"
checkFail
fi
cd "$hugoDir" || exit
echo -e "\u2605 Building website"
hugo
checkFail
cd "$workDir" || exit
echo -e "\u2605 Applying Prettier to files"
npx prettier --write public
checkFail
read -r -p "Deploy website now? [Y/N] " input
case $input in
[yY][eE][sS]|[yY])
cd "$publicDir" || exit
neocities push .
checkFail
;;
[nN][oO]|[nN])
;;
*)
echo "Invalid input..."
exit 1
;;
esac
read -n1 -r -p "All done! Press any key to exit..."
exit 1
And the alias I use inside my Powershell configuration is now:
Function webBuild { bash -i -c 'cd "/path/to/script/"; ./build.sh;' }
Set-Alias -Name build -Value webBuild
Note: I learnt it the hard way that if you’re attempting
to run a Bash script on Windows while outside a WSL environment
(e.g. straight from Powershell) it’s very important to use
the flags -i -c
, otherwise you’ll keep
getting errors and hang-ups. I got this tip from
a Stack Overflow comment
after spending hours debugging 😬