Home | HTML | JavaScript | DOM | Data Types |
%%script bash
# This example has error in VSCode, it run best on Jupyter
cd /tmp
file="sample.md"
if [ -f "$file" ]; then
rm $file
fi
tee -a $file >/dev/null <<EOF
# Show Generated Markdown
This introductory paragraph and this line and the title above are generated using tee with the standard input (<<) redirection operator.
- This bulleted element is still part of the tee body.
EOF
echo "- This bulleted element and lines below are generated using echo with standard output (>>) redirection operator." >> $file
echo "- The list definition, as is, is using space to seperate lines. Thus the use of commas and hyphens in output." >> $file
actions=("ls,list-directory" "cd,change-directory" "pwd,present-working-directory" "if-then-fi,test-condition" "env,bash-environment-variables" "cat,view-file-contents" "tee,write-to-output" "echo,display-content-of-string" "echo_text_>\$file,write-content-to-file" "echo_text_>>\$file,append-content-to-file")
for action in ${actions[@]}; do # for loop is very similar to other language, though [@], semi-colon, do are new
action=${action//-/ } # convert dash to space
action=${action//,/: } # convert comma to colon
action=${action//_text_/ \"sample text\" } # convert _text_ to sample text, note escape character \ to avoid "" having meaning
echo " - ${action//-/ }" >> $file # echo is redirected to file with >>
done
echo ""
echo "File listing and status"
ls -l $file # list file
wc $file # show words
mdless $file # this requires installation, but renders markown from terminal
rm $file # clean up termporary file
- This Bash script generates a Markdown file (‘sample.md’) containing text and formatted lists, then displays some information about the file.
Step by Step breakdown:
- File Cleanup Check: The script checks if a file named sample.md exists. If it does, it deletes it to start fresh.
- Appending Text Using tee: It’s like typing text into a document. The script adds lines of text to a file called sample.md. These lines include a title and a list of items.
- Appending Text Using echo:Similar to writing more lines in the document. More lines are added to the sample.md file, extending the list of items.
- Loop to Generate Actions: Imagine making a list of actions you can do. The script creates a list of actions and descriptions, like “ls: list-directory.” It adds these actions to the list in the sample.md file.
- Display File Listing and Word Count: Like checking the properties of a file. The script shows you details about the sample.md file, such as its size and the number of words in it.
- Render Markdown Using mdless (Optional): This part is like having a program that can display how the document looks. It tries to show you how the Markdown document would appear if it were formatted nicely. But you might need to install something for this to work.
-
Cleanup: Cleaning up is like throwing away rough notes after you’ve finished using them. The script deletes the temporary sample.md file because it’s done using it.
- To sum it up in simpler terms, this script creates a text document, adds some content to it, shows you information about the document, tries to display it nicely (if possible), and then cleans up by deleting the document when it’s done.