shell--1--shell入门 shell中添加信息 类型的判断 清空日志 grep

什么是shell

Shell是操作器系统中的一个软件包,包含在linux内核的外面,为用户和内核之间的交互提供了一个接口,系统中的命令用shell去解释shell接收系统回应的输出并显示到屏幕中。Bash=Gnu Bourne-Again Shell

什么是shell脚本

脚本是一种解释型语言,用shell脚本保存执行的动作,用shell脚本判定命令的执行条件,用脚本来实现动作的批量执行

shell解释器

#!/bin/bash  ##幻数  这个位置可以添加以下几种不过执行的是不同的命令

watch -n 1 date

#!/bin/sh

#!/bin/cat

#!/bin/rm -rf

执行命令的方式

1.chmod + xx x.sh

   xxx.sh

2.sh xxx.sh

shell基本信息的添加

@1每次写脚本之前,都重新写一次信息说明

@2快捷键就可以添加一些固定的信息;直接vim xx.sh 打开一个新的shell自动添加

实验:

实现每次写shell脚本时自动添加个人信息,编辑/etc/vimrc  在最后加入以下文字,之后vim xx.sh  新的vim都会自动添加效果如下:

vim /etc/vimrc
"map <F4> ms:call Westos()<cr>'s              按F4添加会给一个文件添加多个所以引入自动添加
autocmd BufNewFile *.sh,*.script  exec ":call Westos()"
func Westos()
        call append(0,"#############################")
        call append(1,"#Author          :          #")
        call append(2,"#Date            :".strftime("%Y-%m-%d").("#"))
        call append(3,"#Mail            :          #")
        call append(4,"#Version         :          #")
        call append(5,"#Description:               #")
        call append(6,"#                           #")
        call append(7,"#############################")
        call append(8,"")
        call append(9,"#!/bin/bash")
        call append(10,"")
Endfunc
测试:
vim xx.sh 可以看到新建文件达到预期效果

判断类型

#!/bin/bash
[ ! -e "$1" ]&&{
        echo $1 is not exist!
        exit
}
[ -b "$1" ]&&{
        echo $1 is a block device
        exit
}
[ -f  "$1" ]&&{
        echo $1 is a common file
        exit
}
[ -L  "$1" ]&&{
        echo $1 is a Link
        exit
}

[ -s  "$1" ]&&{
        echo $1 is a Socket
        exit
}
[ -b  "$1" ]&&{
       echo $1 is a block
        exit
}
[ -c  "$1" ]&&{
        echo $1 is a c
        exit
}

学习了shell的函数之后补充:可以用函数的方式简化

显示ip,可登录用户名,主机名

#!/bin/bash
echo ip:
ifconfig eth0 | grep inet | head -n 1| cut -d ' ' -f 10
echo user:  
grep /bin/bash /etc/passwd | cut -d : -f 1
grep /bin/sh  /etc/passwd | cut -d : -f 1
grep /bin/tcsh  /etc/passwd | cut -d : -f 1
grep /bin/csh  /etc/passwd | cut -d : -f 1
echo hostname:
hostname | cut -d . -f 1

清空日志

grep

Global search regular expression and print  out the line

全局搜索研究正则表达式并显示出来

Grep 命令是一种强大的文本搜索工具,根据用户指定的模式,对目标文本进行匹配检查,打印匹配到的行

由正则表达式或者字符及基本文字字符所编写的过滤条件

grep -i root passwd 忽略大小写
grep -i ^root passwd root之前为空显示
grep -i root$ passwd root之后为空显示
grep root passwd | grep -i -E "^root|root$" -v  root不在前不在后的显示 -v反向过滤 -E 扩展的本不认识| 用egrep达到一样的效果
 vim westos
xy
xyxy
txyxy
xyxyt
xxyxyxy
xyxyxyxyy
txyxyxyxyt

       

 

猜你喜欢

转载自blog.csdn.net/weixin_40460156/article/details/81879301