HPR3423: "upg.sh" my "dump.txt" to "note.md"




Hacker Public Radio show

Summary: upg.sh my dump.txt to note.md SYNOPSIS: upg.sh Upgrade your system and store stdout into a markdown file. #!/bin/bash # upg.sh FILENAME=sys-upgrade$(date +%m-%d-%Y).md DIRECTORY="${HOME}/Documents/" # step 1: formatting. echo -e "# **System Upgrade:** $(date)n" | tee -a ${DIRECTORY}${FILENAME} echo -e "**Command:** `sudo apt-get update; sudo apt-get upgrade --yes`n" | tee -a ${DIRECTORY}${FILENAME} echo -e "**Command Breakdown:**" | tee -a ${DIRECTORY}${FILENAME} echo -e "- `sudo`, Admin Privilages." | tee -a ${DIRECTORY}${FILENAME} echo -e "- `apt-get`, Package Manager." | tee -a ${DIRECTORY}${FILENAME} echo -e "- `update;`, Package Manager's task; update the system software repositories." | tee -a ${DIRECTORY}${FILENAME} echo -e "- `sudo apt-get upgrade`, Perform system upgrade with updated repositories." | tee -a ${DIRECTORY}${FILENAME} echo -e "- `--yes`, Answers yes to the prompt." | tee -a ${DIRECTORY}${FILENAME} # step 2: run commands with formatting. echo -e "n**Command std-output:**n" | tee -a ${DIRECTORY}${FILENAME} echo -e "```" | tee -a ${DIRECTORY}${FILENAME} echo $(date) | tee -a ${DIRECTORY}${FILENAME} sudo apt-get update | tee -a ${DIRECTORY}${FILENAME} echo -e "n# System update completed.n" | tee -a ${DIRECTORY}${FILENAME} sudo apt-get upgrade --yes | tee -a ${DIRECTORY}${FILENAME} echo -e "n# System upgrade completed.n" | tee -a ${DIRECTORY}${FILENAME} echo -e "```n" | tee -a ${DIRECTORY}${FILENAME} # step 3: additional details with more formatting. echo -e "**Upgraded Package Details:**n" | tee -a ${DIRECTORY}${FILENAME} echo -e "```" | tee -a ${DIRECTORY}${FILENAME} PKGLIST=$(sed -n "/The following packages will be upgraded:/,/^.. upgraded/p" ${FILENAME} | sed '1d;$d' | xargs -n 1 | sed '/:i386$/d') PKGCACHE=$(echo -e "${PKGLIST}n" | xargs -n1 -I _ apt-cache search _) echo "${PKGCACHE}" > ${DIRECTORY}delete.txt echo "${PKGLIST}" | xargs -n 1 -I _ echo "sed -n '/^_ /p'" "${DIRECTORY}delete.txt" | bash | tee -a ${DIRECTORY}${FILENAME}; echo -e "```" | tee -a ${DIRECTORY}${FILENAME} rm -v ${DIRECTORY}delete.txt; PKGLIST= PKGCACHE= # step 4: place EOF (end of file). sed -i '/EOF/d' ${DIRECTORY}${FILENAME} echo "EOF" >> ${DIRECTORY}${FILENAME} #EOF Script breakdown: upg.sh First, we declare bash as our shell with #!/bin/bash. We could also use #!/bin/sh for a more portable script. I like to paste the name of the script we're working on into the script itself # upg.sh. Setup a couple of variables to shorten the syntax. FILENAME=sys-upgrade$(date +%m-%d-%Y).md DIRECTORY="${HOME}/Documents/" # step 1: formatting. Build labels and a short breakdown of the update/upgrade commands used. echo -e "# **System Upgrade:** $(date)n" <-- formatting: label with date. | tee -a ${DIRECTORY}${FILENAME} <-- path/to/file echo -e "**Command:** `sudo apt-get update; sudo apt-get upgrade --yes`n" <-- formatting: command label. | tee -a ${DIRECTORY}${FILENAME}