Contents
  1. 1. Base
    1. 1.1. chsh change shell
      1. 1.1.1. To Know shells installed
      2. 1.1.2. check current shell
      3. 1.1.3. change shell in all environment
    2. 1.2. export: change environment variable
      1. 1.2.1. export output all environment variable
      2. 1.2.2. Difference of env/set/export/declare
      3. 1.2.3. set environment variable
      4. 1.2.4. Roles of export
    3. 1.3. read
      1. 1.3.1. listen from keyboard
      2. 1.3.2. read but time limit
      3. 1.3.3. password input
      4. 1.3.4. read from file
      5. 1.3.5. Line break
    4. 1.4. expr: calculation
      1. 1.4.1. +-*/
      2. 1.4.2. operate string
    5. 1.5. tmux: multi windows and session
      1. 1.5.1. trouble can solve
      2. 1.5.2. Usage
    6. 1.6. alias
    7. 1.7. history
    8. 1.8. xargs
      1. 1.8.1. pipleline
      2. 1.8.2. xargs -p :y/n
      3. 1.8.3. xargs -E quit
    9. 1.9. time
      1. 1.9.1. time a simple command or give resource usage
    10. 1.10. sleep
  2. 2. FIle
  3. 3. Text

Base

chsh change shell

To Know shells installed

1
2
3
4
5
6
7
8
9
10
11
12
$ cat /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

check current shell

1
2
$ echo $SHELL
/bin/zsh

change shell in all environment

1
2
3
4
$ chsh -s /bin/zsh
Changing shell for xwk.
Password for xwk:
chsh: no changes made

export: change environment variable

export output all environment variable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$ export
CLICOLOR=1
COLORFGBG='7;0'
COLORTERM=truecolor
COMMAND_MODE=unix2003
GOBIN=/Users/xwk/gopath/bin
GOPATH=/Users/xwk/gopath
GOROOT=/usr/local/go
HOME=/Users/xwk
HOMEBREW_BOTTLE_DOMAIN=https://mirrors.cloud.tencent.com/homebrew-bottles
HOMEBREW_NO_AUTO_UPDATE=1
ITERM_PROFILE=Default
ITERM_SESSION_ID=w0t1p0:5212F532-B525-4B5D-BB6E-238806196C29
LANG=zh_CN.UTF-8
LC_ALL=zh_CN.UTF-8
LC_TERMINAL=iTerm2
LC_TERMINAL_VERSION=3.3.7beta1
LESS=-R
LOGNAME=xwk
LSCOLORS=gxfxbEaEBxxEhEhBaDaCaD
LaunchInstanceID=3E754314-9261-4388-9A0E-2FC388411C15
OLDPWD=/Users/xwk
PAGER=less
PATH='/Applications/MacVim.app/Contents/bin:/bin:/usr/bin:/usr/local/bin:/Applications/MacVim.app/Contents/bin:/bin:/usr/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/go/bin:/Users/xwk/gopath/bin:/Users/xwk/.fzf/bin:/Users/xwk/gopath/bin'
PWD=/etc
SECURITYSESSIONID=18778
SHELL=/bin/zsh
SHLVL=2

Difference of env/set/export/declare

  1. shell variabel

    • envionment variable
    • custom variable
  2. Difference

    • env: current user environment variable
    • export: same as env, but output sort by name
    • set: all variable in this shell
    • declare: same as set, but output sort by name
  3. Conclusion

    • env/export: show environment variable
    • set/decare: show all variable

set environment variable

1
export PATH=$PATH:/bin

Roles of export

set environment variable

1
2
3
4
5
6
7
8
9
10
11
$ guopu=123
$ echo $guopu
123
$ xwk=456
$ echo $xwk
456
$ export guopu
$ dash
$ echo $guopu
123
$ echo $xwk

read

listen from keyboard

1
2
3
4
#!/bin/bash
read -p "Please input your name and age:" name age
echo "Welcome !!! $name is $age"
exit 0

read but time limit

1
2
3
4
5
6
7
8
#!/bin/bash
if read -t 3 -p "Please input your name and age within 3s:" name age
then
echo "Welcome !!! $name is $age"
else
echo "Sorry too slow!"
fi
exit 0

password input

1
2
3
read  -s -p "Plese input passwd: " passwd
echo "you input: $passwd"
exit 0

read from file

  • &- close standard output eg: file fd
  1. Solution 1: read -u

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    #!/bin/bash
    # assign the file desciptor to file for input fd # 3 is Input file
    exec 3< test.sh

    # read the file using fd # 3
    count=0
    while read -u 3 var
    do
    let count=$count+1
    echo "LIne $count: $var"
    done
    echo "finished"
    echo "LIne num is $count"

    # close fd
    exec 3<&-
  2. Solution 2: pipeline |

    1
    2
    3
    4
    5
    6
    7
    count=0
    cat test.sh | while read line
    do
    let count=$count+1
    echo "Line $count: $line"
    done
    exit 0
  3. Solution 3: redirect <

    1
    2
    3
    4
    5
    6
    7
    8
    count=0
    while read line
    do
    let count=$count+1
    echo "Line $count: $line"
    done < test.sh
    echo "count: $count"
    exit 0

Line break

Do not let backslash (\) act as an escape character read -r variable

expr: calculation

+-*/

1
2
3
4
5
6
7
#!/bin/bash
expr 10 + 10
expr 20 - 10
expr 30 / 10
# * is the special character
expr 10 \* 3
expr \( 3 + 3 \) \* 3 + 10

operate string

运算 表达式 意义
match match STRING REGEXP STRING中匹配REGEXP的字符串返回匹配的长度
substr substr STRING POS LEN 从POS位置截取LEN长度字符串
index index STRING SUBSTR 查找子字符串的起始位置
length length STRING 字符串的长度

tmux: multi windows and session

Terminal MultipleXer

trouble can solve

  • big data transporting while disconnected
  • compiling and runing but over
  • multi windows

Usage

  1. run

    1
    tmux new -s session_name
  2. create again

    • Ctr-b
    • c
  3. switch windows

    • C-b
    • number (the window)
  4. quit

    • C-b d
  5. back

    1
    2
    3
    4
    5
    $ tmux ls
    0: 1 windows (created Sat Feb 29 23:30:39 2020)
    hello: 4 windows (created Sat Feb 29 23:44:30 2020)

    $ tmux a -t hello
  6. other

type operate comment
pane % split horizon
split vertical
x close current
{ current forward
} current backward
; switch pre pane
o next pane(also up/down/left/right)
space switch layout
z max current;again z recover
q show all pane number
window c new window
p pre window
n nex window
w window list
& close current
, rename window
number switch window
f search window
1
2
3
4
5
6
tmux new -s foo # 新建名称为 foo 的会话
tmux ls # 列出所有 tmux 会话
tmux a # 恢复至上一次的会话
tmux a -t foo # 恢复名称为 foo 的会话,会话默认名称为数字
tmux kill-session -t foo # 删除名称为 foo 的会话
tmux kill-server # 删除所有的会话

alias

1
2
3
4
5
6
7
8
# set alias
alias name=value # = before and after don't have space
# show all alias
alias
alias gomodon
# delete alias
unalias vi
unalias -a # delete all

history

  • show time export HISTTIMEFORMAT=’%F %T’
  • repeat action C-p
  • clear history -c

xargs

execute arguments

pipleline

pipleline can use pre input as after input but cann’t use it as arguments eg:

1
2
3
4
➜  ~ echo test.log | cat
test.log
➜ ~ echo test.log | xargs cat
hello luojilab

xargs default see ‘enter tab space’ as space use ‘xargs -0 ‘can change it as NULL

xargs -p :y/n

1
2
3
~ echo test.log | xargs -p cat
cat test.log ?...y
hello luojilab

xargs -E quit

1
2
3
4
➜  ~ cat test.log
hello he hhh yy
➜ ~ cat test.log| xargs -E "hhh" echo
hello he

time

time a simple command or give resource usage

1
2
3
4
5
[root@BJ-DEV-GO ledgers]# time grep mockUids ledgers.log

real 0m0.164s
user 0m0.099s
sys 0m0.046s

sleep

1
2
3
$ date;sleep 30s;date
2020年 3月 1日 星期日 16时16分41秒 CST
2020年 3月 1日 星期日 16时17分11秒 CST

FIle

Text

Contents
  1. 1. Base
    1. 1.1. chsh change shell
      1. 1.1.1. To Know shells installed
      2. 1.1.2. check current shell
      3. 1.1.3. change shell in all environment
    2. 1.2. export: change environment variable
      1. 1.2.1. export output all environment variable
      2. 1.2.2. Difference of env/set/export/declare
      3. 1.2.3. set environment variable
      4. 1.2.4. Roles of export
    3. 1.3. read
      1. 1.3.1. listen from keyboard
      2. 1.3.2. read but time limit
      3. 1.3.3. password input
      4. 1.3.4. read from file
      5. 1.3.5. Line break
    4. 1.4. expr: calculation
      1. 1.4.1. +-*/
      2. 1.4.2. operate string
    5. 1.5. tmux: multi windows and session
      1. 1.5.1. trouble can solve
      2. 1.5.2. Usage
    6. 1.6. alias
    7. 1.7. history
    8. 1.8. xargs
      1. 1.8.1. pipleline
      2. 1.8.2. xargs -p :y/n
      3. 1.8.3. xargs -E quit
    9. 1.9. time
      1. 1.9.1. time a simple command or give resource usage
    10. 1.10. sleep
  2. 2. FIle
  3. 3. Text