目录
1、复制/etc/profile至/tmp/目录,用查找替换命令删除/tmp/profile文件中的 行首的空白字符
3、20分钟内通关vimtutor(可参考https://yyqing.me/post/2017/2017-02-22-vimtutor-chinese-summary)
4、编写脚本 createuser.sh,实现如下功能:使用一个用户名做为参数,如果 指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息
5、编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
6、编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值
1、复制/etc/profile至/tmp/目录,用查找替换命令删除/tmp/profile文件中的 行首的空白字符
(1)复制/etc/profile至/tmp/目录:
cp 命令
解释说明:cp - copy files and directories格式:
cp [OPTION]… [-T] SOURCE DEST
cp [OPTION]… SOURCE… DIRECTORY
cp [OPTION]… -t DIRECTORY SOURCE…
常用选项:
-i 覆盖前提示 //如果不提示的话,会直接覆盖有风险
-n 不覆盖,注意两者顺序
-r, -R 递归复制目录及内部的所有内容
-a 归档,相当于-dR --preserv=all //一般用来备份
-d --no-dereference --preserv=links 不复制原文件,只复制链接名
–preserv[=ATTR_LIST] mode: 权限、ownership: 属主属组、timestamp: 、links、xattr、context、all
-p 等同–preserv=mode,ownership,timestamp
-v --verbose
-f --force // if an existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option is also used)
-u --update 只复制源比目标更新文件或目标不存在的文件
-b 目标存在,覆盖前先备份,形式为 filename~
以下代码示例
[root@centos8 ~]# cp -r /etc/profile /tmp/
cp: overwrite '/tmp/profile'? y
[root@centos8 ~]# cd /tmp/
[root@centos8 tmp]# ls
profile systemd-private-7126ed8258f34f4da6fd0c79513697b7-ModemManager.service-lZXohl vmware-root_830-2999133150 vmware-root_834-2722239005
systemd-private-7126ed8258f34f4da6fd0c79513697b7-colord.service-bZTksj systemd-private-7126ed8258f34f4da6fd0c79513697b7-rtkit-daemon.service-XuS8B4 vmware-root_833-3979642945
(2) 用查找替换命令删除/tmp/profile文件中的 行首的空白字符
进入命令模式输入 %s/^[[:blank:]]\+//g 全局替换
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
if [ -x /usr/bin/id ]; then
if [ -z "$EUID" ]; then
# ksh workaround
EUID=`/usr/bin/id -u`
UID=`/usr/bin/id -ru`
fi
USER="`/usr/bin/id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi
# Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
fi
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
export HISTCONTROL=ignoreboth
else
export HISTCONTROL=ignoredups
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
# By default, we want umask to get set. This sets it for login shell
:%s/^[[:blank:]]\+//g
2、在vim中设置tab缩进为4个字符
Vim 启动时,会根据配置文件(.vimrc)来设置 Vim,因此我们可以通过此文件来定制适合自己的 Vim。
Vim 配置文件分为系统配置文件和用户配置文件:
- 系统配置文件位于 Vim 的安装目录(即全局配置,所有的用户都会执行这个配置)(默认路径为 /etc/.vimrc);
- 用户配置文件位于主目录 ~/.vimrc,即通过执行
vim ~/.vimrc
命令即可对此配置文件进行合理修改。通常情况下,Vim 用户配置文件需要自己手动创建。
设置参数 | 功能描述 |
---|---|
set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936 set termencoding=utf-8 set encoding=utf-8 | 设置编码格式,encoding 选项用于缓存的文本、寄存器、Vim 脚本文件等;fileencoding 选项是 Vim 写入文件时采用的编码类型;termencoding 选项表示输出到终端时采用的编码类型。 |
set nu set number | nu 是 number 的缩写,所以上面两个配置命令是完全等效的,二选一即可。取消行号可使用 set nonu。 |
set cursorline | 突出显示当前行。 |
set mouse=a set selection=exclusive set selectmode=mouse,key | Vim 编辑器里默认是不启用鼠标的,通过此设置即可启动鼠标。 |
set autoindent | 设置自动缩进,即每行的缩进同上一节相同。 |
set tabstop=4 | 设置 Tab 键宽度为 4 个空格。 |
通过以上方式,可以对用户配置文件(.vimrc)进行编辑,进而实现对 Vim 的永久自定义。
以下是题目示例
[root@centos8 ~]# vi .vimrc
set tabstop=4
3、20分钟内通关vimtutor(可参考https://yyqing.me/post/2017/2017-02-22-vimtutor-chinese-summary)
已完成
4、编写脚本 createuser.sh,实现如下功能:使用一个用户名做为参数,如果 指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息
此处以jhonson为用户名进行判断,如果打包存在则提示"此用户已存在",否则创建此用户,并将创建用户的相关id,所属组等信息打印出来.
[root@centos8 ~]# vim /data/creatuser.sh
#!/bin/bash
#
#****************************************************************
#Author: jhonson
#Date: 2021-08-15
#Filename: /data/creatuser.sh
#Copyright (C): 2021 All rights reserved
#****************************************************************
if grep "jhonson" /etc/passwd
then echo "此用户已存在"
else
useradd jhonson
grep "jhonson" /etc/passwd
fi
[root@centos8 ~]# bash /data/creatuser.sh
jhonson:x:1003:1003::/home/jhonson:/bin/bash
[root@centos8 ~]# bash /data/creatuser.sh
jhonson:x:1003:1003::/home/jhonson:/bin/bash
此用户已存在
[root@centos8 ~]# vim /data/creatuser.sh
[root@centos8 ~]# bash /data/creatuser.sh
jhonson:x:1003:1003::/home/jhonson:/bin/bash
此用户已存在
5、编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
[root@centos8 ~]# vim /data/systeminfo.sh
#!/bin/bash
#
#****************************************************************
#Author: jhonson
#Date: 2021-08-15
#Filename: /data/systeminfo.sh
#Copyright (C): 2021 All rights reserved
#****************************************************************
BEGINCOLOR="\e[1;33m"
ENDCOLOR="\e[0m"
echo -e "My hostname is ${BEGINCOLOR}`hostname`$ENDCOLOR"
echo -e "IP address is ${BEGINCOLOR}`ifconfig ens33 |grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}'|head -n1`$ENDCOLOR"
echo -e "OS version is ${BEGINCOLOR}`cat /etc/redhat-release`$ENDCOLOR"
echo -e "Kernel version is ${BEGINCOLOR}`uname -r`$ENDCOLOR"
echo -e "CPU type is ${BEGINCOLOR}`lscpu|grep "Model name" |cut -d: -f2 |tr -s " "`$ENDCOLOR"
echo -e "Memtotol is ${BEGINCOLOR}`cat /proc/meminfo |head -n1 |grep -Eo '[0-9]+.*'`$ENDCOLOR"
echo -e "Disk space is ${BEGINCOLOR}`lsblk |grep 'sda\>'|grep -Eo '[0-9]+[[:upper:]]'`$ENDCOLOR"
[root@centos8 ~]# bash /data/systeminfo.sh
My hostname is centos8.1johnson
IP address is 10.0.0.201
OS version is CentOS Linux release 8.3.2011
Kernel version is 4.18.0-240.el8.x86_64
CPU type is AMD Ryzen 5 1400 Quad-Core Processor
Memtotol is 1833004 kB
Disk space is 200G
6、编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值
#!/bin/bash
#
#****************************************************************
#Author: jhonson
#Date: 2021-08-15
#Filename: /disk.sh
#Copyright (C): 2021 All rights reserved
#****************************************************************
COLOR='\e[1;36m'
END='\e[0m'
MAX=`df|egrep '^/dev/sd'|egrep -o '([0-9]+)%' |tr -d '%'|sort -nr|head -1`
echo -e "${COLOR}The maximum value of space is $MAX in the hard disk partition.${END}"
~
~
~
[root@centos8 ~]# bash /disk.sh
The maximum value of space is 24 in the hard disk partition.