Skip to content

Instantly share code, notes, and snippets.

@jsvisa
Created January 11, 2019 02:39
Show Gist options
  • Save jsvisa/a5f1ce82f2604624c6c534929787bfdf to your computer and use it in GitHub Desktop.
Save jsvisa/a5f1ce82f2604624c6c534929787bfdf to your computer and use it in GitHub Desktop.
bash tutorial #sh #bash

删除一个字符串中后面的字符

$ str=foo_bar:baz
$ echo "${str%%:*}"
foo_bar

删除一个字符串前面的字符

$ str=foo_bar:baz
$ echo "${str##*:}"
baz

字符串转成 List

$ str=‘a|b|c’
$ arr=${str//|/ } && echo $arr
a b c

List 转成字符串

$ arr=(1 2 3)
$ str=$(printf "%s," ${arr[@]:0:2})
1,2

字符串替换

$ str=sh_c
$ echo ${str/_/-}
sh-c

List 数量

$ echo ${#arr[@]}

删除最后一个字符

$ str='123'
$ echo ${str::-1}
12

Map 操作

declare -A regions
regions=( ["SGP"]=40  ["NRT"]=25 )
regions["CDG"]=20
echo ${regions["SGP"]}

数学操作

let a=5+4
echo $a # 9
let "a = 5 + 4"
echo $a # 9
let a++
echo $a # 10
let "a = 4 * 5"
echo $a # 20
let "a = $1 + 30"
echo $a # 30 + first command line argument

find printf 添加打印项

添加 $PREFIX 前缀

find nginx/app -type d -printf '$(PREFIX)/%p '

多字段排序

cat << EOF > all.txt
binance,"2018-09-01",6986,6312,988,61440.61093546052
binance,"2018-09-02",5644,4950,968,34594.98045481693
bitfinex,"2018-10-22",277,236,56,32873.3055055309,29650.881664956407
bitfinex,"2018-10-23",275,222,66,19275.46345581395,37960.79022311062,4431531.91919
EOF

## 根据第二行,再第一行排序,以 ',' 分割
sort -k 2,2 -k 1,1 -t , all.txt > all.csv

取消自动补全

$ complete -F systemctl -r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment