Sh

Sh - How To

use Command env to print env vars

$ docker run ubuntu env
$ env

PYTHONUNBUFFERED=1
CELERY_BROKER_URL=amqp://guest:guest@rabbitmq:5672//
CHOKIDAR_USEPOLLING=true
HOSTNAME=af072bc9372e
PYTHON_VERSION=3.8.17
CELERY_RESULT_BACKEND=django-db
PWD=/app/backend
PYTHON_SETUPTOOLS_VERSION=57.5.0
HOME=/root
LANG=C.UTF-8
AWS_SECRET_ACCESS_KEY=
ONEVIEW_ENV=DEV
TERM=xterm
SHLVL=1
AWS_ACCESS_KEY_ID=
PYTHON_PIP_VERSION=23.0.1
PYTHON_GET_PIP_SHA256=96461deced5c2a487ddc65207ec5a9cffeca0d34e7af7ea1afc470ff0d746207
PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/0d8570dc44796f4369b652222cf176b3db6ac70e/public/get-pip.py
PATH=/root/.local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
AWS_SESSION_TOKEN=
_=/usr/bin/env

resolve DNS IP addresses in Mac

> dscacheutil -q host -a name google.com
name: google.com
ipv6_address: 2a00:1450:4009:820::200e

name: google.com
ip_address: 142.250.187.238
> nslookup google.com
Server:     fe80::1%15
Address:    fe80::1%15#53

Non-authoritative answer:
Name:   google.com
Address: 142.250.187.238

use command date to convert time format

TLDR - date -j -f '%d/%b/%y' $INPUT_DATE +%Y-%m-%d

use lpr to print hardcopies

TL;DR: lpr -p -o EPIJ_Silt=1 -o Resolution=720x720dpi -o EPIJ_Qual=307 ./lpoptoins.pdf

Use lpoptions -l to show custom options for printer, such as EPIJ_Qual/Print Quality: 308 311 *303 309 304 305 306 307.

Note that only EPIJ_Qual is used in printing option e.g. lpr -o EPIJ_Qual=308, the text after / is an short description of the option.

Example output of lpoptions -l

$ lpoptions -l
...
EPIJ_Ink_/Grayscale: *1 0
EPIJ_Qual/Print Quality: 308 311 *303 309 304 305 306 307
ColorModel/ColorModel: *RGB Mono
Resolution/Resolution: 180x180dpi *360x360dpi 720x720dpi
PageSize/Media Size: *A4 EPKG EPKG.NMgn EPPhotoPaper2L A6 A5 B5 B6 EPPhotoPaperLRoll EPPhotoPaperLRoll.NMgn EPIndexCard5x8 EP8x10in EPHiVision102x180 EPHiVision102x180.NMgn EPPostcard100x148 EPPostcard100x148.NMgn Env10 EnvDL EnvC6 Letter EP216x330mm EPIndianLegal Legal EP16K195x270mm Custom.WIDTHxHEIGHT
EPIJ_Silt/Quiet Mode: *0 1
...

upzip /Library/Printers/PPDs/Contents/Resources/EPSON\ ET-2810\ Series.gz to ~/Documents/downloads/ read the mearning of options.

Run the following command in the unzipped downloads folder

$ cat EPSON\ ET-2820\ Series | grep Qua
...
*EPIJ_Qual 308/Draft: ""
*EPIJ_Qual 311/Draft-Vivid : ""
*EPIJ_Qual 303/Normal: ""
*EPIJ_Qual 309/Normal-Vivid: ""
*EPIJ_Qual 304/Fine: ""
*EPIJ_Qual 305/Quality: ""
*EPIJ_Qual 306/High Quality: ""
*EPIJ_Qual 307/Best Quality: ""
...

Other Useful Links:

find source code in certain directories

Question: How to exclude *.tsx, migrations files, and python tests files?

In git grep

git grep 'tiger' -- '*.py' ':!*tests*' ':!*migrations*'
# same as 
git grep 'tiger' -- '*.py' ':(exclude)*tests*' ':(exclude)*migrations*'

After the -- the patterns used are <pathspec>, see man gitglossary.

In Rg

rg 'tiger' --glob '!{**/tests/*,**/migrations/*}'

# if you are sure there are no files with name `tests` or `migations`, you
# can do
rg 'tiger' --glob '!{tests,migrations}'

rg 'tiger' -g '!tests' -g '!migrations'

find the size of current directory

du -d 1 -h | sort --human-numeric-sort --reverse | head

worth noting that

open vim to edit command line alt+v

Notes for the future:

to have alt-v works I need to set the right alt key to be Esc+ in iTerm2

Q: How do I make the option/alt key act like Meta or send escape codes? A: Go to Preferences > Profiles tab. Select your profile on the left, and then open the Keyboard tab. At the bottom is a set of buttons that lets you select the behavior of the Option key. For most users, Esc+ will be the best choice.

from iterm2 faq

create, extract, list a gzipped tarball

create a gzipped tarball from a directory

# -c    Create a new archive
# -v    Verbose output
# -f file.tar.gz    Use archive file
# -z    Filter the archive through gzip i.e. compress or decompress archive
tar -c -z -v -f yuhao_huang.tar.gz foo/

extract a gzipped tarball into a directory

# -x    Extract files from an arc
# -x    Extract files from an archive
# -z    Filter the archive through gzip i.e. compress or decompress archive
# -v    Verbose output
# -f file.tar.gz    Use archive file
tar -x -z -v -f yuhao_huang.tar.gz -C ~/Downloads/bar/

list the content of gzipped tarball without extraction

# -z    Filter the archive through gzip i.e. compress or decompress archive
# -v    Verbose output
# -f file.tar.gz    Use archive file
# -t    List the contents of an archive
tar -z -t -v -f yuhao_huang.tar.gz

Sh - Explanation

shell builtin commands

Certain shell commands are builtin to zsh, others are exposed in PATH. echo is a shell built-in command cat is exposed in PATH.

$ which echo
echo: shell built-in command

$ which cat
/bin/cat

busybox

What is BusyBox?

BusyBox combines tiny versions of many common UNIX utilities into a single small executable. It provides minimalist replacements for most of the utilities you usually find in bzip2, coreutils, dhcp, diffutils, e2fsprogs, file, findutils, gawk, grep, inetutils, less, modutils, net-tools, procps, sed, shadow, sysklogd, sysvinit, tar, util-linux, and vim. The utilities in BusyBox often have fewer options than their full-featured cousins; however, the options that are included provide the expected functionality and behave very much like their larger counterparts.

BusyBox seems useful to

  1. read the C Implementation of the shell utilities
  2. use as sandbox as a simpler environment compared in MacOS

Implementation of shell command cat in C

https://github.com/mirror/busybox/blob/master/coreutils/cat.c

Play with busybox

docker run -it --rm busybox

/bin # ls -l | wc -l
406

There are 406 utilities in busybox /bin, whereas MacOS has 980 in /usr/bin.