Last active
September 29, 2022 00:50
-
-
Save Timothee/71e653a6a8e18ae17fb7 to your computer and use it in GitHub Desktop.
This function lets you easily create series of commands into a single call # It will print each successive command, run it and, as long as it returned # with code 0, continue to the next step. # If any step fails, it will stop and let you pick it back up after you fix the # issues.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# This function lets you easily create series of commands into a single call | |
# It will print each successive command, run it and, as long as it returned | |
# with code 0, continue to the next step. | |
# If any step fails, it will stop and let you pick it back up after you fix the | |
# issues. | |
# | |
# Example of script using this function: | |
# !/usr/bin/env bash | |
# | |
# source command_runner | |
# | |
# foo () { | |
# cat <<-EOL | |
# ls | |
# cd ~ | |
# # You can have comments too | |
# ls | |
# # this will fail and not continue forward | |
# cd notafolder | |
# ls | |
# EOL | |
# } | |
# | |
# run_commands foo | |
# | |
# There are some limitations to the commands you can run and you might | |
# sometimes need to put them inside function instead. | |
source colors.sh | |
run_commands () { | |
skip=0 | |
keepRunning=1 | |
list=0 | |
# You can pass a second argument to skip the given number of steps | |
# e.g. passing 2 will skip all the commands until the one labeled 2 | |
# comments are not counted in the number of commands | |
# Skipped commands are still printed for information | |
if [ -n "$2" ] && [ $2 != "list" ]; then | |
skip=$2 | |
fi | |
# Check that command exists | |
if [[ -z "$(type -t $1)" ]]; then | |
echo "Command not found" | |
echo | |
help | |
exit 1 | |
fi | |
i=0 | |
# Lets you print out the plan | |
if [[ $2 == 'list' ]]; then | |
list=1 | |
fi | |
while read -r line; do | |
# Skip comments | |
if [[ ! $line == \#* ]]; then | |
if [ $skip -gt $i ]; then | |
echo -e "~ $i.$line" | |
elif [ $keepRunning == 1 ] && [ $list != 1 ]; then | |
echo -n -e " $i.${txtwht}${line}${txtrst}" | |
OUT=$($line 2>&1) | |
code=$? | |
if [ $code = 0 ]; then | |
erasePreviousLine | |
echo -e "${chkmrk} $i.${bldgrn}${line}${txtrst}" | |
else | |
erasePreviousLine | |
echo -e "${xmark} $i.${bldred}${line}${txtrst}" | |
failedCode=$code | |
failedCommand=$line | |
keepRunning=0 | |
fi | |
else | |
echo -e " $i.$line" | |
fi | |
i=$((i+1)) | |
fi | |
done < <(echo "$($1)") | |
if [ $keepRunning == 0 ] && [ -n $code ]; then | |
echo | |
echo -e "Command ${txtwht}${failedCommand}${txtrst} failed with exit code ${txtwht}${code}${txtrst}" | |
echo "Output:" | |
echo "$OUT" | |
echo | |
exit $code | |
fi | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Defines variables and functions for colors to be used in other scripts | |
erasePreviousLine () { | |
eraseline='\r\033[K' | |
echo -ne "$eraseline" | |
} | |
# Special characters | |
xmark="\xE2\x9C\x97" | |
chkmrk="\xE2\x9C\x93" | |
# Colors | |
endcolor='\033[0m' | |
white='\033[1;37m' | |
gray='\033[0;37m' | |
red='\033[1;31m' | |
green='\033[1;32m' | |
txtblk='\033[0;30m' # Black - Regular | |
txtred='\033[0;31m' # Red | |
txtgrn='\033[0;32m' # Green | |
txtylw='\033[0;33m' # Yellow | |
txtblu='\033[0;34m' # Blue | |
txtpur='\033[0;35m' # Purple | |
txtcyn='\033[0;36m' # Cyan | |
txtwht='\033[0;37m' # White | |
bldblk='\033[1;30m' # Black - Bold | |
bldred='\033[1;31m' # Red | |
bldgrn='\033[1;32m' # Green | |
bldylw='\033[1;33m' # Yellow | |
bldblu='\033[1;34m' # Blue | |
bldpur='\033[1;35m' # Purple | |
bldcyn='\033[1;36m' # Cyan | |
bldwht='\033[1;37m' # White | |
unkblk='\033[4;30m' # Black - Underline | |
undred='\033[4;31m' # Red | |
undgrn='\033[4;32m' # Green | |
undylw='\033[4;33m' # Yellow | |
undblu='\033[4;34m' # Blue | |
undpur='\033[4;35m' # Purple | |
undcyn='\033[4;36m' # Cyan | |
undwht='\033[4;37m' # White | |
bakblk='\033[40m' # Black - Background | |
bakred='\033[41m' # Red | |
bakgrn='\033[42m' # Green | |
bakylw='\033[43m' # Yellow | |
bakblu='\033[44m' # Blue | |
bakpur='\033[45m' # Purple | |
bakcyn='\033[46m' # Cyan | |
bakwht='\033[47m' # White | |
txtrst='\033[0m' # Text Reset | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment