Xinye Tao | Blog - Collection - Résumé | RSS

Today I Learned

# Created: 2020-12-30; Modified: 2021-06-26

How to write script that can be safely edited on the run?

Method 1:

source actual-script.sh

Method 2:

{
  # actual script
  exit;
}

(StackOverflow)

Create highlight group in vim
# echo rule.vim
highlight Group ctermbg=green guibg=green
call matchadd("Group", "foo")
# vim text
:so rule.vim

(StackExchange)

Reclaim disk space (on a shared server)
  1. delete some files

  2. use sudo lsof +L1 to kill process referencing those files

  3. use tune2fs -m <root-reserved-percentage> <device> to grab more space from root

Serve a web page
python2 -m SimpleHTTPServer <port>
python3 -m http.server
C supports Variable Length Array (VLA)
void initialize(size_t n, size_t m, double A[n][m]);
Forward Declaration

Pros: hide implementations (declare XXImpl), circular dependence, faster compilation

Cons: can’t use alias, potential inheritance is elided, delete an incomplete class is UB

Evaluation Interleaving

f(std::shared_ptr(new A()), g()) is risky if g() can be interleaved between object allocation and shared_ptr initialization.

This is explicitly prohibited after C++17 (StackOverflow)

Tiny details
  • _ variables are dropped immediately
  • Overflow check is disabled in release build
Avoid self-reference

I have a process table and I want to grep it for the phrase “banana”:

ps auxww | grep banana

root 87 Jun21 0:26.78 /System/Library/CoreServices/FruitProcessor --core=banana

mikec 456 450PM 0:00.00 grep banana

Argh! It also greps for the grep for banana! Annoying!

Well, I’m sure there’s pgrep or some clever thing, but my coworker showed me this and it took me a few minutes to realize how it works:

ps auxww | grep [b]anana

root 87 Jun21 0:26.78 /System/Library/CoreServices/FruitProcessor --core=banana

Doc Brown spoke to me: “You’re just not thinking fourth dimensionally!” Like Marty, I have a real problem with that. But don’t you see: [b]anana matches banana but it doesn’t match ‘grep [b]anana’ as a raw string. And so I get only the process I wanted!

(Hacker News)

Using fields

Fields are procedural texts that are automatically generated based on the context. Take PAGE as an example, it is a field representing current page number. You can insert it via Insert > Parts > Field > Page, or manually type in the script { PAGE } (the braces are inserted using Ctrl+F9, need to right click Update Field to generate text). A more completed script would be like { IF { = MOD( {PAGE}, 2 ) } = 0 "{ PAGE } (even)" "{ PAGE } (odd)" }.

(How to control the page numbering in a Word document, List of field codes in Word)