LINUX基础(3)

1、复制/etc/profile至/tmp/目录,用查找替换命令删除/tmp/profile文件中的 行首的空白字符

2、在vim中设置tab缩进为4个字符

3、vim常用写法
4、编写脚本 createuser.sh,实现如下功能:使用一个用户名做为参数,如果 指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息

#!/bin/bash

case $# in
0)
    echo '未获取到用户参数'
    echo '示例: ./createuser.sh <username>'
    exit 1
    ;;
1)
    id $1 2>1 1>/dev/null
    if [[ $? -ne 0 ]]
    then
        useradd $1
        if [[ $? -eq 0 ]]
        then
            user_id=$(id $1|awk -F'[=(]' '{print $2}')
            echo "当前用户$1创建成功,用户id为$user_id"
        else
            echo "当前用户$1创建失败"
        fi
    else
        user_id=$(id $1|awk -F'[=(]' '{print $2}')
        echo "当前用户$1已经存在,用户id为$user_id"
    fi
    ;;
*)
    echo '用户参数过多'
    echo '示例: ./createuser.sh <username>'
    ;;
esac

5、编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小

#!/bin/bash

###################################
# name: systeminfo.sh
# date: 20210619
# author: aaa
# function: get system information
# usage: systeminfo.sh <hostname|kernel_version|cpu...>
###################################

case $1 in
hostname)
    hostname
        ;;
ipv4)
        ifconfig|grep inet|grep -v inet6|grep -v '127.0.0.1'|awk '{print $2}'
        ;;
os_version)
        cat /etc/redhat-release
        ;;
kernel_version)
        uname -r|cut -d "-" -f1
        ;;
cpu_type)
        cat /proc/cpuinfo |grep 'model name'|sed -n '1p'|cut -d ':' -f2
        ;;
mem_size)
        echo $(free -m|grep 'Mem'|awk '{print $2}')M
        ;;
disk_size)
        lsblk|egrep '^sd|^vd'|awk '{print $1,$4}'
        ;;
*)
        echo '输入参数无效,请检查命令使用方式。'
        echo 'Usage: systeminfo.sh <hostname|ipv4|os_version|kernel_version|cpu_type|mem_size|disk_size>'
        ;;
esac

6、编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值

 

 

 

 

posted @ 2021-06-19 18:07  lzzl  阅读(49)  评论(0)    收藏  举报