Linux基础

2021/5/23 7:29:40

本文主要是介绍Linux基础,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

第三周作业

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

[root@bogon tmp]# vim /tmp/profile

if [ -r "$i" ]; then
    if [ "${-#*i}" != "$-" ]; then
        . "$i"
    else
        . "$i" >/dev/null
    fi
fi
done

unset i
unset -f pathmunge

if [ -n "${BASH_VERSION-}" ] ; then
        if [ -f /etc/bashrc ] ; then

        #Bash login shells run only /etc/profile
        #Bash non-login shells run only /etc/bashrc
        #Check for double sourcing is done in /etc/bashrc.

               . /etc/bashrc
       fi
fi
:%s/^[[:blank:]]\+//g
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then 
. "$i"
else
. "$i" >/dev/null
fi
fi
done

unset i
unset -f pathmunge

if [ -n "${BASH_VERSION-}" ] ; then
if [ -f /etc/bashrc ] ; then
# Bash login shells run only /etc/profile
# Bash non-login shells run only /etc/bashrc
# Check for double sourcing is done in /etc/bashrc.
. /etc/bashrc
fi
fi

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

三步骤:

1、编辑/etc/vim/vimrc文件

[root@bogon tmp]# vim /etc/vimrc

2、在文件最后添加如下内容:

set ts=4
set expandtab
set autoindent

3、保存退出,使配置文件生效

:wq
[root@bogon tmp]#source /etc/vimrc

3、20分钟内通关vimtutor(可参考https://yyqing.me/post/2017/2017-02-22-vimtutor-chinese-summary)

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

[root@bogon data]# cat /data/createuser.sh 
#!/bin/bash
read -p "please input username:" USERNAME
if `id $USERNAME &> /dev/null`;then
    echo "username is exited,userid is:`id $USERNAME`"
else
`useradd $USERNAME &>/dev/null`
echo "username is created,userid is:`id $USERNAME`"
fi
[root@bogon data]# ./createuser.sh
please input username:zhang
username is created,userid is:uid=1003(zhang) gid=1003(zhang) groups=1003(zhang)
[root@bogon data]# ./createuser.sh
please input username:wang
username is exited,userid is:uid=1002(wang) gid=1002(wang) groups=1002(wang)

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

[root@bogon data]# cat /data/systeminfo.sh 
#!/bin/bash
echo "hostname is:"
echo `hostname`
echo "IPv4 is:"
echo `ifconfig ens33 |tr  -s ' ' :|cut -d: -f3 |head -n 2 |tail -n 1`
echo "os is:"
echo  `cat /etc/redhat-release`
echo "kernel is:"
echo `uname -r`
echo "CPU is"
echo `lscpu|head -n13 |tail -n1`
echo "MEM is:"
echo `free -h |tr -s " " : |cut -d: -f2 |head -n2 |tail -n1`
echo "disk is:"
echo `lsblk|tr -s " " : |cut -d: -f5 |head -n2 |tail -n1`
[root@bogon data]#chmod a+x /data/systeminfo.sh
[root@bogon data]# ./systeminfo.sh 
hostname is:
bogon
IPv4 is:
10.0.0.201
os is:
CentOS Linux release 8.3.2011
kernel is:
4.18.0-240.el8.x86_64
CPU is
Model name: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
MEM is:
1.7Gi
disk is:
200G

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

[root@bogon data]# cat /data/disk.sh 
#!/bin/bash
echo "disk use max:`df -h |grep "^/dev/s"|tr -s " " %| cut -d% -f5| sort -rn |head -n1`"
[root@bogon data]# chmod a+x /data/disk.sh 
[root@bogon data]# ./disk.sh 
disk use max:24


这篇关于Linux基础的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程