shell 学习(20230411)
1 uniq
uniq命令通过消除重复内容,从给定的输入中找到单一行。
例如:uniq.txt文档如下:
[root@spark10 linux_test]# cat uniq.txt
start
end
first
ok
ok
is
is
is
A、去除重复项: uniq uniq.txt
[root@spark10 linux_test]# uniq uniq.txt
start
end
first
ok
is
或者: sort -u uniq.txt(sort命令用于排序)
[root@spark10 linux_test]# sort -u uniq.txt
end
first
is
ok
start
B、每行重复的次数:uniq -c uniq.txt
[root@spark10 linux_test]# uniq -c uniq.txt
1 start
1 end
1 first
2 ok
3 is
C、查看行有重复的:uniq -d uniq.txt
[root@spark10 linux_test]# uniq -d uniq.txt
ok
is
2 sed
A、sed ‘s/[^/n]/&\n/g’
在给个字符后面追加一个换行符,使得每行只出现一个字符
B、sed ‘/^s/d’
删除最后的空行
3 tr
tr -d ‘\n’
将输入的空格和换行符删除。
4 /tmp存放临时数据(/tmp目录的内存在系统重启后被清空)
5 生产随机120k大小的文件
dd if=/dev/zero bs=120k count=1 of=123.file
[root@spark10 ~]# dd if=/dev/zero bs=120k count=1 of=123.file
1+0 records in
1+0 records out
122880 bytes (123 kB) copied, 0.000314866 s, 390 MB/s
文件切割split
将文件123.file切割成10k大小,新的文件用xaa、xab、xac等命令
split -b 10k 123.file
[root@spark10 ~]# split -b 10k 123.file
[root@spark10 ~]# ls
123.file dead.letter Documents Music Pictures Templates xaa xac xae xag xai xak
anaconda-ks.cfg Desktop Downloads original-ks.cfg Public Videos xab xad xaf xah xaj xal
[root@spark10 ~]#
6 自动化输入
A、读取交互式脚本并演示(读取输入的用户名和密码)
[root@spark10 linux_test]# cat auto.sh
#!/bin/bash
#文件名:auto.sh
read -p "Enter username:" username ;
read -p "Enter Password:" passwd ;
echo You entered username is $username, Password is $passwd;
[root@spark10 linux_test]# echo -e "admin\npassword\n" | ./auto.sh
You entered username is admin, Password is password
B、将echo的输入重定向到文本
[root@spark10 linux_test]# echo -e "admin\npassword\n" > input.txt
[root@spark10 linux_test]# cat input.txt
admin
password
C、从文件中导入交互数据
[root@spark10 linux_test]# ./auto.sh < input.txt
You entered username is admin, Password is password
[root@spark10 linux_test]#
D、使用expect 脚本实现交互式自动化
[root@spark10 linux_test]# cat auto_expect.sh
#!/usr/bin/expect
#文件名:auto_expect.sh
spawn ./auto.sh
expect "Enter username:"
send "admin\n"
expect "Enter Password:"
send "password\n"
expect eof
[root@spark10 linux_test]# ./auto_expect.sh
spawn ./auto.sh
Enter username:admin
Enter Password:password
You entered username is admin, Password is password
[root@spark10 linux_test]#
spawn参数指定需要自动化执行的命令或者脚本
expect参数指定需要等待的消息,这里指:Enter username:
send参数是要发送的消息;这里指:admin\n
expect eof 参数指定交互结束