shell脚本
(课前了解)c脚本编写:
[root@localhost ~]# yum install gcc -y 安装gcc编译
[root@localhost ~]# vim hello.c 编辑c语言文
#include<stdio.h>
void main ()
{
printf("hello world\n");
}
[root@localhost ~]# gcc hello.c 默认编译生成a.out文件
[root@localhost ~]# ls
anaconda-ks.cfg Desktop Downloads ip_show.sh Pictures Templates
a.out Documents hello.c Music Public Videos
[root@localhost ~]# ./a.out 执行编译之后的a.out文件
hello world
[root@localhost ~]# gcc hello.c -o hello -o表示编译成为自己想要的文件名字
[root@localhost ~]# ls
anaconda-ks.cfg Desktop Downloads hello.c Music Public Videos
a.out Documents hello ip_show.sh Pictures Templates
[root@localhost ~]# ./hello 运行c脚本
hello world
shell 脚本编写
一、基本写法
#!/bin/bash
执行:(1) sh XXXX.sh
(2) chmod +x /mnt/XXXX.sh
/mnt/XXXXX.sh
二、脚本前面内容的自动添加
[root@localhost mnt]# vim /etc/vimrc
autocmd BufNewFile *.sh exec ":call WESTOS()" (末尾添加)
func WESTOS ()
call append(0,"##############################")
call append(1,"#Author: xd".(" #"))
call append(2,"#Version: ".(" #"))
call append(3,"#Mail: ".(" #"))
call append(4,"#Date: ".strftime("%Y-%m-%d").(" #"))
call append(5,"#Description: ".(" #"))
call append(6,"# ".(" #"))
call append(7,"##############################")
call append(8," ")
call append(9,"#!/bin/bash")
endfunc
[root@localhost mnt]# vim westos.sh
##############################
#Author: xd #
#Version: #
#Mail: #
#Date: 2018-06-09 #
#Description: #
# #
##############################
#!/bin/bash
三 、shell脚本中常用的命令
1、 diff
(1)比较不同,生成补丁不保存原文件
148 vim westos
westos149 vim westos1
westos
linux
150 diff westos westos1 比较文件westos和westos1文件的不同(以后面的文件为标准)
151 diff -u westos westos1 生成文件补丁
152 diff -u westos westos1 > westos.path 把生成的补丁导入westos.path文件中
153 yum install patch -y
154 patch westos westos.path 给文件westos添加补丁 (不保存westos原文件)
155 ls
156 cat westos
westos
linux
157 cat westos1
westos
linux
168 vim westos
westos
169 vim westos1
westos
linux
170 diff -u westos westos1 > westos.path 比较文件westos和westos1文件的不同(以后面的文件为标准)并生成补丁导入westos.path
171 patch -b westos westos.path 给文件westos添加补丁(保存原文件)
172 ls
173 cat westos.orig
westos
174 cat westos
westos
linux
[root@localhost mnt]# mkdir linux
[root@localhost mnt]# mkdir unix
[root@localhost mnt]# ls
linux unix westos westos1 westos.orig westos.path
[root@localhost mnt]# touch linux/file
[root@localhost mnt]# diff -r linux unix
Only in linux: file
2、cut
cut 命令多用与字符截取
cut -d ##指定分隔符
cut -f 1,7(第1和第7列)|1-7 (第一到第七列) ##指定截取的列
cut -c 1,4(第一个分隔符前和第四个分隔符前内容)|1-4(第一个分隔符到第四个分隔符) ##指定截取的字符位置
脚本练习:显示本机ip
[root@localhost mnt]# cat ip_show.sh
##############################
#Author: xd #
#Version: #
#Mail: #
#Date: 2018-06-09 #
#Description: #
# #
##############################
#!/bin/bash
ifconfig eth0 |grep "inet "|cut -d " " -f 10
或者:ifconfig eth0 | awk -F " " '/inet\>/{print $2}'
测试: sh ip_show.sh
3、sort(字符排序)
sort -n 纯属字排序
sort -r 倒序
sort -u 去掉重复数字
sort -o 输出到指定文件中
sort -t 指定分隔符
sort -k 指定要排序的列
uniq 对重复字符做相应的处理
uniq -u 显示唯一的行
uniq -d 显示重复的行
uniq -c 每行显示一次并统计重复次数
223 cat westos224 sort westos
225 sort -n westos 纯数字排序
226 sort -rn westos 纯数字倒序排列
228 sort -nu westos 去掉重复的数字进行排序
229 sort -nu westos -o /mnt/file 去掉重复的数字进行排序输出到指定文件/mnt/file
230 ls
231 cat file
233 sort -t : -k 2 westos
234 sort -t : -k 2 -n westos :后的第二列纯数字排序
235 uniq -u westos 显示唯一的行
236 uniq -d westos 显示重复的行
uniq -c westos 每行显示一次并统计重复次数
237 vim westos
238 cat westos
239 sort -n westos | uniq -c
240 sort -n westos | uniq -d
259 sort -n westos | uniq -u
4、&&和||
&&用来执行条件成立后执行的命令
||用来执行条件不成立后执行的命令
[root@localhost mnt]# cat check_ip.sh
##############################
#Author: xd #
#Version: #
#Mail: #
#Date: 2018-06-09 #
#Description: #
# #
##############################
#!/bin/bash
ping -c1 -w1 $1 &> /dev/null && echo $1 is up || echo $1 is down
测试 sh check_ip.sh 172.25.254.55
5、对比检测
test命令和[ ]等同test"$A" ="$B" 等同 [ "$A" = "$B" ]
ge 大于等于[ "$A" -ge"$B" ]
gt 大于[ "$A" -gt "$B" ]
= [ "$A" = "$B" ] 对比A和B是否相等
!= 不等于 [ "$A" != "$B" ]
-eq 等于[ "$A" -eq "$B" ]
-ne 不等于[ "$A" -ne "$B" ]
-le 小于等于[ "$A" -le "$B" ]
-lt 小于[ "$A" -lt "$B" ]
-a 两者都满足[ "$A" -ne "$B" ] -a [ "$A" -ne "$B" ]
-o 满足一个[ "$A" -ne "$B" ] -o[ "$A" -lt "$B" ]
脚本练习:检测数字是否在10以内
#!/bin/bash
[ -z "$1" ] && {
echo please give me number!!
exit 1
}
[ "$1" -lt "10" -a "$1" -gt "0" ] && {
echo "yes, $1 is between 1~10 "
}||{
echo "no,$1 is not between 1~10"
}
测试:sh check_num.sh 7
sh check_num.sh 20
文件1 -ef 文件2 文件1和文件2是否互为硬连接(一个节点)
cd /mnt
touch file
ln /mnt/file /mnt/file1
ls -l
[ "/mnt/file" -ef "/mnt/file1" ] && echo yes ||echo no
文件1 -nt 文件2 文件1生成时间是否比文件二新
rm -fr *
touch file
touch file1
[ "file" -nt "file1" ] && echo yes || echo no
文件1 -ot 文件2 文件1生成时间是否比文件二老
[ "file" -nt "file1" ] && echo yes || echo no
-e "file" 文件是否存在-f "file" 文件是否为普通文件
-L "file" 是否为软连接
-S "file" 是否为套节字
-b "file" 是否为块设备
-d "file" 是否为目录
-c "file" 是否为字符设备
脚本练习:检测某个文件是否存在,若存在指出什么类型文件
vim file_check.sh
#!/bin/bash
[ -z "$1" ] && {
echo "please input a file name after script!!"
exit 1
}
[ -e "$1" ] || {
echo "$1 is not exist"
exit 0
}
[ -L "$1" ] |&& {
echo "$1 is a link "
exit 0
}
[ -f "$1" ] &&{
echo "$1 is a common file"
exit 0
}
[ -b "$1" ] && {
echo "$1 is a block file"
exit 1
}
测试:
sh file_check.sh
sh file_check.sh file
sh file_check.sh /dev/vdb
sh file_check.sh /etc/system-release
sh file_check.sh /etc/passwd
6、tr 字符转换
脚本练习:
#!/bin/bash
WORD=$(echo $1 | tr 'A-Z' 'a-z') 将字符A-Z转换为a-z
[ "$WORD" = "hello" ] &&{
echo yes
} || {
echo no
}
测试:
sh test.sh hello
sh test.sh HELLO
脚本练习:检测系统中是否存在此用户,若不存在则建立此用户并赋予其密码
#!/bin/bash
[ "$#" -eq "2" ] || {
echo "please input username and password after script!!"
exit 1
}
Check_User=` getent passwd $1`
[ -n "$Check User" ]&&{
echo $1 is exist!!
exit 1
}
useradd $1
echo $2 | passwd --stdin $1
sh user.sh student
sh user.sh student student
sh user.sh tom tom