Shell基本概念
基本概念
终端
如果终端不显示login:
提示,试一下CONTROL + Q
组合键。
退出终端可以用组合键CONTROL + D
(即EOF信号),或是exit
命令。
特殊字符
& ; | * ? ^ $ # % ! ~ + ' " ` < > [ ] ( ) {} / \
- 反斜杠可以转义单个特殊字符。
- 单引号可以包起一串字符转义。
对于特制字符,如:
-
CONTROL + H
:删除字符 -
CONTROL + U
:删除行 -
CONTROL + M
: - 等等...
转义的方式是在前面加上CONTROL + V
:
$ echo 'xxxxxxxxx^U' xxxxxxxxx $ echo xxxxxxxx^U | od -c 0000000 x x x x x x x x 025 \n 0000012
通配符
-
?
匹配单个字符 -
*
匹配零到多个字符 -
[]
匹配字符集:[aeiou]
,[0-9a-zA-Z]
输入输出管道
向两个方向输出:tee
tee把从标准输入读到的内容复制成两份:一份输出到标准输出, 另一份输出到指定的文件。
例,一份写到文件,另一份输出,再管道给grep:
$ echo 'Send to stdout ...' | tee out.txt | grep 'stdout' Send to stdout ... $ cat out.txt Send to stdout ...
命令参数
结束选项--
--
表示以后的所有参数都不是选项:
$ ls -- -l # -l是方便名,不是选项开关
简单工具
命令行相关工具
命令行记录器:script
一般地,我们可以通过在终端上敲入script来启动它。
pungki@dev-machine:~$ script Script started, file is typescript pungki@dev-machine:~$
-
不指定文件的话,你会看到在当前目录,有了一个叫
typescript
的文件。 -
可以指定文件来保存打印稿的结果。如:
script mylog
。 -
选项
-q
是安静模式。用户登录时完全不会察觉到script命令的运行。 -
选项
-a
是追加,不会覆盖以往的记录。 -
敲击
ctrl+d
或exit
,可以停止记录。你会发现写入记录是发生在停止之后的。
当你再次收到一条命令提示,就说明终端上出现的任何东西,都将被记录下来。
想要script在登录时就生效,可以将其加在shell profile。如果你在使用bash,放进
profile
去吧:
$ vi ~/.profile # run the script command to record everything # use -q for quite and -a option to append the script # /usr/bin/script -qa /usr/local/script/log_record_script
资源定位工具:which
在确定程序名字的前提下,which工具显示程序所在的绝对路径。
$ which ssh /usr/bin/ssh
使用which程序的目的是检查你调用的程序是不是真正想用的版本和程序。 而不是另外一个同名或是不同位置的程序。
资源定位工具:whereis
在确定程序名字的前提下,whereis
会在系统的标准路径(而不是用户指定的)下,
搜索与工具相关的文件。
举例来说,获得ls
和kill
命令的二进制文件/资源以及帮助页:
root@tecmint:~# whereis ls ls: /bin/ls /usr/share/man/man1/ls.1.gz root@tecmint:~# whereis kill kill: /bin/kill /usr/share/man/man2/kill.2.gz /usr/share/man/man1/kill.1.gz
注意:当需要知道二进制文件保存位置时有用.
为命令起别名:alias 与 unalies
alias
是一个系统自建的shell命令,允许你为名字比较长的或者经常使用的命令指定别名
。
我经常用ls -l
命令,它有五个字符(包括空格)。于是我为它创建了一个别名l
。
root@tecmint:~# alias l='ls -l'
试试它是否能用:
root@tecmint:~# l total 36 drwxr-xr-x 3 tecmint tecmint 4096 May 10 11:14 Binary drwxr-xr-x 3 tecmint tecmint 4096 May 21 11:21 Desktop drwxr-xr-x 2 tecmint tecmint 4096 May 21 15:23 Documents drwxr-xr-x 8 tecmint tecmint 4096 May 20 14:56 Downloads drwxr-xr-x 2 tecmint tecmint 4096 May 7 16:58 Music drwxr-xr-x 2 tecmint tecmint 4096 May 20 16:17 Pictures drwxr-xr-x 2 tecmint tecmint 4096 May 7 16:58 Public drwxr-xr-x 2 tecmint tecmint 4096 May 7 16:58 Templates drwxr-xr-x 2 tecmint tecmint 4096 May 7 16:58 Videos
去掉l
别名,要使用unalias
命令:
root@tecmint:~# unalias l
再试试:
root@tecmint:~# l bash: l: command not found
关键字搜索工具:whatis
whatis工具按关键字搜索与关键字完全匹配的信息:
$ apropos who who (1) - show who is logged on
需要建立whatis数据库与维护该数据库的makewatis工具, 此任务一般由cron定时任务每天更新最新信息。
关键字搜索工具:apropos
对于不确定具体名字的内容,可以用apropos工具搜索关键字相关的内容:
$ apropos who at.allow (5) - determine who can submit jobs via at or batch at.deny (5) - determine who can submit jobs via at or batch bsd-from (1) - print name of those who have sent mail from (1) - print name of those who have sent mail ...
apropos需要建立whatis数据库与维护该数据库的makewatis工具, 此任务一般由cron定时任务每天更新最新信息。
man工具加上选项-k
得到与apropos的效果相同。
文件搜索工具:slocate
slocate在本地系统搜索文件:
$ slocate motd /lib/security/pam_motd.so /usr/share/man/man5/motd.5.gz /etc/motd
在使用slocate和updatebd工具时,需要更新slocate数据库。更新工具一般由cron脚本 每天更新一次。
常用工具
帮助文件:man
man程序用来显示程序的帮助文档。对于不同的类型的程序按不同的编号分类:
-
1
:可执行程序或 shell 命令 -
2
:系统调用(内核提供的函数) -
3
:库调用(程序库中的函数) -
4
:特殊文件(通常位于 /dev) -
5
:文件格式和规范,如 /etc/passwd -
6
:游戏 -
7
:杂项(包括宏包和规范,如 man(7),groff(7)) -
8
:系统管理命令(通常只针对 root 用户) -
9
:内核例程 [非标准
例如,
man 1 printf # 显示shell命令printf的帮助文档 man 3 printf # 显示程序库中printf的帮助文档
指定用浏览器打开帮助文档,需要安装groff
:
sudo apt-get install groff man -Hfirefox printf
通过环境变量PAGER
指定分布器,比如可以得到更好的高亮。
方法1:
~/.LESS_TERMCAP
:
export LESS_TERMCAP_mb=$(tput bold; tput setaf 2) # green export LESS_TERMCAP_md=$(tput bold; tput setaf 6) # cyan export LESS_TERMCAP_me=$(tput sgr0) export LESS_TERMCAP_so=$(tput bold; tput setaf 3; tput setab 4) # yellow on blue export LESS_TERMCAP_se=$(tput rmso; tput sgr0) export LESS_TERMCAP_us=$(tput smul; tput bold; tput setaf 7) # white export LESS_TERMCAP_ue=$(tput rmul; tput sgr0) export LESS_TERMCAP_mr=$(tput rev) export LESS_TERMCAP_mh=$(tput dim) export LESS_TERMCAP_ZN=$(tput ssubm) export LESS_TERMCAP_ZV=$(tput rsubm) export LESS_TERMCAP_ZO=$(tput ssupm) export LESS_TERMCAP_ZW=$(tput rsupm)
~/.bashrc
:
# Get color support for 'less' export LESS="--RAW-CONTROL-CHARS" # Use colors for less, man, etc. [[ -f ~/.LESS_TERMCAP ]] && . ~/.LESS_TERMCAP
Termcap is a library that Less uses to access the terminal. Termcap is largely obsolete, having been replaced by Terminfo, but Terminfo offers a Termcap compatibility interface to applications. Less is content with the Termcap interface and uses that.
The Termcap library is a description of the terminal's facilities. Each facility is identified by a two-letter (or more generally two-character) code. For example, hc
identifies hardcopy terminals (i.e. printers, not screens); co
is the number of columns; md
starts displaying bold text. Each capability has a value, which can be a boolean (as with hc
), an integer (as with co
) or a string (as with md
). Many of the strings are escape sequences that applications can send to the terminal to achieve a certain effect.
Why escape sequences? Because the interface between the terminal and the application is a character stream (more precisely, one character stream in each direction: one for user input, one for output to display). When an application writes a character to the terminal, it is usually displayed. A few characters have a different behavior: they are control characters, which do things like moving the cursor around, switching display attributes, etc. There are a lot more commands than control characters, so most commands are accessed by escape sequences, which begin with a special character (often the escape character, hence the name).
For example, when Less wants to display some bold text, it looks up the value of the md
capability. This is a string, which Less writes to the terminal. The terminal recognizes this string as an escape sequence, and adjusts its internal state so that subsequent characters will be displayed in bold.
In the early days of hardware terminals, different brands had different escape sequences and capabilities; the Termcap database and interface was invented so that applications wouldn't have to know about every terminal model. Nowadays most terminal emulators have very similar capabilities, but the Termcap or Terminfo database is still useful to cope with minor differences.
The LESS_TERMCAP_*
variables can be set in the environment or in the .lesskey
file. It provides Less with alternative values for Terminal capabilities. When Less wants to use a terminal capability, say switch to bold, it first checks if there is a LESS_TERMCAP_md
variable. If this variable exists, Less uses its value as the escape sequence to switch to bold. If not, it uses the value from the Termcap database. This mechanism allows the user to override Termcap database settings for Less.
The most useful LESS_TERMCAP_*
settings are escape sequences. You can map attributes to different attributes. You can use the tput
command to look up the value of a capability for the current terminal in the system's Termcap or Terminfo database. You can use escape sequences directly if you don't mind being terminal-dependent. For example, this setting tells Less to display in bold red when instructed to display in bold:
LESS_TERMCAP_md=$(tput md; tput AF 1)
or if your tput command doesn't support Termcap names:
LESS_TERMCAP_md=$(tput bold; tput setaf 1)
Man sends Less text with some very simple formatting that can only express bold and italics. In addition, Less uses various formatting capabilities for its internal use, such as to highlight search results and to display the mode line at the bottom. Here are some of the escape sequences that Less uses (I only list capabilities that it is reasonably useful to remap):
termcap terminfo ks smkx make the keypad send commands ke rmkx make the keypad send digits vb flash emit visual bell mb blink start blink md bold start bold me sgr0 turn off bold, blink and underline so smso start standout (reverse video) se rmso stop standout us smul start underline ue smue stop underline
To show output in color, use the setaf capability (or AF with Termcap).
The LESS_TERMCAP_* settings are not mentioned in the LESS documentation.
方法2:
$ mkdir ~/.terminfo/ && cd ~/.terminfo
Now get the terminfo description:
$ wget http://nion.modprobe.de/mostlike.txt
Now compile it using tic (the terminfo entry-description compiler)
$ tic mostlike.txt
(you may want to delete the mostlike.txt file after compiling)
And then just define an alias in the rc file of your favorite shell.
$ alias man="TERMINFO=~/.terminfo/ LESS=C TERM=mostlike PAGER=less man"
If you want to modify the terminfo file, use infocmp mostlike to get the content of it later.
方法三:
export PAGER=most
如果不习惯most
的按键(不能使用vi风格),可以手动指定高亮。
在~/.bashrc
:
# Less Colors for Man Pages export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking export LESS_TERMCAP_md=$'\E[01;38;5;74m' # begin bold export LESS_TERMCAP_me=$'\E[0m' # end mode export LESS_TERMCAP_se=$'\E[0m' # end standout-mode export LESS_TERMCAP_so=$'\E[38;5;246m' # begin standout-mode - info box export LESS_TERMCAP_ue=$'\E[0m' # end underline export LESS_TERMCAP_us=$'\E[04;38;5;146m' # begin underline
密码生成工具:mkpasswd
根据指定的长度,产生一个难猜的随机密码。
[avishek@tecmint ~]$ mkpasswd -l 10 zI4+Ybqfx9
注意:-l 10
产生一个10个字符的随机密码,而-l 20
产生20个字符的密码,它可以设置
为任意长度来取得所希望的结果。这个命令很有用,经常在脚本语言里使用来产生随机的
密码。你可能需要yum
或apt
expect
包来使用这个命令。
[avishek@tecmint ~]$ yum install expect
或
[avishek@tecmint ~]$ apt-get install expect