This is my script for running commands from specific codeblock in .md file.
Usage
- Change
HEAD
variable to unique string that is one line above codeblock you want to run. - Change
FILE
variable to path of file that contains this codeblock. - Execute provided command.
Warnings
- Do NOT run commands you don’t understand in your shell.
- I have tested only things present in my [[#Test File]]
Command
CMD=$(HEAD="## Script 2"; FILE="$HOME/path/to/file.md"; sed -n "/^${HEAD}/,/\`\`\`$/ p" < $FILE | sed "/^\`\`\`/ d;/${HEAD}/ d"); echo; echo "$CMD" ; echo; echo "Do you want to run this command? (Enter) - (^C to abort)" ; read ; eval "$CMD"
Example
- Running bellow command on [[#Test File]] will print.
CMD=$(HEAD="## Script 2"; FILE="$HOME/path/to/Test File.md"; sed -n "/^${HEAD}/,/\`\`\`$/ p" < $FILE | sed "/^\`\`\`/ d;/${HEAD}/ d"); echo; echo "$CMD" ; echo; echo "Do you want to run this command? (Enter) - (^C to abort)" ; read ; eval "$CMD"
Output:
echo "Test 2 Success"
# Check if comments work fine
echo "Test 3 Success"
function test_function1() {
echo "Test 4 Success"
}
test_function1
function test_function2() {
echo "Test 5 Fail"
}
sudo ls /root
Do you want to run this command? (Enter) - (^C to abort)
Test 2 Success
Test 3 Success
Test 4 Success
[sudo] password for vicioussquid:
kernel.log
Test File
## Script 1
echo "Mega Fail"
```sh
echo "Test 1 Failed"
```
## Script 2
```sh
echo "Test 2 Success"
# Check if comments work fine
echo "Test 3 Success"
function test_function1() {
echo "Test 4 Success"
}
test_function1
function test_function2() {
echo "Test 5 Fail"
}
test_quotes="/tmp/File with spaces.txt"
touch "$test_quotes"
cat "$test_quotes"
sudo ls /root
```
## Script 3
echo "Mega Fail"
```sh
echo "Test 6 Fail"
```