Miscellanea 2020

01/22/2020

02/26/2020

  • 如果不想让一个executable 执行多次,可以在每次run的时候在当前或固定文件夹create一个hidden file, for example: .commad.lock,然后写入当前正在run的command 参数等。通过检查这个file是否存在去决定是不是正在执行。

  • redhat 一个很不错的网站: https://www.redhat.com/sysadmin/

  • command uuidgen 可以用来generate random unique number.

04/07/2020

  • find softlink file, use type l:
1
find . -type l -name dsenv

or use -L, then the -type predicate will always match against the type of the file that a symbolic link points to rather than the link itself (unless the symbolic link is broken).

1
find -L / type f -name dsenv

类似于readlink, 会去softlink directory里面寻找original file.

04/09/2020

  • > /dev/null 2>&1 can be written as &> /dev/null

  • su vs su -. 都是login to another user, - 表示normal login,login后会完全变成当前user的初始环境,比如在当前user的home dir且$PATH也是当前user的。没有-, 则会继承上个user的环境,比如dir还是上次的。2个login都会执行~/.bashrc.

04/18/2020

  • 在pod container中,如果script process不是init process (pid 1),那么script中的trap 内容不会被执行。 还不太清楚为什么(这是docker container的一个特性)

04/26/2020

  • declare -F show function name in current shell
  • declare -f show function definition in current shell, declare -f <f name> get just that definition

06/16/2020

  • 很多build可以用Makiefile + make command去实现,应该是一个通用的工具。

06/21/2020

06/24/2020

  • bash -x ./script, no need set -x

06/25/2020

  • script performance comparison strace -c ./script. 也就是说,多用bash internal command from help. -c will output show system time on each system call.

06/26/2020

07/05/2020

  • shopt -s nocasematch, set bash case-insensitive match in case or [[ ]] condition. 这个是从bash tutorial 中文版中学到的, shopt is bash built-in setting, unlike set is from POSIX.

07/09/2020

  • vim can directly operate on file in tarball: vim xx.tgz then save the changes

07/12/2020

07/29/2020

  • !! re-execute last command.
  • !<beginning token> re-execute last command start with this token.
  • $_ last token in last command line

08/22/2020

  • Interesting: top 10 most used CLI: history | awk {'print $2'} | sort | uniq -c | sort -nr | head -n 10
  • same idea to check my blog theme: ls -ltr | awk 'NR>1 {print $NF}' | cut -d'-' -f1 | sort | uniq -c | sort

09/09/2020

  • Mac netstat list listening port, the netstat on Mac is not that powerful as on Linux:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    ## -p: protocol
    ## -n: numerical address
    ## -a: show all connections
    netstat -an -ptcp | grep -i listen
    ## or
    ## -i: lists IP sockets
    ## -P: do not resolve port names
    ## -n: do not resolve hostnames
    lsof -i -P -n | grep -i "listen"

09/18/2020

10/25/2020

11/03/2020

Known from IBM channel, docker security talk

11/20/2020

  • shell nop command: :, synopsis is true, do nothing, similar to pass in python

    1
    2
    3
    4
    5
    ## : as true
    while :; do
    sleep 1
    echo 1
    done
  • shell mutliline comment, can use heredoc

    1
    2
    3
    : <<'COMMENT'
    ...
    COMMENT

11/24/2020

  • python shebang:
1
2
#!/usr/bin/env python
# -*- coding: utf-8 -*-

11/27/2020

  • ls -1 每行只输出一个文件名.
  • mv -v verbose

12/06/2020

  • bash read file line by line
1
2
3
4
5
6
7
8
9
10
11
cat $file | while read line
do
echo $line
done

# or
input="/path/to/txt/file"
while IFS= read -r line
do
echo "$line"
done < "$input"

12/09/2020

  • sudo to edit restricted permission file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# executing as non-root user
# wrong, because redirection is done by the shell which doesn't has write permission.
sudo echo "sth" >> /root/.bashrc

# correct
echo "sth" | sudo tee -a /root/.bashrc
# or
sudo sh -c 'echo sth >> /root/.bashrc'
# or mutli-line
sudo tee -a /root/.bashrc << EOF
dexec()
{
docker exec -it \${1} sh
}
EOF

12/10/2020

12/25/2020

  • cat jsonfile | python -m json.tooljq 类似,一个json的format工具,但只能pretty print.
  • sudo: unable to resolve host: 我不太理解为什么sudo 会涉及到hostname resolution?
0%