【CentOS 7筆記30】,Shell介紹和命令歷史#

shallow丿ovehtml


什麼是Shell

  • shell是一個命令解釋器,提供用戶和機器之間的交互
  • 支持特定語法,好比邏輯判斷、循環
  • 每一個用戶均可以有本身特定的shell
  • CentOS 7默認shell爲bash(Bourne Agin Shell)
  • bash(Bourne Again Shell)是sh(Bourne Shell)的加強版
  • 還有zsh、ksh等
[root@localhost ~]# yum list | grep zsh
autojump-zsh.noarch                     22.3.0-3.el7                   epel     
zsh.x86_64                              5.0.2-28.el7                   base     
zsh-html.x86_64                         5.0.2-28.el7                   base     
zsh-lovers.noarch                       0.9.0-1.el7                    epel     
[root@localhost ~]# yum list | grep ksh
ksh.x86_64                              20120801-34.el7                base     
mksh.x86_64                             46-5.el7                       base     
python-XStatic-Rickshaw.noarch          1.5.0.0-4.el7                  epel     
python-moksha-common.noarch             1.2.3-2.el7                    epel     
python-moksha-hub.noarch                1.5.3-2.el7                    epel     
python-moksha-wsgi.noarch               1.2.2-2.el7                    epel

命令歷史

history命令 .bash_history 最大1000條 /etc/profile中修改 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S" 永久保存 chattr +a ~ /.bash_history !! !n !wordpython

history

[root@localhost ~]# ls /root/.bash_history
	/root/.bash_history
[root@localhost ~]# less !$
	less /root/.bash_history
	#最大1000條
[root@localhost ~]# histroy
	#命令歷史
[root@localhost ~]# echo $HISTSIZE
	1000

若使用history查看命令歷史,若顯示超過1000條數,這是臨時暫存於內存,還沒真正寫入文件shell

[root@localhost ~]# history -c
[root@localhost ~]# history 
	   17  history
[root@localhost ~]# cat .bash_history
	#命令歷史

history -c指僅僅把內存中的命令中刪除vim

當只有退出終端時,存於內存的命令纔會寫入文件bash

修改命令歷史最大數量

[root@localhost ~]# vi /etc/profile
	# /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=`id -u`
			UID=`id -ru`
		fi
		USER="`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
	# Current threshold for system reserved uid/gids is 200
	# You could check uidgid reservation validity in
	# /usr/share/doc/setup-*/uidgid file
	if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
		umask 002
	else
		umask 022
	fi

	for i in /etc/profile.d/*.sh ; do
		if [ -r "$i" ]; then
			if [ "${-#*i}" != "$-" ]; then
				. "$i"
			else
				. "$i" >/dev/null
			fi
		fi
	done

	unset i
	unset -f pathmunge

找到HISTSIZE=1000這一行,能夠任意修改數值less

[root@localhost ~]# echo $HISTSIZE
	1000
[root@localhost ~]# source /etc/profile
[root@localhost ~]# echo $HISTSIZE
	5000

修改history

當我輸入history命令時,顯示都是前面一個數字編號,後面加着命令。ide

[root@localhost ~]# history
	   17  history 
	   18  cat .bash_history
	   19  ls -l .bash_history
	   20  ls
	   21  history
[root@localhost ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S  "
[root@localhost ~]# echo $HISTTIMEFORMAT
	%Y/%m/%d %H:%M:%S  
[root@localhost ~]# history
	   17  2017/11/16 03:14:45  history 
	   18  2017/11/16 03:15:20  cat .bash_history
	   19  2017/11/16 03:16:55  ls -l .bash_history
	   20  2017/11/16 03:17:02  ls
	   21  2017/11/16 03:19:07  history
	   22  2017/11/16 03:44:39  HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S  "
	   23  2017/11/16 03:44:42  history

保存配置

當從新打開一個終端時,輸入echo $HISTTIMEFORMAT,則不存在。說明系統環境配置默認是不存在的,如果要求生效則須要修改/etc/profile,將HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S 添加在HISTSIZE=5000後面,則會徹底生效ui

[root@localhost ~]# vi /etc/profile
	# /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=`id -u`
			UID=`id -ru`
		fi
		USER="`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=5000
	HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S  "
	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
	# Current threshold for system reserved uid/gids is 200
	# You could check uidgid reservation validity in
	# /usr/share/doc/setup-*/uidgid file
	if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
		umask 002
	else
		umask 022
	fi

	for i in /etc/profile.d/*.sh ; do
		if [ -r "$i" ]; then
			if [ "${-#*i}" != "$-" ]; then
				. "$i"
			else
				. "$i" >/dev/null
			fi
		fi
	done

	unset i
	unset -f pathmunge
[root@localhost ~]# source /etc/profile
[root@localhost ~]# echo $HISTTIMEFORMAT	#沒有生效,須要從新註銷或從新登陸

永久保存

即便不設置最大數量值,能夠使用下面命令,而後把命令歷史追加就好了,指系統再無權限刪除其餘命令this

[root@localhost ~]# chattr +a ~/.bash_history

若非正常退出主機,則暫存內存的命令則不會記錄於配置文件中idea

!!

兩個!指只取最後一條命令

[root@localhost ~]# w
	 04:23:24 up  1:32,  1 user,  load average: 0.00, 0.01, 0.05
	USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
	root     pts/0     04:07    4.00s  0.06s  0.02s w
[root@localhost ~]# !!
	w
	 04:23:26 up  1:32,  1 user,  load average: 0.00, 0.01, 0.05
	USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
	root     pts/0     04:07    6.00s  0.04s  0.00s w

!n

[root@localhost ~]# history
  996  2017/11/16 03:56:53  echo $HISTTIMEFORMAT
  997  2017/11/16 04:00:30  vim /etc/profile
  998  2017/11/16 04:06:07  echo $HISTTIMEFORMAT
  999  2017/11/16 04:08:11  history
 1000  2017/11/16 04:12:52  chattr +a ~/.bash_history
 1001  2017/11/16 04:23:24  w
 1002  2017/11/16 04:24:57  histroy
 1003  2017/11/16 04:25:00  history
[root@localhost ~]# !1001
	w
	 04:25:41 up  1:34,  1 user,  load average: 0.00, 0.01, 0.05
	USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
	root     pts/0     04:07    5.00s  0.05s  0.00s w

!word

[root@localhost ~]# !echo
	echo $HISTTIMEFORMAT
	%Y/%m/%d %H:%M:%S

匹配最後使用echo的命令

相關文章
相關標籤/搜索