第一个Shell脚本
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:
# @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 "-------------------------------------------------------"
本文作者:湖中客
本文链接:https://www.cnblogs.com/hzke/p/17844409.html
转载声明:转载请注明出处,如有侵权,请联系作者修改或删除。
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步