编写脚本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:
# **********************************************************