shell编程入门1

1.什么是shell
shell处于kernel的外层用来负责接收使用者输入的命令然后将命令解释成kernel能了解的方式,然后由kernel去执行,再将结果传回默认的输出周边,它是一个命令解释器也是一个语言程序。
2.shell的第一次编写
 

vim hello.sh
 #!/bin/bash             ##解释器/bin/bash
 echo "hello world!!!"   
chmod +x hello.sh        ##添加执行权限
sh hello.sh              ##执行shell

vim westos.sh
  1 #!/bin/bash 
  2 gedit
执行之前,给予执行权限:
chmod +x hello.sh
执行:
westos.sh
ctrl z                  ##打入后台运行
ps f
可以看到解释器是/bin/bash
vim westos.sh
  1 #!/bin/sh 
  2 gedit
执行:
./westos.sh
ps f
可以看到解释器是/bin/sh

脚本的检测
当脚本编写好之后都要先检测脚本能否正常运行

 1)执行后加 -x 例:sh -x westos.sh
 2)在脚本中加 -x  例:#!/bin/bash -x

脚本注释的自动写入
通常我们需要对写好的脚本声明方便查看和使用,但每次脚本在开头手动添加太麻烦我们可以实现自动写入

vim /etc/vimrc
 67 #map <F6> ms:call han()<cr>'s      ##设置一个快捷键
 68 #auto BufNewFile *.sh exec ":call han()"    ##或者当格式为.sh时自动写入
 69 function han()
 70         call append(0,"###################################")
 71         call append(1,"#SHELL NAME:                      #")
 72         call append(2,"#CREATETIME:".strftime("%Y-%m-%d %H:%M:%S").("#"))
 73         call append(3,"###################################")
 74 
 75 endfunction

shell编程中常用的命令
1.diff命令

diff 中常用的参数

-b 或 --ignore-space-change  不检查空格字符的不同
-B 或 --ignore-blank-lines  不检查空白行
-c  显示全部内文,并标出不同之处
-i 或 --ignore-case  不检查大小写的不同
-p :若比较的文件为 C 语言的程序码文件时,显示差异所在的
函数名称;
-q 或 --brief :仅显示有无差异,不显示详细的信息
-r 或 --recursive :比较子目录中的文件
-u 以合并的方式来显示文件内容的不同

2.patch命令

patch数据导入:
yum install patch -y
diff -u westos westos.new > westos.path ##将文件不同的地方导出到.path文件中
patch -b westos westos.path       ##-b创建还原备份
cat westos


3.cut 命令

cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段写至标准输出。
如果不指定 File 参数,cut 命令将读取标准输入。必须指定 -b、-c 或 -f 标志之一。

主要参数
-b :以字节为单位进行分割。这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志。
-c :以字符为单位进行分割。
-d :自定义分隔符,默认为制表符。
-f :与-d一起使用,指定显示哪个区域。
-n :取消分割多字节字符。仅和 -b 标志一起使用。如果字符的最后一个字节落在由 -b 标志的 List 参数指示的
范围之内,该字符将被写出;否则,该字符将被排除。

例:找出IP地址

找出ip地址:
ifconfig eth0|head -2|tail -1|cut -d " " -f 10        ###-d:指定分隔符(单个字符),-f:指定列
ifconfig eth0|head -2|tail -1|awk -F " " '{print $2}'
找出系统可以登陆的用户名
#!/bin/bash
cat /etc/passwd | grep "/bin/bash"|cut -d : -f 1
或者:
grep bash$ /etc/passwd |cut -d : -f 1

4.sort命令

sort -n                      ###纯数字排序
sort -r              ###倒序
sort -u              ###去掉重复的数字
sort -o                  ###输出到指定的文件中,也可以直接用>
sort -t                      ###指定分隔符
sort -k              ###指定要排序的列

实验:
排序/bin/下的文件,只显示大小和文件名
ls -l /bin/ | awk -F " " '{print $5,$9}' | sort -nr
等价于ls -S /bin/ 

test命令

l:less  g:great t:then e:equid z:zero n:nozero 
test "$A" == "$B" 等同 [ "$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不等于$B且(and)$A大于$B
[ "$A" -ne "$B" -o "$A" -gt "$B" ]===>>$A不等于$B或者(or)$A大于$B
[ -z "$A" ]===>>为空为真
[-n "$A" ]===>>不为空为真
[ "file1" -ef "file2" ]===>>2个文件的索引节点(inode)是否相等,ln硬链接
[ "file1" -nt "file2" ]===>>newer then:file1比file2新为真
[ "file1" -ot "file2" ]===>>older then:file1比file2旧为真

[ -e "file" ]===>>文件存在
[ -f "file" ]===>>文件存在且是常规文件
[ -L "file" ]===>>link:链接文件
[ -S "file" ]===>>Socekt:套节字(下载mariadb:/var/lib/mysql下)
[ -b "file" ]===>>block:块设备
[ -d "file" ]===>>directory:目录
[ -c "file" ]===>>char:字符设备
-e file ##是否存在
-f file ##文件
-L file ##链接
-S file ##套接字
-b file ##块设备
-d file ##目录
-c file ##字符设备

编写一个脚本
1.网络测试 能否ping通对方

 #!/bin/bash
 ping  -c1 -w1 $1 &>/dev/null &&echo up || echo down ##这里是数字“1“

##测试  
[root@desktop8 mnt]# sh westos.sh 172.25.8.250
up

2.根分区占用超过80% 报警

[root@desktop8 mnt]# vim use.sh
#!/bin/bash
NUM=`df -h / | awk 'NR==2{print $5}'| awk -F "%" '{print $1}'`
[ "$NUM" -ge "80" ] && (echo "/ is full" | mail -s warning root)

3.网络检测 不输入IP时报错

#!/bin/bash
[ -z "$1" ] &&echo ERROR || ( ping  -c1 -w1 $1 &>/dev/null &&echo "$1" up || echo "$1" down )
或者用-n
-z 当输入为空时  -n 当输入存在时

判断用户是否为root

vim use.sh 
#!/bin/bash
[ "$USER" = "root" ] && (
        > /var/log/message
        echo -e "\033[31;1mlog cleared!!\033[0m"
        )||(
        echo -e "\033[31;1myou are not root!!!\033[0m"
)

参数说明
echo -e “\033[背景;字体;属性 \033[0m”
 echo -e "\033[32;1mlog cleared!!!!\033[0m"                                 ###绿色
 echo -e "\033[31;1myou are not root!!!\033[0m"                             ###红色



test检测文件类型

#!/bin/bash
check()
{
        if [ $1 "$2" ]
        then
        echo "$2 is $3"
        exit 0
        fi
}
 [ -z "$1" ] && (
        echo "input a file after command"; exit 1
        )
 [ -e "$1" ] && (
        check -L $1 "links file"
        check -s $1 "socket file"
        check -c $1 "char file"
        check -b $1 "block file"
        check -d $1 "directory"
        check -f $1 "common file"
        )||(
        echo "$1 is not exist!"
)


 

猜你喜欢

转载自blog.csdn.net/qq_41636653/article/details/82313779