脚本练习

本文介绍了如何使用Linux shell脚本进行系统信息查询和用户管理。通过disk.sh脚本展示了获取硬盘最大空间利用率的方法;systeminfo.sh脚本则用于显示主机名、IPv4地址、操作系统版本等关键信息;createuser.sh脚本则实现了用户创建或检查用户存在的功能。这些脚本对于日常系统维护和自动化操作具有实用价值。
摘要由CSDN通过智能技术生成

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

it@ubuntusvr:~$ vim disk.sh
it@ubuntusvr:~$ cat disk.sh 
#!/bin/bash
df | grep ^/dev/sd | grep -o '[0-9]\+%' | sort -nr | head -1
it@ubuntusvr:~$ chmod a+x disk.sh 
it@ubuntusvr:~$ ./disk.sh 
6%
it@ubuntusvr:~$ 

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

it@testsvr:~$ vim systeminfo.sh
it@testsvr:~$ cat systeminfo.sh 
#!/bin/bash

echo Hotstname: `hostname`;
echo IPv4 Address: `ip addr | grep -A 1 ens | xargs -l6 | cut -d" " -f20`;
echo OS Version: `cat /etc/os-release | grep ^PRETTY_NAME | cut -d= -f2`;
echo Kernel Version: `uname -r`;
echo CPU:`lscpu | grep "Model name" | tr -s " " | cut -d: -f2`;
echo Memory: `free -h | grep Mem | tr -s " " : | cut -d: -f2`;
echo Disk: `lsblk | grep disk | tr -s " "| cut -d" " -f4`;
it@testsvr:~$ chmod a+x systeminfo.sh  
it@testsvr:~$ ./systeminfo.sh  
Hotstname: testsvr
IPv4 Address: 10.10.10.105/24 10.10.11.104/24
OS Version: "Ubuntu 20.04.1 LTS"
Kernel Version: 5.8.0-41-generic
CPU: Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
Memory: 1.9Gi
Disk: 160G

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

root@testsvr:~# vim createuser.sh 
root@testsvr:~# cat createuser.sh
#!/bin/bash

USERS=$*

for i in $USERS; do
    getent passwd $i >& /dev/null
    if [ $? -ne 0 ]; then
        useradd $i
	echo The $i UID is `id $i`
    else
	echo The $i already exists
    fi
done
root@testsvr:~# chmod a+x createuser.sh 
root@testsvr:~# 
root@testsvr:~# ./createuser.sh it
The it already exists
root@testsvr:~# ./createuser.sh tom
The tom UID is uid=1001(tom) gid=1001(tom) groups=1001(tom)

编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等

root@testsvr:~# vim .vimrc 
root@testsvr:~# cat .vimrc
autocmd BufNewFile *.sh exec ":call SetTitle()"
function  SetTitle()
   call append(0,"#!/bin/bash")
   call append(1,"# **********************************************************")
   call append(2,"# Auther:       Aaron")
   call append(3,"# Call Me:      110")
   call append(4,"# File Nmae:    ".expand("%"))
   call append(5,"# Version:      V1.0")
   call append(6,"# Date:         ".strftime("%Y-%m-%d"))
   call append(7,"# Description: ")
   call append(8,"# **********************************************************")
endfunction
root@testsvr:~# 
root@testsvr:~# 
root@testsvr:~# vim test.sh
root@testsvr:~# cat test.sh
#!/bin/bash
# **********************************************************
# Auther:       Aaron
# Call Me:      110
# File Nmae:    test.sh
# Version:      V1.0
# Date:         2021-03-12
# Description: 
# **********************************************************

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值