- Shell
- History
- TAB key
- Alias
- Wildcard characters
- Redirection function
Shell
shell是一个命令解释器,提供用户和机器之间的交互;Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。
History
Role: Bash built-in commands, Managing command history
Grammar:
history [n] 不加数字默认列出内存中HISTSIZE参数设定条数的历史命令history [参数]
Parameters:
c:清除目前内存中的命令记录
"!" Use of:
!n 执行命令历史中的第n条命令记录(ps:"!"与"n"之间没有空格)!! 执行命令历史中的最后一条命令!$ 代表上一条命令里最后一个参数!命令keywords 执行以keywords开头最后一次执行的命令
Configuration file:
~/.bash_historyhistory记录存档文件每个用户家目录下拥有自己独立的记录文件/etc/profileHISTSIZE参数配置文件 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S" 记录命令执行的时间chattr +a ~/.bash_history 永久保存
History command Shortcut keys
# ctrl+r,可查询history中的关键字来查找命令(reverse-i-search)‘cat‘: cat .bash_history |wc -l## 操作步骤:# 1. 按下ctrl+r进入搜索界面# 2. 输入命令关键字,搜索出最近执行的命令# 3. 按下enter键执行此命令# 4. 按左右键可编辑此命令# 5. 按上下键可查看此命令前后输入的命令历史
TAB key
Role:
命令及文档名称补全
How to use:
输入命令或文档名称头几个字母按一次或两次Tab键即可补全或列出所有符合条件的命令、路径或文件
Instance:
# 命令补全# 连续按两次Tab,显示所有匹配结果if[Tab][Tab]if ifcfg ifconfig ifdown ifenslave ifnames ifup# 此处依然保留原输入内容,以便继续输入if# 路径及文件名称补全# 路径用Tab补全,最后文件名列出匹配结果ls /etc/syscon[Tab]fig/net[Tab]work-s[Tab]cripts/ifcfg-[Tab][Tab]ifcfg-eth0 ifcfg-lo# 最后依然保留原输入内容ls /etc/sysconfig/network-scripts/ifcfg-
Alias
Role:
为命令行设置别名
Grammar:
alias 查询别名alias alias_cmd=‘command line‘ 添加别名配置文件: ~/.bashrc
Instance:
# 查询alias别名aliasalias cp=‘cp -i‘alias l.=‘ls -d .* --color=auto‘alias ll=‘ls -l --color=auto‘alias ls=‘ls --color=auto‘alias mv=‘mv -i‘alias rm=‘rm -i‘alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘# 临时添加aliasalias vi=‘vim‘which vialias vi=‘vim‘ /usr/bin/vim# 配置文件~/.bashrc,可永久配置aliasvi ~/.bashrc******************************************************# User specific aliases and functions......alias mv=‘mv -i‘## 新增alias,并保存退出alias vi=‘vim‘......******************************************************
Note:
# "source"或"."重新加载配置文件source ~/.bashrc# /etc/profile.d/目录下可以放置需要开机启动的shell脚本文件ls /etc/profile.d/colorls.csh glib2.csh lang.csh less.csh vim.csh which2.shcolorls.sh glib2.sh lang.sh less.sh vim.sh
Wildcard characters
Wildcard meaning:
* 0个或多个任意字符? 1个任意字符# 命令中使用的话跟shell中一样,后面的东西不生效
Instance:
# "*"匹配任意个任何字符# 匹配1开头的所有文件ls 1*1 12 124.log 13.log# "?"匹配1个任意字符# 匹配"1"+"1任意字符"+".log"的文件ls 1?.log13.log# "#"注释掉其之后的命令# "#"注释掉了第二条命令的1*匹配ls 1? 1*1 12 12 124.log 13.logls 1? #1*12
Redirection function
Role:
把标准输出、标准输入、错误输出重定向至文件或设备
Symbols and their meanings:
> 输出重定向>> 输出追加重定向< 输入重定向<< 输入追加重定向1 正确信息标准输出2 错误信息标准输出
Grammar:
1>right.destination 2>error.destination 正确及错误信息分别存放1>right.destination 2>&1 正确及错误信息放在一起2>/dev/null 把错误信息输出到系统黑洞
Instance:
# output redirection >, >> # Output redirection, then add content with append redirect cat/etc/passwd >./passwdecho "This is a new line" >>./passwd# with Tai l command to see the last three lines of content and find what's new tail-3./passwdtest:x:500:500::/home/test:/bin/bashsu-test:x:501:501::/home/su-test:/bin/ Bashthis is a new line# chase emphasis will overwrite the original content of the file echo "This is the only content will leave" >./passwdcat./passwdthis are only content Will leave# input redirection <, << # cat can create files and enter content in this form, and finally Ctrl+d exit cat > Stdinyes, I can do the same thing like Vimnow E XIT by pushing Ctrl and D together# View results cat Stdinyes, I can do the same thing like Vimnow exit by pushing Ctrl and D Toge ther# using files as standard input to Stdincat > stdin </etc/passwd# View two files different, found to be consistent diff./stdin-redirect/etc/passwd# Put the correct and error information in a file record # The following statement has the correct output and error output ll stdin-redirect i_am_not_existls:cannot Access i_am_not_exist:no such file or Direc tory-rw-r--r--1 root root 934 May 6 22:37 stdin-redirect# redirect it to 1 files ll stdin-redirect i_am_not_exist 1>list.log 2> ; &1cat List.logls:cannot Access I_am_not_exist:no sUch file or directory-rw-r--r--1 root root 934 May 6 22:37 stdin-redirect# output error message to System black Hole # Enter the error message or correct information into the black hole LS Stddddls:can Not access stdddd:no such file or directoryls stdddd 2>/dev/null
Linux Shell Basics-1