1. 程式人生 > 實用技巧 >8.3-Shell指令碼程式設計基礎-案例分析(三)

8.3-Shell指令碼程式設計基礎-案例分析(三)

1.讓所有使用者的PATH環境變數的值多出一個路徑,例如:/usr/local/apache/bin

[root@localhost data]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin   
[root@localhost data]# echo "PATH=$PATH:/usr/local/apache/bin">>/etc/profile.d/path.sh
[root@localhost data]# vim /etc/profile.d/env.sh
[root@localhost data]# source /etc/profile.d/env.sh 
[root@localhost data]# echo $PATH
/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/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)

在 ~/.bashrc中將放入以下命令:
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-eth0'
alias editnet='vim /etc/sysconfig/network-scriptsifcfg-ens33'
[root@localhost ~]#cd /etc/sysconfig/network-scripts/
[root@localhost /etc/sysconfig/network-scripts]#cd ..
[root@localhost /etc/sysconfig]#cd ..
[root@localhost /etc]#cd ..
[root@localhost /]#cdnet
[root@localhost /etc/sysconfig/network-scripts]#

3.任意使用者登入系統時,顯示紅色字型的警示提醒資訊“Hi,dangerous!”

在/etc/profile.d/env.sh 中新增
echo -e "\e[1;31mHi,dangerous ! \e[0m"

4.編寫生成指令碼基本格式的指令碼,包括作者,聯絡方式,版本,時間,描述等

5.編寫使用者的環境初始化指令碼reset.sh,包括別名,登入提示符, vim的設定,環境變數等

[root@localhost ~]#cat .bashrc 
# .bashrc

# User specific aliases and functions

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-eth0'
alias editnet='vim /etc/sysconfig/network-scriptsifcfg-ens33'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi
[root@localhost ~]#cat .vimrc 
set ignorecase
set cursorline
set ts=4
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,"#Author: Kevin.Wen")
 call setline(4,"#Revision: 1.0")
 call setline(5,"#QQ: ????")
 call setline(6,"#Date: ".strftime("%Y-%m-%d"))
 call setline(7,"#FileName: ".expand("%")) 
 call setline(8,"#********************************************************************")
 call setline(9,"")
endif
endfunc
autocmd BufNewFile * normal G