笔记|技巧|工具
replace character
sh
# use “tr” replace ' ' with '/n'
your-shell-command | tr ' ' '\n'`test if a command exist
bash
command -v nvim >/dev/null 2>&1 && export EDITOR="nvim">/dev/null : 将输出重定向到 null
2>&1: 将标准错误重定向到标准输出
if in ssh
sh
[ -n "$SSH_TTY" ] && command -v fish >/dev/null 2>&1 && exec fish后台运行
sh
sleep 100 &任务在后台运行,不阻塞当前终端
运行后在 C-z 暂停,使用 bg 让暂停的任务在后台继续运行
查看后台任务使用jobs(jobs -l)拉回前台使用 fg
nohup python train.py > output.log 2>&1 &
nohup 表示 no hang up ,常住后台,关闭终端不影响
测试网络
sh
curl https://clients3.google.comthis URL is the best option to ping as it should work 24/7 without maintenance pause
一些命令行工具收集
- 计算代码行数
https://github.com/XAMPPRocky/tokei
- 画图
gnuplot
进程替换 Process Substitution
bash/zsh的特性
sh
<(command) # 把 command 的“输出”当成文件
>(command) # 把 command 的“输入”当成文件查看临时文件
sh
ls <(pwd)
# /dev/fd/33执行顺序
sh
diff <(ls) <(ls -a)- Bash 提前 fork 子进程
- 执行 ls
- 把 ls 的 stdout 接到一个 pipe
- 把 pipe 的读端暴露成 /dev/fd/XX
- 把这个路径字符串传给 diff
即首先执行ls,将结果pipe到一个临时文件,接着执行
sh
diff /dev/fd/63 /dev/fd/64>()
sh
# tee 写入两个“文件” 实际是写入两个命令的 stdin
echo "hello" | tee >(grep h) >(wc -c)
# 重定向 stdout 到命令
ls > >(grep py)