Kevin_306

06-SHELL脚本编程基础-系统基础信息20210222 (一)

1.编程语言 

高级编程语言:

编译:高级语言-->编译器-->机器代码文件-->执行,如:C,C++(运行性能高)
解释:高级语言-->执行-->解释器-->机器代码,如:shell,python,php,JavaScript,perl(开发效率高)

2.Shell 脚本语言的基本用法

  2.1第一个脚本hello.sh

[root@centos8 ~]# cat hello.sh 
#!/bin/bash
#
#********************************************************************
#Author:            kevin ma
#QQ:                 10650@@@@@
#Date:                 2021-03-16
#FileName:            hello.sh
#URL:                 https://home.cnblogs.com/u/kevin306
#Description:        The test script
#Copyright (C):     2021 All rights reserved
#********************************************************************

#第一个shell脚本,打印hello,world
echo "hello,world!"
[root@centos8 ~]# /root/hello.sh
hello,world!

 

  范例:获取电脑的系统信息 

 高亮版本代码:

 1 [root@centos8 ~]# cat systeminfo.sh
 2 #!bin/bash
 3 #***********************************************************
 4 #Author:                  kevin ma
 5 #QQ:                      10650*****
 6 #Date:                    2021-02-22
 7 #FileName:                System Infomation
 8 #URL:                     https://home.cnblogs.com/u/kevin306
 9 #Description:             The test script
10 #Copyright(C):            2021 All rights reserved
11 #************************************************************
12 COLOR="\033[1;31m"
13 END="\033[0m"
14 echo -e "--------------$COLOR HostSystem Infomation $END--------------"
15 echo -e "HOSTNAME:$COLOR `hostname`$END"
16 echo -e "IPADDR:$COLOR `ifconfig ens33|grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' |head -n1`$END"
17 echo -e "KERNEL:$COLOR `uname -r`$END"
18 echo -e "OSVERSION:$COLOR `cat /etc/redhat-release`$END"
19 echo -e "CPU:$COLOR `lscpu |grep "Model name" |tr -s " "|cut -d : -f2`$END"
20 echo -e "MEM:$COLOR `free -h|grep Mem |tr -s " " :|cut -d : -f2` $END"
21 echo -e "DISK:$COLOR `lsblk |grep '^sd' |tr -s ' ' |cut -d ' ' -f4`$END"

非高亮版本代码: 

 1 root@centos8 ~]# cat systeminfo1.sh
 2 #!bin/bash
 3 #
 4 #***********************************************************
 5 #Author:                  kevin ma
 6 #QQ:                      10650*****
 7 #Date:                    2021-02-22
 8 #FileName:                System Infomation
 9 #URL:                     https://home.cnblogs.com/u/kevin306
10 #Description:             The test script
11 #Copyright(C):            2021 All rights reserved
12 #************************************************************
13 echo "-------------- HostSystem Infomation --------------"
14 echo "HOSTNAME:" `hostname`
15 echo "KERNEL:" `uname -r`
16 echo "OSVERSION:" `cat /etc/redhat-release`
17 echo "IPADDR:" `ifconfig ens33|grep -Eo "([0-9]{1,3}\.){3}[0-9]{1,3}" |head -n1`
18 echo "CPU:" `lscpu |grep "Model name" |tr -s " "|cut -d : -f2`
19 echo "MEM:" `free -h|grep Mem |tr -s " " :|cut -d : -f2` 
20 echo "DISK:" `lsblk |grep "^sd" |tr -s " " |cut -d " " -f4`

 

 

 

posted on 2021-02-06 11:50  熊猫小虾  阅读(79)  评论(0编辑  收藏  举报

导航