shell介紹 命令歷史 命令補全和別名 通配符 輸入輸出重定向

8.1 shell介紹

•shell是一個命令解釋器,提供用戶和機器之間的交互html

• 支持特定語法,好比邏輯判斷、循環linux

• 每一個用戶均可以有本身特定的shellshell

• CentOS7默認shell爲bash(Bourne Agin Shell)vim

• 還有zsh、ksh等centos

查看當前用戶shell:

1.env命令bash

SHELL=/bin/zshspa

2.echo $SHELL.net

➜ ~ echo $SHELLrest

/bin/zshhtm

zsh和ksh安裝:

[root@localhost ~]# yum list | grep zsh

zsh.x86_64 5.0.2-25.el7_3.1 updates

zsh-html.x86_64 5.0.2-25.el7_3.1 updates

[root@localhost ~]# yum install -y zsh

[root@localhost ~]# yum list |grep ksh

ksh.x86_64 20120801-26.el7 base

mksh.x86_64 46-5.el7 base

[root@localhost ~]# yum install -y ksh

查看linux系統中支持哪些shell:

[root@localhost ~]# cat /etc/shells

/bin/sh

/bin/bash

/sbin/nologin

/usr/bin/sh

/usr/bin/bash

/usr/sbin/nologin

/bin/zsh

/bin/ksh

/bin/rksh

修改當前用戶shell:

1.chsh命令

[root@localhost]~# chsh

Changing shell for root.

New shell [/usr/bin/zsh]: /bin/bash

Shell changed.

2.vim /etc/passwd

root:x:0:0:root:/root:/bin/zsh 修改第七段用戶的bash

3.usermod -s /bin/zsh root

[root@localhost]~# usermod -s /bin/zsh root

[root@localhost]~# head -n1 /etc/passwd

root:x:0:0:root:/root:/bin/zsh

8.2 命令歷史

• history命令:查看歷史使用命令

• .bash_history:命令歷史存放目錄

• 最大1000條:默認存放1000條

• 變量HISTSIZE:echo $HISTSIZE 產看歷史命令記錄最大值

HIASTSIZE變量的配置在/etc/profile裏

• /etc/profile中修改

• HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

指定變量的模式,顯示命令運行的時間

vim /etc/profile 加到HISTSIZE下面一行

source /etc/profile

1000 2017/06/29 20:09:09 history

1001 2017/06/29 20:13:31 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

1002 2017/06/29 20:13:35 history

• 永久保存 chattr +a ~/.bash_history

• !!

運行上一條命令

• !n

運行第n條命令

• !word

命令歷史中最近的word開頭的命令

history -c 晴空內存中的歷史命令,存命令的.bash_history不會被刪除。

打開終端運行的命令,都是暫存在內存中,只有退出終端時纔會寫入.bash_history文件。

8.3 命令補全和別名

• tab鍵,敲一下,敲兩下

• 參數補全,安裝bash-completion

centos 6中只支持補全命令;

centos 7中支持,須要安裝bash-completion後重啓;

yum install -y bash-completion

init 6

[root@localhost ~]# rpm -qa bash-completion

bash-completion-2.1-6.el7.noarch

[root@localhost ~]# systemctl res

rescue reset-failed restart

[root@localhost ~]# systemctl restart network

network-online.target network.service network.target

[root@localhost ~]# systemctl restart network.service

• alias別名給命令從新起個名字

alias 查看系統中全部的別名文件

• 各用戶都有本身配置別名的文件 ~/.bashrc

• ls /etc/profile.d/

• 自定義的alias放到~/.bashrc

8.4 通配符

•* 任意個任意字符

ls *.txt

• ?一個任意字符

ls ?.txt

• [ ] 方括號中的任意一個字符,或的意思

ls [0-9].txt

ls [a-z].txt

ls [123].txt

ls [A-Z].txt

ls [0-9a-zA-Z].txt

• {} 花括號中的任意一個,也是或的意思,中間用,分開

ls {1,2}.txt

ls {a,c,3}.txt

8.5 輸入輸出重定向

• > 重定向到指定文件,覆蓋已存在文件;命令錯誤時,錯誤信息不重定向,若目標文件存在,則

目標文件重置爲空,目標文件不存在時新建文件。

cat 1.txt >2.txt

• >> 追加劇定向;當命令錯誤時,錯誤信息不追加到文件,目標文件不存在時新建目標文件。

cat 1.txt >> 2.txt

• 2> 錯誤信息重定向到文件;當命令正確時,目標文件存在則重置爲空,目標文件不存在則新

建目標文件。

ls aaa.txt 2>err

• 2>> 錯誤追加劇定向;當命令正確時,目標文件存在則不追加劇定向保持原樣,目標文件不存

在則新建目標文件。

ls aaa.txt 2>>err

• 目標文件參數能夠寫成:>file1 2>file2 正確的輸出重定向到file1,錯誤的輸出重定向到file2

ls [1-3].txt 123.fie >file1 2>file2

• &> 無論正確仍是錯誤都重定向;

• &>> 無論正確仍是錯誤都追加劇定向;

• < 輸入重定向左邊的命令執行

wc -l < 1.txt

相關文章
相關標籤/搜索