Bash command line menu system

For trained users, command-line interface of shells, such as BASH, is very convenient for crafting up complex actions that are much more cumbersome to do in GUI. However, some action may require a long line of command that requires looking up documentation to construct. One would find themself using “history” to look up a past invocation for copying to the current line of command. It is thus useful to have a command menu system for these reusable command lines available. choice.sh is my solution to this need. The content of the script is as follows:

#!/bin/bash
# Read and list lines from "choice.cmd". Allowing user interaction
# through stderr to choose a line and pass it back to stdout so it
# can be "eval"ed by the BASH

mapfile -u 4 -t 4<${BASH_SOURCE[0]%\.sh}.cmd

echo Run command: >&2
for ((i=0;i<${#MAPFILE[@]};i++));do
	printf "%4s: %s\n" "$((i+1))" "${MAPFILE[$i]}" >&2
done
printf "Choice: " >&2
read i
re='^[1-9][0-9]*$'
if [[ $i =~ $re ]] && [[ $((i-1)) -lt ${#MAPFILE[@]} ]]; then
	echo ${MAPFILE[$((i-1))]}
else
	echo "echo Cancelled."
fi

One can place file in, say, ~/lib, and add an alias, alias c='eval `~/lib/choice.sh`' to the ~/.bashrc. The menu system can then be invoked by typing c, enter on the command line. It will read all lines from the file choice.cmd in the same directory as the script, list them to the terminal screen using stderr, allow the user to make a choice, and pass the line to stdout so it will be evaled by the shell. Example usage is as follows:

cjj@parakeet:~$ c
Run command:
   1: eval "$(/home/cjj/opt/miniconda3/bin/conda shell.bash hook)"
   2: xrandr --output HDMI-1 --mode 1024x768 --scale 1x1 --pos 0x312
   3: xrandr --output HDMI-1 --mode 1024x768 --scale-from 1920x1080 --same-as eDP-1
Choice: 1
(base) cjj@parakeet:~$

Fetch images for html

Find img tags with remote source images in an HTML file and download the images to a local folder. Change the HTML files to use the local files instead.

This was done with the one liner:

grep img rbm.html|tr '"' '\n'|grep http|sort -u|while read f;do l=`basename ${f}`;ff=${l%.???};fe=${l##*\.};echo ${ff} ${fe};if [ -f "rbm_pics/${l}" ];then i=1;while [ -f "rbm_pics/${ff}-${i}.${fe}" ];do i=$((i+1));done;l="${ff}-${i}.${fe}";fi;curl --output rbm_pics/${l} "${f}";sed "sX${f}Xrbm_pics/${l}X" rbm0.html > t.html;mv t.html rbm0.html;done

A better formatted version is as follows:

grep img rbm.html|tr '"' '\n'|grep http|sort -u|while read f;do
	l=`basename ${f}`
	ff=${l%.???}
	fe=${l##*\.}
	echo ${ff} ${fe}
	if [ -f "rbm_pics/${l}" ];then
		i=1
		while [ -f "rbm_pics/${ff}-${i}.${fe}" ];do
			i=$((i+1))
		done
		l="${ff}-${i}.${fe}"
	fi
	curl --output rbm_pics/${l} "${f}"
	sed "sX${f}Xrbm_pics/${l}X" rbm0.html > t.html
	mv t.html rbm0.html
done

Realizable imaginations

The perceivable of our mind can be realities and can be imaginations. The life process of a conscious being is in the production of imaginations from the perceived realities as well as striving in the realization of these imaginations. However, there are also unrealizable imaginations, which are sometimes called fantasies. The best status of being is working in the realization of our own realizable imaginations. And, the worst case scenario will be indulging someone else’s fantasies.

Freedom and limitation

When we come into this world, we have various resources at our disposal. Our freedom is in the actions we can take or things we can do with the resources at hand. This should align with our thinking and imagination. (Or, the other way around.) We need the ability to see possibilities with these limited resources and take the ones that make us the happiest. I think excessive limitations on one’s action tends to degrade such ability. Therefore, it is important to design a societal structure that will maximize personal freedom, only guards against the worst harms, and only as a last resort, through the limitation of freedom.

共同的經驗

過去的我和現在的我是一樣的嗎。此刻的我對過往所存的一切記憶,有多少是真正我的。基於過去的我而做的行為決定和為了別人所做的行為決定,有本質上的不同嗎?如果不考量現在的我來作為,把事情歸因到過去和別人的原故,這對身為有自由意志的個體來說,難道不是推委卸責的作法?

Freedom of speech

Speech is a means of communication. It is an open communication. Not all communication is a speech so freedom of speech is not the same as the freedom of communication. It is within one’s right to make any kind of speech. However, just as any other kind of actions, one should be responsible for its consequences. Also just like any other kind of actions, the freedom can be limited only when any of its consequences are commonly agreed to be certain and damaging that the damage is not within one’s privileges to cast.