Jump to content

Bash programming/Note taking

From Wikiversity

This is a productivity shell script that can be used for note taking into a text file. It automatically applies time stamps before each line. The text file can later be edited using a text editor.

It can be used on both Linux (on desktop and laptop computers) and Android-based smartphones using a terminal emulator software like the one by Jack Palevich or Termux. On Android, it can be downloaded from third-party sources.

It requires at least Android 7.0 given that sed was not included in Android before. If compatibility with Android 6.0 is necessary, all | sed pipes need to be removed from the code. They are used to replace for a tab separated value (TSV) layout where a comma is substituted with an indent character and two commas are substituted with one comma. This functionality is unavailable prior to Android 7.

In addition to the main note file "N.txt", it creates a raw file called "N.raw.txt" which contains unmodified user input and "N.debug.txt" which contains more technical information.

For statistical use, it also memorizes a total line count into a text file called ".lnid" (line ID) and a session count into ".sid" (session ID). The window also shows a line counter of the number of lines of the current sessions (lnc).

device_id="my_device_name"; # this helps distinguishing notes taken from different devices

function timestamp { date +%Y-%m-%d\ %H:%M:%S; }
lnc=1 # line counter for current session
lnid=1 # default
sid=1 # default

# usurp directories if they reserve that file name
if [ -d .lnid ]; then mv .lnid .lnid-usurped-$(timestamp)-$RANDOM; fi
if [ -d .sid ]; then mv .sid .sid-usurped-$(timestamp)-$RANDOM; fi

# create line and session counters if they don't exist
if [ ! -f .lnid ]; then echo 1 >>.lnid; fi
if [ ! -f .sid ]; then echo 1 >>.sid; fi

# load line counter and session counter
lnid=$(<.lnid)
sid=$(<.sid)

# count session
sid=$((sid+1))
echo -e $sid >.sid


x09=$(printf "\x09") # indent character for TSV format.
j_close=0 # instead of exit to prevent closing window if launched from . ./filename.sh instead of sh filename.sh .

while [ $j_close == 0 ] ; do
# prompt with colour codes - inverts colours for contrast.
printf "\e[47m\e[30m$(timestamp)|$lnc\e[00m "
read j_input

# log input to journal / note file
timestamp=$(timestamp) # storing timestamp in variable for efficiency and to prevent change during script
echo -e "$timestamp sid:$sid lnid:$lnid lnc:$lnc device_id:$device_id j_input:$j_input" >>N.debug.txt # contains more details
echo -e "$j_input" >>N.raw.txt # contains only what was typed in
# Only log non-blank lines. Convert commatas to indents and double commatas to single commatas.
if [[ "$j_input" != "" ]]; then
	j_input_processed=$(printf "$j_input" |sed -r "s/,,/tmp_$timestamp/g" |sed -r "s/,/$x09/g" |sed -r "s/tmp_$timestamp/,/g" )
echo -e "$timestamp $lnc from $device_id\x09$j_input_processed" >>N.txt
fi

# line counter (do not count blank lines)
if [[ "$j_input" != "" ]]; then 
	lnc=$((lnc+1))
	lnid=$((lnid+1))
	echo -e $lnid >.lnid
fi

# commands
if [[ $j_input == "_indent" ]] then sed -i "s/,/\x09/g" N.txt; sed -i "s/,,/,/g" N.txt; fi
if [[ $j_input == "_Exit" ]]; then j_close=1; fi
done