Created
June 29, 2020 18:05
-
-
Save danilol/de4291158147fe9442d86e703ada4e20 to your computer and use it in GitHub Desktop.
Script to note and run commands
This file contains 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
#!/bin/bash | |
#################### | |
### note and run ### | |
#################### | |
# This function saves commands to a file (notes.txt) in your current directory and run it after. | |
# It offers the possibility to only note or delete the notes.txt file. | |
# The motivation for this script appeared when creating multiple projects, | |
# I wanted to remember the commands/steps I used to generate/configure projects so it | |
# would be easier to document it later. | |
# | |
# How to: | |
# 1) Add this file to your $PATH: | |
# export PATH="$PATH:~/scripts" | |
# | |
# 2) In your terminal run: `note_n_run` and the script will wait for a command | |
# The script will create a file called `notes.txt` in your current directory | |
# and append the command you passed to the file. | |
# | |
# 3) Check the notes.txt file in your current directory =) | |
# | |
# The script accepts the following parameters: | |
# -n or --only-notes if you only want to note a command and not run it | |
# -d or --delete-notes if you want to remove the notes.txt file. | |
#!/bin/bash | |
note_n_run() { | |
# always note to this file in current directory | |
FILENAME="notes.txt" | |
FILE="$PWD/$FILENAME" | |
# -a only changes the "access" and "change" times in case file already exists | |
touch -a $FILENAME | |
while :; do | |
case $1 in | |
-n|--only) # Does not run the command, only create the note | |
arg_1="true" | |
;; | |
-d|--delete-note) # Delete note from current dir if it exists | |
echo "Removing file $FILE" && rm $FILE && return 0 | |
;; | |
-?*) | |
printf 'Invalid option: %s\n' "$1" >&2 | |
return 0 | |
;; | |
*) break | |
esac | |
shift | |
done | |
# just for clarity if it exists or not | |
if [ ! -f "$FILENAME" ]; then | |
echo "Creating file $FILENAME in $PWD" | |
else | |
echo "File '$FILENAME' exists" | |
fi | |
if [ "$arg_1" ]; then | |
echo "Only note option!" | |
# reads the console input | |
read input_command | |
# add the content to the file | |
echo $input_command >> $FILENAME | |
echo "Instruction noted!" | |
else | |
echo "Enter the cmd to run'n'note:" | |
# reads the console input | |
read input_command | |
# add the content to the file | |
echo $input_command >> $FILENAME | |
echo "Instruction noted!" | |
echo "Running cmd '$input_command'" | |
# runs the command | |
$input_command | |
fi | |
unset arg_1 | |
return 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment