第一个Shell脚本

发布时间 2023-11-20 16:43:47作者: 湖中客

1.先创建.vimrc文件,自动生成shell脚本注释

set ignorecase         #设置忽略大小写查找 
set cursorline         #设置高亮当前行
set autoindent         #设置在插入模式下,对每行按与上行同样的标准进行缩进
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
    if expand("%:e") == 'sh'
    call setline(1,"#!/bin/bash")
    call setline(2,"#")
    call setline(3,"#**************************************************************")
    call setline(4,"# @Author:                          hzk")                              #编写作者                    
    call setline(5,"# @FileName:                        ".expand("%"))                     #显示文件名字
    call setline(6,"# @Version:                         V1.0")                             #文件版本
    call setline(7,"# @CreationDate:                    ".strftime("%Y-%m-%d %H:%M:%S"))   #编写时间   
    call setline(8,"# @Description:                     The test script ")                 #脚本描述                                    
    call setline(9,"# @Copyright(C)".strftime("%Y")."xxx公司All Rights Reserved.")         #公司声明 
    call setline(10,"#*************************************************************")
    call setline(11,"")
    endif
endfunc
autocmd BufNewFile * normal G

2.Shell脚本格式

#!/bin/bash                 #指定解释程序
CONFIGURATION_VARIABLES     #配置变量
FUNCTION_DEFINITIONS        #定义函数
MAIN_CODE                   #主代码

3.shell 脚本执行方式

#!/bin/bash
#
# **************************************************************
# @Author:                          hzk
# @FileName:                        test.sh
# @Version:                         V1.0
# @CreationDate:                    2023-11-15 16:48:42
# @Description:                     The test script 
# @Copyright(C)2023 xxx公司 All Rights Reserved.
#*************************************************************
#经典写法
echo "hello,world"
#流行写法
echo 'Hello,world!'

#执行方法1
[root@localhost ~]# bash  /data/hello.sh

#执行方法2
[root@localhost ~]# cat  /data/hello.sh | bash

#执行方法3
[root@localhost ~]# chmod +x /data/hello.sh

#相对路径执行
[root@localhost data]#./test.sh

#绝对路径执行
[root@localhost data]#/data/test.sh 

4.第一个Shell脚本,打印系统信息

#!/bin/bash
#
#**************************************************************
# @Author:                          hzk
# @FileName:                        test.sh
# @Version:                         V1.0
# @CreationDate:                    2023-11-20 11:43:25
# @Description:                     The test script 
# @Copyright(C)2023 xxx公司All Rights Reserved.
#*************************************************************
echo -e "------------------Host systeminfo----------------------"
echo -e "HOSTNAME:        `hostname`"                                                #主机名
echo -e "IPADDR:          `ifconfig ens33|grep netmask|tr -s " "|cut -d" " -f3`"     #IP地址
echo -e "OSVERSION:       `cat /etc/redhat-release`"                                 #系统版本
echo -e "KERNEL:          `uname -r`"                                                #内核版本
echo -e "CPU:             `lscpu|grep '型号名称'|tr -s ' '|cut -d: -f2`"             #CPU型号
echo -e "MEMORY:          `free -h|grep Mem|tr -s ' ' : |cut -d : -f2`"              #系统内存
echo -e "DISK:            `lsblk|grep '^sd'|tr -s ' '|cut -d" " -f4`"                #系统磁盘
echo -e "-------------------------------------------------------"