{6.shell脚本命令}
[1.diff]
diff file file1 **比较两个文件的不同
-c **显示周围的行
-u **按照一格式统一输出生成补丁
-r **比较两个目录中文件的不同
patch file file.patch **打补丁
-b **备份原文件
[2.grep]
grep 关键字符 文件|目录 **在文件或目录中查找含有关键字的行
grep -i **忽略大小写
-n **显示关键字所在的行
-c **显示过滤结果的个数
-v **反向过滤
-E "关键字1|关键字2" **过滤多个关键字
-r **在目录中查找含有关键字的文件
注意: ^关键字 **以关键字开头
关键字$ **以关键字结尾
grep -E "^root|root$" -v passwd -n | grep root **反向过滤passwd中以root开头和root结尾的行并显示含有root的行
[root@localhost mnt]# ifconfig eth0 | grep inet | grep inet6 -v |cut -d " " -f 10
172.25.254.17
[root@localhost mnt]# ifconfig eth0 | grep inet | grep inet6 -v | awk -F " " '{print $2}'
172.25.254.17
[3.cut]
cut **截取字符
cut -d 分隔符 **指定分隔符
cut -f 1,7 **显示指定的列(此处显示1和7列)
[root@localhost mnt]# cut -d ":" -f 1,7 passwd **显示passwd中以":"为分隔符第一列和第七列
root:/bin/bash
bin:/sbin/nologin
daemon:/sbin/nologin
adm:/sbin/nologin
lp:/sbin/nologin
sync:/bin/sync
shutdown:/sbin/shutdown
halt:/sbin/halt
mail:/sbin/nologin
operator:/sbin/nologin
games:/sbin/nologina
cut -c 1-4 **显示指定的字符(此处显示第二个字符到第四个字符)
[4.sort]
sort file **排序
-n **纯数字排序
-u **去冗余
-u|uniq -c **去冗余并统计冗余次数
-t **指定分隔符
-k **指定列
[5.uniq]
sort file |uniq -c **去除冗余并统计冗余次数
-d **显示冗余行
-u **显示唯一行
附加:tr 'a-z' 'A-Z' < file **将file中的小写换成大写
tr 'A-Z' 'a-z' < file **将file中的大写换成小写
[6.]
sed 's/原字符/替换字符/g' file
sed -e '策略1' -e '策略2' file
sed -i file **把转换后的内容输入到指定文件
sed '3,5s/原字符/替换字符/g' **3-5行替换
sed xd **屏蔽指定行
sed xp **复制指定行
sed -n xp **只显示指定行
[root@localhost mnt]# sed -ne 3p -ne 5p passwd **显示passwd的第三和第五行
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
for i in {1..5};do echo $i;done
将每个用户对应配密码,做法如下:
[root@localhost mnt]# ls
[root@localhost mnt]# vim userfile
[root@localhost mnt]# cat userfile
user1
user2
user3
[root@localhost mnt]# vim passwdfile
[root@localhost mnt]# cat passwdfile
westos1
westos2
westos3
[root@localhost mnt]# wc -l userfile
3 userfile
[root@localhost mnt]# wc -l userfile | cut -d " " -f 1
3
[root@localhost mnt]# ls
passwdfile userfile
[root@localhost mnt]# vim create_user.sh **编写脚本
[root@localhost mnt]# cat create_user.sh
#!/bin/bash
MAX=$( wc -l $1 | cut -d " " -f 1)
for NUM in $( seq $MAX)
do
USERNAME=$(sed -n ${NUM}p $1)
PASSWD=$( sed -n ${NUM}p $2)
useradd $USERNAME
echo $PASSWD |passwd --stdin $USERNAME
done
[root@localhost mnt]# chmod +x create_user.sh **给执行权限
[root@localhost mnt]# ./create_user.sh /mnt/userfile /mnt/passwdfile
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.
Command (m for help): m **帮助
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition **删除分区
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types **列出系统可用的分区类型
m print this menu
n add a new partition **新建分区
o create a new empty DOS partition table
p print the partition table **显示分区
q quit without saving changes **退出
s create a new empty Sun disklabel
t change a partition's system id **修改分区功能id
u change display/entry units
v verify the partition table
w write table to disk and exit **保存更改到分区表中
x extra functionality (experts only)
Command (m for help): n **新建分区
Partition type:
p primary (0 primary, 0 extended, 4 free) **分区类型为主分区
e extended **分区类型为扩展分区
Select (default p): **默认为主分区
Using default response p
Partition number (1-4, default 1): **主分区id
First sector (2048-20971519, default 2048): **此分区起始位置(一般默认)
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M **分区大小
Partition 1 of type Linux and of size 100 MiB is set
Command (m for help): p
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xdce4867b
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
Command (m for help): wq **保存退出,如果按q表示放弃更改退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe **同步分区表
[root@localhost ~]# cat /proc/partitions **查看已生成分区
major minor #blocks name
253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 102400 vdb1
mkfs.xfs /dev/vdb1 **格式化
mount /dev/vdb1 /mnt **挂载
df **查看是否挂载
blkid **查看系统可使用的分区
[2.swap]
mkswap /dev/vdb6 **格式化为swap分区
swapon -a /dev/vdb6 **激活
swapon -s **查看系统中的swap分区
swapoff /dev/vdb6 **关闭激活的swap分区
上述激活为临时激活,重启后消失,如需永久激活,操作如下:
vim /etc/fstab **修改配置文件
#
# /etc/fstab
# Created by anaconda on Wed May 7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
/dev/vdb6 swap swap defaults 0 0
设备 挂载点 文件系统类型
举例:
[root@localhost ~]# mkswap /dev/vdb6 **格式化为swap分区
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=ebbd4a4a-910b-4d2e-b5f8-87832a3fc63a
[root@localhost ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
/dev/vdb6: UUID="ebbd4a4a-910b-4d2e-b5f8-87832a3fc63a" TYPE="swap"
[root@localhost ~]# swapon -a /dev/vdb6 **激活
[root@localhost ~]# swapon -s **查看系统中的swap分区
FilenameTypeSizeUsedPriority
/dev/vdb6 partition10485720-1
[root@localhost ~]# swapoff /dev/vdb6 **关闭激活的swap分区
[root@localhost ~]# swapon -s
[root@localhost ~]# vim /etc/fstab
[root@localhost mnt]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May 7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
/dev/vdb6 swap swap defaults 0 0
/dev/vdb5 /mnt xfs defaults 0 0
[root@localhost ~]# mount -a
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3128524 7345376 30% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 80 942580 1% /dev/shm
tmpfs 942660 17028 925632 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
/dev/vdb5 98988 5280 93708 6% /mnt
添加临时swap分区
dd if=/dev/zero of=/mnt/swapfile bs=1M count=1000 **在/mnt下划分一块名为swapfile的分区,块大小为1M,数量为1000
mkswap /dev/vdb1 **格式化为swap分区
swapon -a /dev/vdb6 **激活
注意:主分区+逻辑分区总数不超过15
分区时,分三个主分区,第四个为扩展分区(剩下空间都给它)