变量

1、环境变量(全局变量)

  3个命令显示变量值:set、env、declare

env

[root@web02-7 log]# env |tail
LANG=en_US.UTF-8
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
LOGNAME=root
CVS_RSH=ssh
SSH_CONNECTION=10.0.0.1 57993 10.0.0.7 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
G_BROKEN_FILENAMES=1
_=/bin/env

declare

[root@web02-7 log]# declare|tail
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHLVL=1
SSH_CLIENT='10.0.0.1 57993 22'
SSH_CONNECTION='10.0.0.1 57993 10.0.0.7 22'
SSH_TTY=/dev/pts/1
TERM=xterm
UID=0
USER=root
_=
colors=/etc/DIR_COLORS

set -o

[root@web02-7 log]# set -o|head
allexport      	off
braceexpand    	on
emacs          	on
errexit        	off
errtrace       	off
functrace      	off
hashall        	on
histexpand     	on
history        	on
ignoreeof      	off

  

2、普通变量(局部变量)

[root@web02-7 log]# ll /etc/profile
-rw-r--r-- 1 root root 1822 Feb 20 10:49 /etc/profile
[root@web02-7 log]# ll /etc/bashrc 
-rw-r--r--. 1 root root 2681 Oct  2  2013 /etc/bashrc
[root@web02-7 log]# ll /etc/profile.d/
total 52
-rw-r--r--. 1 root root 1150 Apr 12  2016 colorls.csh
-rw-r--r--. 1 root root 1179 Apr 12  2016 colorls.sh
-rw-r--r--. 1 root root   92 Nov 22  2013 cvs.csh
-rw-r--r--. 1 root root   78 Nov 22  2013 cvs.sh
-rw-r--r--. 1 root root  192 Jan 21  2016 glib2.csh
-rw-r--r--. 1 root root  192 Jan 21  2016 glib2.sh
-rw-r--r--. 1 root root 1745 May 12  2016 lang.csh
-rw-r--r--. 1 root root 2706 May 12  2016 lang.sh
-rw-r--r--. 1 root root  123 Jun  4  2014 less.csh
-rw-r--r--. 1 root root  121 Jun  4  2014 less.sh
-rw-r--r--. 1 root root  105 Jul 24  2015 vim.csh
-rw-r--r--. 1 root root  269 Jul 24  2015 vim.sh
-rw-r--r--. 1 root root  169 May 20  2009 which2.sh

  在登录Linux系统并启动一个bash shell时,默认情况下bash会在若干个文件中查找环境变量的设置。这些文件可统称为系统环境文件。bash检查的环境变量文件的情况取决于系统运行shell的方式。系统运行shell的方式一般有3种:

  1)通过系统用户登录后默认运行的Shell

  2)非登录交互式运行Shell

  3)执行脚本运行非交互式Shell

  用户登录后首先会加载/etc/profile全局环境变量文件,这是linux系统上默认的Shell主环境变量文件。系统上每个用户登录都会加载这个文件。

  加载完/etc/profile全局环境变量文件后,才会执行/etc/profile.d目录下的脚本文件,这个目录下的脚本文件很多。如系统的字符集设置(/etc/sysconfig/i18n)等。

  之后开始运行$HOME/.bash_profile(用户环境变量文件),在这个文件中又会去找$HOME/.bashrc(用户环境变量文件)有执行,没有不执行。在$HOME/.bashrc文件中又会去找/etc/bashrc(全局环境变量文件),如果有执行,如果没有不执行。

  如果用户的Shell不是在登录时启动的,那么这种非登录Shell只会加载$HOME/.bashrc(用户环境变量文件),并会去找/etc/bashrc(全局环境变量文件)。如果希望在非登录Shell下也可以读到设置的环境变量等内容,就需要将变量设定等写入$HOME/.bashrc或者/etc/bashrc,而不是$HOME/.bash_profile或/etc/profile.

 

只有root用户才能执行。

 

 清除/var/log/messages日志

#!/bin/bash
#######
#features:clean massages
#date:2017-2-20
#auto:xusx
#######
LOG_dir=/var/log
ROOT_UID=0
if [ "$UID" -ne "$ROOT_UID" ]
 then
   echo "must be root runing"
   exit 1
fi

cd $LOG_dir || {
  echo "cannot change to necessary directory."
  exit 1
}
cat /dev/null > messages && {
  echo "Log cleaned up."
  exit 0
}
  echo "Log cleaned up fail.."
exit 1

  

posted @ 2017-02-21 13:53  reborn枪  阅读(199)  评论(0编辑  收藏  举报