Linux练习(Part6.3:shell脚本编程)


练习

1、让所有用户的PATH环境变量的值多出一个路径,例如:/usr/local/apache/bin

[11:34:22 root@Centos8 ~]#cat /etc/profile.d/pro.sh
#!/bin/bash
#
#********************************************************************
#Author:                    chenchunyu
#QQ:                        2504039287
#Date:                      2021-01-24
#FileName:                 /etc/profile.d/pro.sh
#URL:                       https://www.cnblogs.com/chenchunyuBEyOND/
#Description:          The test script
#Copyright (C):         2021 All rights reserved
#********************************************************************
PATH="$PATH:/usr/local/apache/bin"
[11:35:08 root@Centos8 ~]#. /etc/profile.d/pro.sh
[11:35:15 root@Centos8 ~]#echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/apache/bin:/root/bin:/usr/local/apache/bin

2、用户 root 登录时,将命令指示符变成红色,并自动启用如下别名: rm=‘rm -i’

cdnet=‘cd /etc/sysconfig/network-scripts/’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eth0’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 或 ifcfg-ens33 ’ (如果系统是CentOS7)

[root@Centos8 ~]# cat env.sh
#!/bin/bash
#
#********************************************************************
#Author:                    chenchunyu
#QQ:                        2504039287
#Date:                      2021-01-24
#FileName:                 env.sh
#URL:                       https://www.cnblogs.com/chenchunyuBEyOND/
#Description:          The test script
#Copyright (C):         2021 All rights reserved
#********************************************************************
#用户root登陆时,将命令指示符变成红色,并自启动如下别名:
#rm='rm -i'
#cdnet='cd /etc/sysconfig/network-scripts'
#editnet='vim /etc/sysconfig/network-scripts/ifcfg-eth0'
#editnet='vim /etc/sysconfig/network-scripts/ifcfg-eto16777736 或 ifcfg-ens33'(如果系统是Centos 7)
cat >>~/.bashrc <<EOF                                                                          
PS1='\[\e[1;31m\][\u@\h \W]\\$ \[\e[0m\]'
alias rm='rm -i'
alias cdnet='cd /etc/sysconfig/network-scripts'
alias editnet='vim /etc/sysconfig/network-scripts/ifcfg-ens33'
EOF
[root@Centos8 ~]# alias rm
alias rm='rm -i'
[root@Centos8 ~]# alias cdnet
alias cdnet='cd /etc/sysconfig/network-scripts'
[root@Centos8 ~]# alias editnet
alias editnet='vim /etc/sysconfig/network-scripts/ifcfg-ens33'

3、任意用户登录系统时,显示红色字体的警示提醒信息“Hi,dangerous!”

[12:06:10 root@Centos8 ~]#cat env2.sh
#!/bin/bash
#
#********************************************************************
#Author:                    chenchunyu
#QQ:                        2504039287
#Date:                      2021-01-24
#FileName:                 env2.sh
#URL:                       https://www.cnblogs.com/chenchunyuBEyOND/
#Description:          The test script
#Copyright (C):         2021 All rights reserved
#********************************************************************
#任意用户登录系统时,显示红色字体的警示提醒信息“Hi,dangerous!”
cat >>~/.bash_profile <<EOF
echo -e '\E[1;31mHi,dangerous!\E[0m'
EOF

Last login: Sun Jan 24 12:01:17 2021 from 100.0.0.2
Hi,dangerous!
[12:06:10 root@Centos8 ~]#

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

[12:16:21 root@Centos8 ~]#cat .vimrc
set ts=4
set expandtab
set ignorecase
set cursorline
set autoindent
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
        if expand("%:e") == 'sh'
        call setline(1,"#!/bin/bash")
        call setline(2,"#")
        call setline(3,"#********************************************************************")
        call setline(4,"#Author:                    chenchunyu")
        call setline(5,"#QQ:                        2504039287")
        call setline(6,"#Date:                      ".strftime("%Y-%m-%d"))
        call setline(7,"#FileName:                 ".expand("%"))
        call setline(8,"#URL:                       https://www.cnblogs.com/chenchunyuBEyOND/")
        call setline(9,"#Description:          The test script")
        call setline(10,"#Copyright (C):        ".strftime("%Y")." All rights reserved")
        call setline(11,"#********************************************************************")
        call setline(12,"")
        endif
endfunc
autocmd BufNewFile * normal G
posted @ 2021-01-24 11:30  加油啊坚持啊搞钱啊  阅读(133)  评论(0)    收藏  举报