#前言

之前自己写过一些定时脚本,比如整理照片啊、加密备份啥的,之前一直没有把cron的运行日志记录下来,最近记录之后发现写的有好多问题,之后有关zsh的问题也会在这里进行记录总结。

#comm

在进行文件比对的时候,有可能因为文件结尾是CRLF导致每一行都不一样,这时候需要统一换行符。

简单的方式是用vim打开文件,然后:

1
:set ff=unix

#零填充

1
2
 [2022/10/14 16:06:27] mbp ➜  ~  printf "%02d" 2
02%

#grep

1
cat data.txt | grep  "[9]$"

筛选所有9结尾的行

#纳秒/微秒

zsh 本身的 date 不支持微秒

替代工具是gdate

1
2
 [2022/10/13 11:21:12] mbp ➜  ~  gdate +%s%N
1665631578359404000
1
2
# 安装gdate
brew install coreutils

使用场景是,脚本的 token 生成等。

1
2
# 微秒在纳秒的基础上除法
echo $(($(gdate +%s%N)/1000000))

#列表数据/数组

理论上空格可以作为列表分隔的元素,所以最简单的使用方法是:

1
2
3
for i in 1 2 3; do
echo $i
done

也可以用参数的形式:

1
2
3
4
a=(1 2 3)
for i in ${a}; do
echo $i
done

#向列表添加数据

1
2
3
4
5
6
qs=('aaaa' 'bbbb')
qs+=('foo')
for q in ${qs}; do
echo "test ${q}"
echo ""
done

#反引号

被反引号`包括的内容会作为命令执行,然后把结果作为参数传给变量

1
2
3
cur_sec=`date '+%s'`
echo $cur_sec
# 获取秒级时间戳

#no matches found

在用通配符*的时候,有时候如果没有文件,就会报错:

1
2
$ mv /Users/onns/Desktop/temp/* ${another_dir}
no matches found: /Users/onns/Desktop/temp/*

原因改天再补吧!

#解决方法

解决方法是在运行脚本之前先运行一下:

1
setopt +o nomatch

但是必须要写在脚本文件里,我写在.zshrc里结果没用,我也不知道为什么,如果有人知道可以告知一下。

#相关链接

#tar: Removing leading ‘/’ from member names

这个就比较好解决了,一搜就有,没想到tar默认是用相对地址的= =

不过我比较好奇的是,为什么在压缩过程的输出是输出到stderr里而不是stdout里,也是TODO,改日再说。

#解决方法

添加-P参数:

1
tar -zcvPf  - "${imgdir}" | openssl des3 -salt -k $img_password | dd of=${bakdir}img-${now_date}-${now_time}.bak

#相关链接

#Prompt

1
PROMPT='%F{green}[%*] %n@%m%f %F{blue}%~%f $ '

#相关链接

#默认值

1
FOO="${VARIABLE:-default}"

:-后加默认值

#for 循环

1
2
for ((c = 1; c <= page; c++)); do
done

(()) 里面写变量的时候,不需要加$

#screen 乱码

创建窗口的时候用:

1
screen -U -S onns

重连的时候用:

1
screen -U -r pid

不知道为什么用配置文件的方式没生效= =

#时间相关

显示今天是今年的第几天:

1
2
 [2022/12/06 17:06:25] mbp ➜  ~  gdate +%j
340

#curl 相关

curl 的-G选项代表 get,否则默认 post

1
curl -G --data-urlencode "key=value" url

可以在 shell 中输入需要 urlencode 的内容,比如 json 输入的时候
如果用了--data-urlencode,再在 url 里加参数会不生效,必须全部用这个参数指示

#对文件进行拆分

1
2
3
4
5
$ split -h
split [-l line_count] [-a suffix_length] [file [prefix]]
split -b byte_count[K|k|M|m|G|g] [-a suffix_length] [file [prefix]]
split -n chunk_count [-a suffix_length] [file [prefix]]
split -p pattern [-a suffix_length] [file [prefix]]

最常用的是按照行号分割:

1
2
split -l 200 order.csv oa.
# 将order.csv按照200行拆分成一个文件,文件的前缀是oa.

#seq

可以生成序号的命令:

1
2
3
4
for i in $(seq 1 60)
do
#curl xxxx
done

#查找内容前后数据

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
$ grep --help
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE.
Example: grep -i 'hello world' menu.h main.c

Pattern selection and interpretation:
-E, --extended-regexp PATTERN is an extended regular expression
-F, --fixed-strings PATTERN is a set of newline-separated strings
-G, --basic-regexp PATTERN is a basic regular expression (default)
-P, --perl-regexp PATTERN is a Perl regular expression
-e, --regexp=PATTERN use PATTERN for matching
-f, --file=FILE obtain PATTERN from FILE
-i, --ignore-case ignore case distinctions
-w, --word-regexp force PATTERN to match only whole words
-x, --line-regexp force PATTERN to match only whole lines
-z, --null-data a data line ends in 0 byte, not newline

Miscellaneous:
-s, --no-messages suppress error messages
-v, --invert-match select non-matching lines
-V, --version display version information and exit
--help display this help text and exit

Output control:
-m, --max-count=NUM stop after NUM selected lines
-b, --byte-offset print the byte offset with output lines
-n, --line-number print line number with output lines
--line-buffered flush output on every line
-H, --with-filename print file name with output lines
-h, --no-filename suppress the file name prefix on output
--label=LABEL use LABEL as the standard input file name prefix
-o, --only-matching show only the part of a line matching PATTERN
-q, --quiet, --silent suppress all normal output
--binary-files=TYPE assume that binary files are TYPE;
TYPE is 'binary', 'text', or 'without-match'
-a, --text equivalent to --binary-files=text
-I equivalent to --binary-files=without-match
-d, --directories=ACTION how to handle directories;
ACTION is 'read', 'recurse', or 'skip'
-D, --devices=ACTION how to handle devices, FIFOs and sockets;
ACTION is 'read' or 'skip'
-r, --recursive like --directories=recurse
-R, --dereference-recursive likewise, but follow all symlinks
--include=FILE_PATTERN search only files that match FILE_PATTERN
--exclude=FILE_PATTERN skip files and directories matching FILE_PATTERN
--exclude-from=FILE skip files matching any file pattern from FILE
--exclude-dir=PATTERN directories that match PATTERN will be skipped.
-L, --files-without-match print only names of FILEs with no selected lines
-l, --files-with-matches print only names of FILEs with selected lines
-c, --count print only a count of selected lines per FILE
-T, --initial-tab make tabs line up (if needed)
-Z, --null print 0 byte after FILE name

Context control:
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
-C, --context=NUM print NUM lines of output context
-NUM same as --context=NUM
--color[=WHEN],
--colour[=WHEN] use markers to highlight the matching strings;
WHEN is 'always', 'never', or 'auto'
-U, --binary do not strip CR characters at EOL (MSDOS/Windows)

When FILE is '-', read standard input. With no FILE, read '.' if
recursive, '-' otherwise. With fewer than two FILEs, assume -h.
Exit status is 0 if any line is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.

Report bugs to: bug-grep@gnu.org
GNU grep home page: <http://www.gnu.org/software/grep/>
General help using GNU software: <http://www.gnu.org/gethelp/>

-A 10 显示查找内容+后面的 10 行。
-B 10 显示查找内容+前面的 10 行。
-C 10 显示查找内容+前面和后面的 10 行。

#判断文件是否存在

#从数据中选取指定列

#TODO

  • [ ] setopt +o nomatch的作用
  • [ ] tar输出问题