实用技巧:如何更有效率的使用Linux键盘
正确的使用键盘上的符号可以在帮助你在编写简单脚本时充分应用各种技巧
[Tab] 用于自动补齐一个命令和路径或文件名
[[email protected] ~]# user 在输入user 后连按键盘上的[Tab]键两下,可以将以user开头的命令列出
[[email protected] ~]# usera 在输入usera后连按键盘上的[Tab]键一下,可以将以usera开头的命令useradd命令自动补齐
[[email protected] ~]# useradd 在输入useradd后连按键盘上的[Tab]键两下,可以将当前目录下的所有文件和目录列出
~ 这个符号代表用户自己的home目录
[[email protected] ~]# cd /
[[email protected] ~]# pwd
[[email protected] ~]# cd ~
[[email protected] ~]# pwd
! 在一个命令前加上这个符号可以在命令的历史记录中调用上一个以这个命令开头的命令 [[email protected] ~]# find /root/install.log
[[email protected] ~]# file /root/install.log
[[email protected] ~]# !f 在个命令将调用file /root/install这个查找文件的命令
[[email protected] ~]# !fi 在个命令将调用file /root/install这个查找文件的命令
[[email protected] ~]# !fin 在个命令将调用find /root/install这个查找文件的命令
$ 用于引用一个变量
[[email protected] ~]# myhost=www.liuziyang.cn
[[email protected] ~]# echo $myhost
& 在一个命令结尾表示将任务放在后台运行
% 用于表示一个任务(任务不是进程)编号
[[email protected] ~]# while true; do echo hello >> /dev/tty2 ; sleep 1; done&
[[email protected] ~]# while true; do echo www.liuziyang.cn >> /dev/tty2 ; sleep 1; done&
[[email protected] ~]# [ctrl]+[alt]+[F2] 这里表示切换到第二个控制台/dev/tty2,查看输出结果
在每执行下面的一个命令后都在[ctrl]+[alt]+[F1]和[F2]间进行切换来看一个结果的变化
[[email protected] ~]# jobs
[[email protected] ~]# fg %1
[[email protected] ~]# [ctrl]+z 这里按键盘上的[ctrl]和z这样的组合
[[email protected] ~]# jobs [[email protected] ~]# kill %2
[[email protected] ~]# jobs
[[email protected] ~]# bg %1
[[email protected] ~]# jobs
[[email protected] ~]# kill %1 `` 表示一个命令或脚本 '' 表示一个字符串 "" 表示一个字符串
注意观查每一个命令的输出结果
[[email protected] ~]# echo `hostname`
[[email protected] ~]# echo 'hostname'
[[email protected] ~]# echo "hostname" | 管道符号,用于在一个命令语句后进行过滤
[[email protected] ~]# ls -l /etc | more
[[email protected] ~]# cat /etc/passwd | awk -F: '{PRint $7}' | sort | uniq
^ 匹配字符串首
$ 匹配字符串尾
> 定向符号
< 定向符号
>> 追加符号
注意每个命令的输出结果对比
[[email protected] ~]# grep ^root < /etc/passwd > result1.txt
[[email protected] ~]# cat result1.txt
[[email protected] ~]# grep bash$ < /etc/passwd > result1.txt
[[email protected] ~]# grep bash$ < /etc/passwd > result2.txt
[[email protected] ~]# cat result1.txt
[[email protected] ~]# cat result2.txt
[[email protected] ~]# grep root < /etc/passwd >> result2.txt
[[email protected] ~]# cat result2.txt
先写这些吧,多动手实践就能理解这些命令并可以应用到实际中。