shell 案例

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

cp /etc/profile  /tmp/

:%s/^ \+//g

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

进入扩展模式 : :set tabstop=4

3、20分钟内通关vimtutor(可参考https://yyqing.me/post/2017/2017-02-22-vimtutor-chinese-summary)
4、编写脚本 createuser.sh,实现如下功能:使用一个用户名做为参数,如果 指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息

#!/bin/bash
cat /etc/passwd|cut -d: -f1|grep $1 &> /dev/null
if [ $? -eq 0 ];then
echo "$1 exist"
else
useradd $1
fi


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

#!/bin/bash
echo '主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小'
host_name=`hostname`
IPV4=`ifconfig eth0|grep -o '\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}'|head -n1`
OS=`cat /etc/redhat-release`
neihe=`uname -r`
CPU=`cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c`
neicun=`free -g|grep 'Mem'|tr -s ' ' ':'|cut -d: -f2`
disk=`df -h|tr -s ' ' ':'|cut -d ':' -f 1,2`
echo "####################################"
echo "主机名:$host_name"
echo "IPV4地址:$IPV4"
echo "操作系统版本:$OS"
echo "内核版本:$neihe"
echo "CPU型号:$CPU"
echo "内存大小:$neicun"
echo "硬盘大小:$disk"
echo "####################################"


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

#!/bin/bash
disk=`df -h|grep "^/dev"|tr -s " " ':'|sort -nr -t: -k5|tail -1|cut -d: -f5`
echo "$disk"

posted @ 2021-06-27 22:09  从零到1  阅读(33)  评论(0编辑  收藏  举报