linux Shell入门:掌握Linux,OS X,Unix的Shell环境

2019-09-23 09:27:24于丽

被Bash shell初始化过程中应用的文件搞糊涂了吗?

下面的"bash初始化文件"流程图应该有些帮助:

根据账户设定的默认shell,你的用户配置或系统配置可能是下面其中一种:

找出zsh shell配置文件

zsh的wiki中建议用下面的命令:

strings =zsh | grep zshrc

示例输出:

/etc/zshrc
.zshrc

输入下面的命令列出你的zsh shell文件:

ls -l /etc/zsh/* /etc/profile ~/.z*

查看所有zsh配置文件:

less /etc/zsh/* /etc/profile ~/.z*

找出ksh shell配置文件

    查看~/.profile或者/etc/profile文件。

找出tcsh shell配置文件

    C shell查看~/.login,~/.cshrc文件。 TC shell查看~/.tcshrc和~/.cshrc文件。

我可以写个类似这样每次登录时都自动执行的脚本吗?

是的,把你的命令或别名或其他设定添加到~/.bashrc(bash shell)或者~/.profile(sh/ksh/bash)或者~/.login(csh/tcsh)文件中。

我可以写个类似这样每次登出都自动执行的脚本吗?

是的,把你的命令或别名或其他设定添加到~/.bash_logout(bash)或者~/.logout(csh/tcsh)文件。

history:获取关于shell会话的更多信息

输入history命令来查看本次会话的历史:

history

示例输出:

  9 ls
  10 vi advanced-cache.php
  11 cd ..
  12 ls
  13 w
  14 cd ..
  15 ls
  16 pwd
  17 ls
  ....
  ..
  ...
  91 hddtemp /dev/sda
  92 yum install hddtemp
  93 hddtemp /dev/sda
  94 hddtemp /dev/sg0
  95 hddtemp /dev/sg1
  96 smartctl -d ata -A /dev/sda | grep -i temperature
  97 smartctl -d ata -A /dev/sg1 | grep -i temperature
  98 smartctl -A /dev/sg1 | grep -i temperature
  99 sensors

输入history 20来查看命令历史的后20条:

history 20

示例输出:

Fig.06: View session history in the bash shell using history command

图6:在bash shell中使用history命令查看会话历史

你可以重复使用之前的命令。简单地按下[上]或[下]方向键就可以查看之前的命令。在shell提示符下按下[CTRL-R]可以向后搜索历史缓存或文件来查找命令。重复最后一次命令,只需要在shell提示符下输入!!就好了:

ls -l /foo/bar
!!

在以上的历史记录中找到命令#93 (hddtemp /dev/sda),输入:

!93

使用sudo或su改变用户

下面是语法:

su userName
 
## 登录为tom用户 ##
su tom
 
## 为用户tom打开一个新的shell会话 ##
su tom
 
## 登录为root用户 ##
su -
 
## sudo命令语法(必须在系统中配置有这个命令) ##
sudo -s
sudo tom