Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

Netkiller Shell 手札

Unix, Linux, FreeBSD & Mac Shell

Mr. Neo Chan, 陈景峯(BG7NYT)



中国广东省深圳市望海路半岛城邦三期
518067
+86 13113668890


2009-11-15

电子书最近一次更新于 2024-01-26 00:17:00

版权声明

转载请与作者联系,转载时请务必标明文章原始出处和作者信息及本声明。

http://www.netkiller.cn
http://netkiller.github.io
http://netkiller.sourceforge.net
微信公众号: netkiller
微信:13113668890 请注明“读者”
QQ:13721218 请注明“读者”
QQ群:128659835 请注明“读者”
知乎专栏

内容摘要

Netkiller 系列手札 已经被 Github 收录,并备份保存在北极地下250米深的代码库中,备份会保留1000年。

Preserving open source software for future generations

The world is powered by open source software. It is a hidden cornerstone of modern civilization, and the shared heritage of all humanity.

The GitHub Arctic Code Vault is a data repository preserved in the Arctic World Archive (AWA), a very-long-term archival facility 250 meters deep in the permafrost of an Arctic mountain.

We are collaborating with the Bodleian Library in Oxford, the Bibliotheca Alexandrina in Egypt, and Stanford Libraries in California to store copies of 17,000 of GitHub’s most popular and most-depended-upon projects—open source’s “greatest hits”—in their archives, in museum-quality cases, to preserve them for future generations.

我们正在与牛津的博德利安图书馆、埃及的亚历山大图书馆和加利福尼亚州的斯坦福图书馆合作,将 GitHub 最受欢迎和最依赖的项目 17,000 份副本保存在他们的档案中,在博物馆质量的案例中,为子孙后代保存这些作品。

https://archiveprogram.github.com/arctic-vault/

我的系列文档:

Netkiller Linux 手札 Netkiller FreeBSD 手札 Netkiller Shell 手札 Netkiller Security 手札
Netkiller Web 手札 Netkiller Monitoring 手札 Netkiller Storage 手札 Netkiller Mail 手札
Netkiller Virtualization 手札 Netkiller Cryptography 手札   

以下文档停止更新合并到 《Netkiller Linux 手札》

Netkiller Debian 手札 Netkiller CentOS 手札 Netkiller Multimedia 手札    

致读者

Netkiller 系列手札 已经被 Github 收录,并备份保存在北极地下250米深的代码库中,备份会保留1000年。

Preserving open source software for future generations

The world is powered by open source software. It is a hidden cornerstone of modern civilization, and the shared heritage of all humanity.

The GitHub Arctic Code Vault is a data repository preserved in the Arctic World Archive (AWA), a very-long-term archival facility 250 meters deep in the permafrost of an Arctic mountain.

We are collaborating with the Bodleian Library in Oxford, the Bibliotheca Alexandrina in Egypt, and Stanford Libraries in California to store copies of 17,000 of GitHub’s most popular and most-depended-upon projects—open source’s “greatest hits”—in their archives, in museum-quality cases, to preserve them for future generations.

https://archiveprogram.github.com/arctic-vault/

目录

1. 自述
1.1. 写给读者
1.2. 作者简介
1.3. 如何获得文档
1.4. 打赏(Donations)
1.5. 联系方式
1. Shell
1.1. 快捷键
1.1.1. 命令行编辑命令
1.1.2. 重新执行命令快捷键
1.1.3. 终端控制快捷键
1.1.4. Bang (!) 命令
1.2. chsh - change login shell
1.3. 执行程序返回值
2. Bash Shell
2.1. bash - GNU Bourne-Again SHell
2.1.1. -n 检查脚本是否有语法错误
2.1.2. -x 显示详细运行过程
2.2. 切换身份
2.3. I/O 重定向
2.3.1. stdout
2.3.2. error 重定向
2.3.3. 使用块记录日志
2.3.4. tee - read from standard input and write to standard output and files
2.3.5. 创建文件
2.3.6. 快速清空一个文件的内容
2.4. pipes (FIFOs)
2.5. mktemp - create a temporary file or directory 临时目录与文件
2.6. History 命令历史记录
2.6.1. .bash_history
2.6.2. 清理历史记录
2.6.3. .mysql_history
2.7. hash - hash database access method
2.8. prompt
2.9. 变量 variable
2.9.1. 系统变量
2.9.2. 表达式
2.9.3. Internal Environment Variables
2.9.4. set 设置变量
2.9.5. unset 变量销毁
2.9.6. 设置变量默认值
2.9.7. export 设置全局变量
2.9.8. declare
2.9.9. Numerical 数值运算
2.9.10. Strings 字符串操作
2.9.11. Array 数组
2.9.12. read 赋值多个变量
2.9.13. eval
2.9.14. typeset
2.9.15. envsubst - substitutes environment variables in shell format strings
2.10. conditions if and case
2.10.1. if
2.10.2. case
2.11. Loops for, while and until
2.11.1. for
2.11.2. while
2.11.3. until
2.12. Functions
2.12.1. Local variables
2.13. User interfaces
2.13.1. input
2.14. subshell
2.15. Example
2.15.1. 有趣的Shell
2.15.2. backup
2.15.3. CPU 核心数
2.15.4. Password
2.15.5. processes
2.15.6. Shell 技巧
2.15.7. to convert utf-8 from gb2312 code
2.15.8. 使用内存的百分比
2.15.9. 合并apache被cronlog分割的log文件
2.15.10. Linux 交集 差集 并集
3. 小众 Shell
3.1. fish shell
3.1.1. 安装 fish shell
3.1.2. 配置 fish
3.1.3. 环境变量
3.2. Z Shell
3.2.1. installing Z shell
3.2.2. Oh My ZSH!
3.2.3. Starting file
3.2.4. Prompting
3.2.5. Aliases
3.2.6. History
3.2.7. FAQ
3.3. Berkeley UNIX C shell (csh)
3.3.1.
3.4. KornShell
3.4.1.
4. Shell 命令
4.1. Help Commands
4.1.1. man - an interface to the on-line reference manuals
4.2. getconf - Query system configuration variables
4.3. test 命令
4.3.1. 判断目录
4.4. 目录和文件
4.4.1. dirname
4.4.2. filename
4.4.3. test - check file types and compare values
4.4.4. file — determine file type
4.4.5. stat
4.4.6. mkdir - make directories
4.4.7. rename
4.4.8. touch
4.4.9. truncate
4.4.10. ls - list directory contents
4.4.11. cp - copy files and directories
4.4.12. rm - remove files or directories
4.4.13. df - report file system disk space usage
4.4.14. du - estimate file space usage
4.4.15. tac - concatenate and print files in reverse
4.4.16. split - split a file into pieces
4.4.17. find - search for files in a directory hierarchy
4.5. package / compress and decompress
4.5.1. tar — The GNU version of the tar archiving utility
4.5.2. cpio - copy files to and from archives
4.5.3. gzip
4.5.4. zip, zipcloak, zipnote, zipsplit - package and compress (archive) files
4.5.5. bzip2, bunzip2 - a block-sorting file compressor
4.5.6. RAR
4.5.7. 7-Zip
4.5.8. RAR
4.5.9. xz, unxz, xzcat, lzma, unlzma, lzcat - Compress or decompress .xz and .lzma files
4.6. 日期和时间
4.6.1. 日期格式
4.6.2. -d --date=
4.6.3. 时间戳
4.6.4. RFC 2822
4.6.5. UTC
4.6.6. 字符串转日期
4.7. 数值与运算
4.7.1. 数值运算
4.7.2. seq - print a sequence of numbers
4.7.3. bc - An arbitrary precision calculator language
4.8. 文本处理
4.8.1. iconv - Convert encoding of given files from one encoding to another
4.8.2. 字符串处理命令expr
4.8.3. cat - concatenate files and print on the standard output
4.8.4. nl - number lines of files
4.8.5. tr - translate or delete characters
4.8.6. cut - remove sections from each line of files
4.8.7. printf - format and print data
4.8.8. Free `recode' converts files between various character sets and surfaces.
4.8.9. /dev/urandom 随机字符串
4.8.10. col - filter reverse line feeds from input
4.8.11. apg - generates several random passwords
4.8.12. head/tail
4.8.13. 反转字符串或文件内容
4.8.14. TAB符号与空格处理
4.8.15. grep, egrep, fgrep, rgrep - print lines matching a pattern
4.8.16. sort - sort lines of text files
4.8.17. uniq
4.8.18. awk
4.8.19. sed
4.9. 表格操作/行列转换
4.9.1. column - columnate lists
4.9.2. paste - merge lines of files
4.9.3. join
4.10. standard input/output
4.10.1. xargs - build and execute command lines from standard input
4.11. flock - manage locks from shell scripts
4.12. 进制转换 - 16进制 - 8进制 - 二进制
4.12.1. od - dump files in octal and other formats
4.12.2. hexdump, hd -- ASCII, decimal, hexadecimal, octal dump
4.12.3. xxd - make a hexdump or do the reverse.
4.12.4. binutils
4.13. 文件比较
4.13.1. diff
4.13.2. sdiff
4.13.3. diff3
4.14. ed, red - text editor
4.15. vim
4.15.1. vim 初始化
4.15.2. 查找与替换
4.15.3. 删除操作
4.15.4. 插入文件
4.15.5. 批处理
4.15.6. line()
4.15.7. set fileformat
4.15.8. 空格与TAB转换
4.16. Wget - The non-interactive network downloader.
4.16.1. Logging and input file
4.16.2. 下载相关参数
4.16.3. HTTP options (HTTP 选项)
4.16.4. Recursive download
4.16.5. --no-passive-ftp disable the "passive" transfer mode.
4.16.6. 下载一组连续的文件名
4.17. CURL - transfer a URL
4.17.1. 基本用法
4.17.2. 提交表单数据
4.17.3. 上传文件
4.17.4. connect-timeout
4.17.5. max-time
4.17.6. compressed
4.17.7. 代理服务器
4.17.8. -w, --write-out <format> 输出格式定义
4.17.9. -A/--user-agent <agent string>
4.17.10. referer
4.17.11. -v
4.17.12. -o, --output FILE Write output to <file> instead of stdout
4.17.13. -L, --location
4.17.14. -H/--header <line> Custom header to pass to server (H)
4.17.15. curl-config
4.17.16. 指定网络接口或者地址
4.17.17. Cookie 处理
4.17.18. Restful 应用 JSON 数据处理
4.17.19. 访问自签名证书
4.17.20. HTTP2
4.17.21. FAQ
4.18. expect
4.18.1. 模拟登录 telnet 获取Cisco配置
4.18.2. 模拟登录 ssh
4.18.3. SCP
4.18.4. openssl 例子
4.19. expect-lite - quick and easy command line automation tool
4.20. sshpass - noninteractive ssh password provider
4.21. Klish - Kommand Line Interface Shell (the fork of clish project)
4.21.1. 安装Klish
4.21.2. 为用户指定clish作为默认Shell
4.21.3. FAQ
4.22. Limited command Shell (lshell)
4.23. TUI
4.23.1. screen - screen manager with VT100/ANSI terminal emulation
4.23.2. tmux — terminal multiplexer
4.23.3. byobu - wrapper script for seeding a user's byobu configuration and launching a text based window manager (either screen or tmux)
4.23.4. htop - interactive process viewer
4.23.5. elinks
4.23.6. chat
4.24. jq - Command-line JSON processor
4.24.1. --raw-output
4.25. asciinema 终端录屏
4.26. parallel - build and execute shell command lines from standard input in parallel
4.27. multitail
4.28. Logging
4.28.1. logger - a shell command interface to the syslog(3) system log module
4.29. Password
4.29.1. Shadow password suite configuration.
4.29.2. newusers - update and create new users in batch
4.29.3. chpasswd - update passwords in batch mode
4.29.4. sshpass - noninteractive ssh password provider
4.30. 信息摘要
4.30.1. cksum, sum -- display file checksums and block counts
4.30.2. md5sum - compute and check MD5 message digest
4.30.3.
4.31. envsubst - substitutes environment variables in shell format strings
6. Shell Terminal
6.1. terminal
6.1.1. resize - set TERMCAP and terminal settings to current xterm window size
6.1.2. tset, reset - terminal initialization
6.1.3. stty - change and print terminal line settings
6.2. tput
6.2.1. Change the prompt color using tput
6.3. dialog
6.3.1. --inputbox
6.4. whiptail - display dialog boxes from shell scripts
6.4.1. --msgbox
6.4.2. --infobox
6.4.3. --yesno
6.4.4. --inputbox
6.4.5. --passwordbox
6.4.6. --textbox
6.4.7. --checklist
6.4.8. --radiolist
6.4.9. --menu
6.4.10. --gauge
1. 附录
1.1. 参考文献

表格清单

2.1. 文件目录表达式
2.2. 字符串表达式
2.3. 组合表达式

范例清单

2.1. A "Power User" Prompt
2.2. A Prompt the Width of Your Term
2.3. The Elegant Useless Clock Prompt
2.4. Basic conditional example if .. then
2.5. Conditionals with variables
2.6. case
2.7. Functions with parameters sample
2.8. Using select to make simple menus
2.9. Using the command line
2.10. Reading user input with read
2.11. read
2.12. random password
4.1. backup(find + tar)
4.2. example for expect
4.3. example for expect
4.4. example 1
4.5. *.exp
4.6. parallel - build and execute shell command lines from standard input in parallel
6.1. whiptail - yesno
6.2. whiptail - inputbox
6.3. whiptail - passwordbox
6.4. whiptail - passwordbox
6.5. whiptail - example 1
6.6. whiptail - radiolist

1. 有趣的 Shell 应用

1.1. Ascii 星球大战电影

         
neo@MacBook-Pro-M2 ~>  nc towel.blinkenlights.nl 23        
        
        

1.2. 天气预报

         
neo@MacBook-Pro-M2 ~> curl http://wttr.in/
Weather report: Shenzhen, China

        \  /       Partly cloudy
    _ /"".-.     18 °C          
        \_(   ).   ↘ 26 km/h      
        /(___(__)  10 km          
                0.0 mm         
                                                        ┌─────────────┐                                                       
┌──────────────────────────────┬───────────────────────┤  Mon 09 Jan ├───────────────────────┬──────────────────────────────┐
│            Morning           │             Noon      └──────┬──────┘     Evening           │             Night            │
├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤
│               Overcast       │               Overcast       │               Overcast       │               Overcast       │
│      .--.     17 °C          │      .--.     21 °C          │      .--.     19 °C          │      .--.     18 °C          │
│   .-(    ).   ↓ 7-9 km/h     │   .-(    ).   ↙ 7-8 km/h     │   .-(    ).   ↙ 7-10 km/h    │   .-(    ).   ↙ 7-10 km/h    │
│  (___.__)__)  10 km          │  (___.__)__)  10 km          │  (___.__)__)  10 km          │  (___.__)__)  10 km          │
│               0.0 mm | 0%    │               0.0 mm | 0%    │               0.0 mm | 0%    │               0.0 mm | 0%    │
└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘
                                                        ┌─────────────┐                                                       
┌──────────────────────────────┬───────────────────────┤  Tue 10 Jan ├───────────────────────┬──────────────────────────────┐
│            Morning           │             Noon      └──────┬──────┘     Evening           │             Night            │
├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤
│  _`/"".-.     Patchy rain po…│  _`/"".-.     Patchy rain po…│      .-.      Light drizzle  │               Overcast       │
│   ,\_(   ).   17 °C          │   ,\_(   ).   18 °C          │     (   ).    17 °C          │      .--.     16 °C          │
│    /(___(__)  ↙ 6-9 km/h     │    /(___(__)  ↙ 8-9 km/h     │    (___(__)   ↙ 9-13 km/h    │   .-(    ).   ↙ 7-10 km/h    │
│      ‘ ‘ ‘ ‘  10 km          │      ‘ ‘ ‘ ‘  10 km          │     ‘ ‘ ‘ ‘   2 km           │  (___.__)__)  10 km          │
│     ‘ ‘ ‘ ‘   0.1 mm | 61%   │     ‘ ‘ ‘ ‘   0.1 mm | 81%   │    ‘ ‘ ‘ ‘    0.7 mm | 83%   │               0.0 mm | 0%    │
└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘
                                                        ┌─────────────┐                                                       
┌──────────────────────────────┬───────────────────────┤  Wed 11 Jan ├───────────────────────┬──────────────────────────────┐
│            Morning           │             Noon      └──────┬──────┘     Evening           │             Night            │
├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤
│  _`/"".-.     Patchy rain po…│  _`/"".-.     Patchy rain po…│    \  /       Partly cloudy  │    \  /       Partly cloudy  │
│   ,\_(   ).   16 °C          │   ,\_(   ).   17 °C          │  _ /"".-.     18 °C          │  _ /"".-.     17 °C          │
│    /(___(__)  ↙ 5-7 km/h     │    /(___(__)  ↙ 5-6 km/h     │    \_(   ).   ↙ 7-9 km/h     │    \_(   ).   ← 7-11 km/h    │
│      ‘ ‘ ‘ ‘  10 km          │      ‘ ‘ ‘ ‘  10 km          │    /(___(__)  10 km          │    /(___(__)  10 km          │
│     ‘ ‘ ‘ ‘   0.1 mm | 87%   │     ‘ ‘ ‘ ‘   0.1 mm | 84%   │               0.0 mm | 0%    │               0.0 mm | 0%    │
└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘

Follow @igor_chubin for wttr.in updates