Bash
3 min read
Shell 和 Bash 很有趣,可以让很多事情变得更好高效,这里留下一个索引,不时回顾。
资源
- Bash Hacker Wiki 该Wiki保存任何和GNU Bash相关的文档,旨在提供人类可读的文档和信息,避免阅读Bash手册
- awesome-shell 一份精心组织的命令行工具及资源的列表
- terminals-are-sexy 为 CLI 爱好者精心挑选的终端框架、插件和资源列表
- the-art-of-command-line 命令行的艺术(中文版)
- bash-guide Bash指南
- awesome-bash 关于 Bash 脚本和资源的精选列表
- Shell Field Guide 编写脚本时使用到的技巧和技术目录
- Bash 手册 Bash GNU手册
Bash代码规范
- styleguide 来自 Google 的 Shell 脚本的代码规范
- shellcheck 一个静态 shell 脚本分析工具,本质上是 bash/sh/zsh 的 lint
Bash脚本框架
- bashly Bash 命令行框架和 CLI 生成器
包管理器
- bash-it Bash 框架
这里可以找到有意思命令脚本
- bashoneliners.com
- Shell Scripts
- Bash Scripts
- commandlinefu.com
- shell-fu
- scripts
- 100-shell-script-examples
- simple-bash-scripts
有趣的脚本
将snakeCase 转换为UpperCamelCase
$ echo "this_is_the_string" | gsed -r 's/(^|_)([a-z])/\U\2/g'
ThisIsTheString
将UpperCamelCase 转换为 UpperCamelCase
$ echo 'ThisIsTheString' | gsed -r 's/([A-Z])/_\l\1/g' | gsed -r 's/^_//'
this_is_the_string
批量将文件从UpperCamelCase 转换为 UpperCamelCase
for file in `ls *.go`; do \
mv "$file" "$(echo $file | gsed -r 's/([A-Z])/_\l\1/g' | gsed -r 's/^_//')"; \
done
批量格式化当前目录下的 go 文件
for file in `ls *.go`; do \
gofmt -w $file; \
done
读取文件中的每一行,以它作为文件名新建文件
如有一个文件 file
, 每一行有以空格分割的2列,现以第一列作为文件名(文件类型是 log
),第二列为文件内容, file
文件内容如下:
one 1
two 2
three 3
可以使用:
gawk '{print $2 > $1".log"}' file
以易读的方式显示当前$PATH
$ echo "${PATH//:/\n}"
Output:
/usr/local/bin
/System/Cryptexes/App/usr/bin
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/go/bin
/Library/Apple/usr/bin
对输出进行排序:
$ echo "${PATH//:/\n}" | sort
或者使用tr
:
$ echo "$PATH" | tr ":" "\n" | nl
使用awk
$ echo $PATH | awk -F: '{for(i=1;i<=NF;i++)print $i}'
$ awk 'BEGIN{RS=":"} {print $0}' <<<"$PATH"
remove duplicates in $PATH zsh
$ typeset -U path
nmap验证MongoDB未授权访问漏洞
nmap -p 27017 --script mongodb-info <ip>
Unix/Linux Command Reference
Last updated on 2023-12-11