1.shell脚本(计算机壳层)
在计算机科学中,Shell俗称壳(用来区别于核),是指“为使用者提供操作界面”的软件(命令解析器)。它类似于DOS下的command.com和后来的cmd.exe。它接收用户命令,然后调用相应的应用程序。
同时它又是一种程序设计语言。作为命令语言,它交互式解释和执行用户输入的命令或者自动地解释和执行预先设定好的一连串的命令;作为程序设计语言,它定义了各种变量和参数,并提供了许多在高级语言中才具有的控制结构,包括循环和分支。
shell 命令重新初始化用户的登录会话。当给出该命令时,就会重新设置进程的控制终端的端口特征,并取消对端口的所有访问。然后 shell 命令为用户把进程凭证和环境重新设置为缺省值,并执行用户的初始程序。根据调用进程的登录用户标识建立所有的凭证和环境。
2.diff命令的使用
diff 命令比较文本文件。它能比较单个文件或者目录内容。 diff 命令只有当输入为文本文件时才有效。 如果指定了 Directory1
和 Directory2 参数,diff 命令比较两个目录下名字相同的文本文件。列出不同的二进制文件、公共子目录和只在一个目录出现的文件。
diff命令用于比较两个文件的差异,它可以被制作成补丁文件,使用patch命令对相应的文件打补丁。
mkdir /yyh
ls
vim test
hello westos
cp test test1
vim test1
hello linux
lee
diff test test1
vim test1
diff -b test test1
diff 命令是用来比较两个文件或目录的不同
diff [options] target1 target2
diff file1 file2文件
diff direcory1 directory2目录
a 表示添加 ----add
c 表示更改 ----change
d 表示删除 ----delete
表示第一个文件中的内容
表示第二个文件中的内容, — 分割线
diff中常用的参数
-b 或 --ignore-space-change 不检查空格字符的不同
-B 或 --ignore-blank-lines 不检查空白行
-c 显示全部内文,并标出不同之处
-i 或 --ignore-case 不检查大小写的不同
-q 或 --brief :仅显示有无差异,不显示详细的信息
-r 或 --recursive :比较子目录中的文件
-u 以合并的方式来显示文件内容的不同
-q和-c的不同之处:-c更智能一些
3.patch打补丁
yum install patch
patch --help
4.cut 命令多用与字符截取
cut -d指定分隔符
cut -f 1,7|1-7 指定截取的列
cut -c 1,4|1-4 指定截取的字符位置
实验步骤:
cat passwd
cut -d : -f 1,3 passwd
cut -d : -f 1-3 passwd
cut -c 1-3 passwd
cut -c 1,3 passwd
5.sort多用于字符排序
sort -n
纯数字排序
sort -r
倒序
sort -u
去掉重复数字
sort -o
输出到指定文件中
sort -t
指定分隔符
sort -k
指定要排序的列
实验步骤:
sort test
sort -n test
sort -nr test
sort -nru test
sort -nru test -o westos
cat westos
vim test
加上分隔符,前面再加一列
cat test
sort -n test
sort -t : -k 2 -n test
6.uniq对重复字符做相应的处理
只能在排序之后进行
uniq -u
显示唯一的行
uniq -d
显示重复的行
uniq -c
每行显示一次并统计重复次数
sort n test | uniq -c
sort n test | uniq -d
sort n test | uniq -u
7.如何检测谁ssh连接自己的次数最多?
last 查看谁都连过
last -i 查看并且显示ip
last -i | grep 0.0.0.0 -v 将0.0.0.0ip的全部过滤掉
last -i | grep 0.0.0.0 -v | cut -d " " -f 14 截取14列
last -i | grep 0.0.0.0 -v | grep wtmp -v | cut -d " " -f 14 | uniq -c | sort -nr -t " " -k 5 | head -n 1
last -i | grep 0.0.0.0 -v | grep wtmp -v | cut -d " " -f 14 | uniq -c | sort -rn | head -n 1
8.test命令
(=和==均是相等的意思,与c语言不一样)
test 命令和 [] 等同
test "$A" == "$B" 等同 [ "$A" == "$B" ]
[ "$A" = "$B" ]相等
[ "$A" != "$B" ]不相等
[ "$A" -eq "$B" ]等于
[ "$A" -ne "$B" ]不等于
[ "$A" -le "$B" ]小于等于
[ "$A" -lt "$B" ]小于
["$A" -ge "$B" ]大于等于
["$A" -gt "$B" ]大于
["$A" -ne "$B" -a "$A" -gt "$B" ]与条件
["$A" -ne "$B" -o "$A" -gt "$B" ]或条件
[-z "$A" ]判断是否为空,空为真,非空为假
[-n "$A" ]和条件相反就是真,非空为真,空为假
["file1" -ef "file2" ] 判断前后两个文件是否互相为硬链接
["file1" -nt "file2" ]判断前面的硬链接是否与新
["file1" -ot "file2" ]判断前面的硬链接是否旧
[-e "file" ]判断文件是否存在
[-f "file" ]判断文件是否为普通文件
[-L "file" ]是否为软链接
[-S "file" ]判断是否为套接字
[-b "file" ]判断是否为块设备
[-d "file" ]判断是否为目录
[-c "file" ]判断是否为字符设备
实验步骤:
a=1
b=1
test "$a" = "$b" && echo yes || echo no
a=2
test "$a" = "$b" && echo yes || echo no
["$a"="$b"] && echo yes || echo no
["$a"!="$b"] && echo yes || echo no
[!"$a"="$b"] && echo yes || echo no
["$a"="$b"] && echo yes || echo no
["$a" -eq "$b"] && echo yes || echo no
["$a" -ne "$b"] && echo yes || echo no
["$a" -lt "$b"] && echo yes || echo no
["$a" -le "$b"] && echo yes || echo no
["$a" -gt "$b"] && echo yes || echo no
["$a" -ge "$b"] && echo yes || echo no
cd /yyh
ls
ln /yyh/test /yyh/test0
["/mnt/westos" -ef "/mnt/westos1"] && echo yes || echo no
[-e "file"]是否是一个文件
[-f "file"]是否是一个普通文件
[-L "file"]是否是一个软链接文件
[-S "file"]是否是一套接字个文件
[-b "file"]是否是一个块设备文件
[-c "file"]是否是一个字符设备文件
yum install mariadb-server -y
systemctl start mariadb
ls -l /var/lib/mysql/mysql.sock
[ -S "/var/lib/mysql/mysql.sock"] && echo yes || echo no
[ -b "/dev/vda"] && echo yes || echo no
[ -d "/dev/vda"] && echo yes || echo no
[ -c "/dev/pts/0"] && echo yes || echo no
9.tr命令(大小写转换)
vim test.sh
echo $1 | tr 'a-z' 'A-Z'
sh test haha