Linux下对shell脚本的初步了解

  一、简述

shell 也是操作系统中的一个软件,它包在 linux 内核的外面,为用户和内核之间的交互提供了一个接口系统中的命令

用shell 去解释,shell 接收系统回应的输出并显示其到屏幕中

1、编写简单程序

[root@desktop mnt]# vim hello.sh
[root@desktop mnt]# cat hello.sh 
#!/bin/bash
echo hello!!!!!
[root@desktop mnt]# sh hello.sh 
hello!!!!!

给脚本一个可执行权限 用绝对路径也可以运行

[root@desktop mnt]# chmod +x hello.sh 
[root@desktop mnt]# ./hello.sh
hello!!!!!

查看脚本的运行过程

[root@desktop mnt]# sh -x hello.sh 
+ echo 'hello!!!!!'
hello!!!!!

二、编写脚本的注释

[root@desktop mnt]# vim /etc/vimrc 
[root@desktop mnt]# cat /etc/vimrc 
在文本的最后写入以下
map <F4>  ms:call WESTOS()<cr>'s   按下f4,调用WESTOS函数
 func WESTOS ()
        call append(0,"#################################")  调用append函数 用来显示出括号中内容 0表示第一行        
        call append(1,"# Author:                       #")    作者信息
        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        

测试

测试注释是否保存 ,进入vim界面 按F4就可显示


如果想要自动产生注释需要将 map那行注释掉

shell脚本下的常用基本命令

1、diff命令

diff命令常用于比较两个文件的不同

a 表示添加
c 表示改变

d 表示删除

[root@desktop mnt]# touch file1
[root@desktop mnt]# vim file1
[root@desktop mnt]# vim file2
[root@desktop mnt]# cat file1
123
hello
linux

[root@desktop mnt]# cat file2
123
hello

[root@desktop mnt]# diff file2 file1
2a3   ##翻译为:2文件添加 第一个文件的第三行
< linux
[root@desktop mnt]# diff -r /etc/ /mnt/    ## -r比较目录
Only in /etc/: abrt
Only in /etc/: adjtime
Only in /etc/: aliases
Only in /etc/: aliases.db
Only in /etc/: alsa
Only in /etc/: alternatives


2、patch

用于解决两文件的差异性
[root@desktop mnt]# diff -u file1 file2      ##显示打补丁的过程
--- file1	2018-06-29 07:34:30.517145515 -0400
+++ file2	2018-06-29 07:34:48.877145515 -0400
@@ -1,4 +1,3 @@
 123
 hello
-linux
 
[root@desktop mnt]# diff file2 file1 > file.path      ##生成补丁文件
[root@desktop mnt]# ls
file1  file2  file.path  hello.sh
[root@desktop mnt]# patch file2 file.path      打入补丁
patching file file2
[root@desktop mnt]# cat file2       跟file1相同
123
hello
linux

[root@desktop mnt]# diff file1 file2   没有差异性
[root@desktop mnt]# 

3、cut

用来截取
[root@desktop etc]# cut -d : -f 1 passwd   -d指定分隔符为:   -f表示分要截取的列
 root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
dbus
polkitd
avahi
avahi-autoipd
rpc
rpcuser
nfsnobody
ovirtagent
postfix
sshd
chrony
student
usbmuxd
colord
abrt
libstoragemgmt
unbound
qemu
saslauth
ntp
rtkit
radvd
pulse
gdm
gnome-initial-setup
tcpdump
westos
test

截取字符

[root@desktop etc]# cut -c 1-3 passwd
roo
bin
dae
adm
lp:
syn
shu
hal
mai
ope
gam
ftp
nob
dbu
pol
ava
ava
rpc
rpc
nfs
ovi
pos
ssh
chr
stu
usb
col
abr
lib
unb
qem
sas
ntp
rtk
rad
pul
gdm
gno
tcp
wes
tes

脚本实验:执行脚本文件只显示ip

[root@desktop mnt]# vim ip_show.sh
[root@desktop mnt]# sh ip_show.sh 
172.25.254.166
[root@desktop mnt]# cat ip_show.sh 
ifconfig eth0 | grep "inet\>" | cut -d " " -f 10

4、&&和| |


&& 表示true,在条件成立时使用

| | 表示false,在条件不成立时使用


判断ip是否可以连通

[root@desktop mnt]# vim check_ip.sh
[root@desktop mnt]# sh check_ip.sh 
is down
[root@desktop mnt]# sh check_ip.sh 172.25.254.167
172.25.254.167 is up
[root@desktop mnt]# cat check_ip.sh 
ping -c1 -w1 $1 &> /dev/null && echo $1 is up || echo $1 is down

5、sort

多用于字符排序
sort -n 纯数字排序
sort -r 倒序
sort -u 去掉重复的数字
sort -o 输出到指定文件
sort -t 指定分隔符

sort -k 指定要排序的列

[root@desktop mnt]# vim num
[root@desktop mnt]# cat num 
1
3
13
2
4
44
24
45
56
32
12
[root@desktop mnt]# sort -n num 
1
2
3
4
12
13
24
32
44
45
56

6、uniq

对重复字符作相应的处理
uniq -u 显示唯一的行
uniq -d 显示重复的行

uniq -c 每行显示一次并统计重复次数

7、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” ] ne表示不等于,a表示and都满足
[ “$A” -ne “$B” -o “$A” -gt “$B” ] o表示or至少满足一个
[ -z “$A” ] 表示值为空
[ -n “$A” ] 表示值不为空
[ “file1” -ef “file2” ] ef 表示文件是否为硬连接
[ “file1” -nt “file2” ] 表示new,file1是否比file2文件新
[ “file1” -ot “file2” ] 表示old,file1是否比file2文件老

test测试文件类型

-e 表示存在
-f 普通文件
-L 软连接,在脚本文件中写判断文件类型时,将软连接判断写在普通文件判断前面,因为软连接也是一种文本文件
-S 套接字
-b 块设备
-d 目录
-c 字符设备

9、tr

tr 转换字符大小
格式:tr 参数1 参数2 标准输入


[root@desktop mnt]# vim trfile
[root@desktop mnt]# tr 'a-z' 'A-Z' < trfile 
ABC 
DEF
[root@desktop mnt]# tr 'A-Z' 'a-z' < trfile 
abc 
def
[root@desktop mnt]# 















猜你喜欢

转载自blog.csdn.net/a313434458/article/details/80860900
今日推荐