Execute commands written in a README.md file
I recently tried to directly execute the commands present in the README.md file of my projects. 2 This way of functioning has indeed a double advantage:
- Avoiding data redundancy
- Constant updating of the last orders
I also tried to be able to directly select and execute extracted commands thanks to the excellent plug-in [fzf] (https://github.com/junegunn/fzf). The goal was to make the simplest, using if possible the tools provided by GNU core utilities. Here is the solution I arrived at:
findProjectsCommands()
{
LINES=$(sed '/^```$/,/^```$/{//!b};d' README.md 2>&1);
if [ $? -eq 2 ]; then echo "README.md not found"; return; fi
if [ $? -ne 0 ]; then echo "$LINES"; return; fi
COMMAND=$(echo "$LINES" | fzf -i);
if [ $? -ne 0 ]; then return; fi
# Stripping trailing spaces
COMMAND=$(echo -e "${COMMAND}" | sed -e 's/^[[:space:]]*//')
echo -n $COMMAND | xclip -selection c -i ;
history -s "$COMMAND";
echo "$COMMAND";
eval $COMMAND
}
findLocalCommands=findProjectsCommands;
Note that the last line simply assigns the function to an alias.
Just put this block of code in a file read at launch terminal (.bashrc for example) to enjoy it.