Skip to content

Instantly share code, notes, and snippets.

@ppcamp
Created May 30, 2024 15:17
Show Gist options
  • Save ppcamp/dfeae9a01e88ced62d954446a9148fbf to your computer and use it in GitHub Desktop.
Save ppcamp/dfeae9a01e88ced62d954446a9148fbf to your computer and use it in GitHub Desktop.
Bash simple run script
#!/bin/bash
PROGRAM=DEFAULT
FOLDER_TO_EXECUTE="$HOME/$PROGRAM"
CURR_FOLDER="$(pwd)"
# How to run
#
# 1. Copy this script to /usr/bin
# sudo cp output.sh /usr/bin/genoutput
# 2. Give execute permissions
# sudo chmod u+x /usr/bin/genoutput
# 3. Restart your bash terminal, and call genoutput
if [ $# -eq 0 ]; then
echo "No arguments provided"
helper
exit 1
fi
if ! [[ "$CURR_FOLDER" == "$FOLDER_TO_EXECUTE" ]]; then
printf "Current folder (%s) is not $FOLDER_TO_EXECUTE\n" "$CURR_FOLDER"
echo "This script can be run only inside of $DEFAULT folder"
exit 1
fi
SPACE="\n\n\n\n\n\n"
LINE=`printf '_%.0s' {1..80}`
function helper {
printf "Usage: output.sh <build_command> <run_command>\n"
printf "Example: output.sh \"make build\" \"make run\"\n"
}
function space {
printf "$SPACE"
}
function running {
printf "Running: %s\n" "$1"
printf "$LINE$SPACE"
}
function run {
# Steps are run in background and the output is piped to a file
(
running "$1";\
$1;\
printf $SPACE;\
running "$2";\
$2\
) | tee /mnt/d/make_output
}
# Switch case statement
case "$1" in
"api")
run "make" "make test"
;;
"app")
run "make build" "make build_app"
;;
*)
helper
;;
esac
# (make ; printf '=%.0s' {1..100}; printf '\n'; make test) | tee /mnt/d/make_output
# (make build ; printf '\n\n\n'; printf '_%.0s' {1..100}; printf '\n\n\n'; make build_app) | tee /mnt/d/make_output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment