1. 程式人生 > >shell基本知識

shell基本知識

shel his hostname shell基礎 歷史記錄 usr dev bin 常用

shell基礎知識
history命令 查看歷史記錄
[root@localhost ~]# history
1 echo $PATH
2 echo $LANG
3 locale
4 locale -a |grep zh
5 locale -a|grep zhhistory -c
6 locale
7 locale -a|grep zh
8 locale
9 yum groupinstall chinese-support
10 locale
echo $HISTSIZE 查看可以記錄的歷史條數
[root@localhost ~]# echo $HISTSIZE
1000
history -c 清空歷史

HISTSIZE 在/etc/profile中定義
HOSTNAME=/usr/bin/hostname 2>/dev/null
HISTSIZE=1000 我們可以修改這個數值,執行source /etc/profile
if [ "$HISTCONTROL" = "ignorespace" ] ; then
export HISTCONTROL=ignoreboth
else
export HISTCONTROL=ignoredups
fi
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 定義時間
[root@localhost ~]# HISTTIMEFORMAT="%Y/%m%d %H:%M:%S "
[root@localhost ~]# echo $HISTTIMEFORMAT
%Y/%m%d %H:%M:%S
[root@localhost ~]# history
1 2018/0111 06:49:18 HISTTIMEFORMAT="%Y/%m%d %H:%M:%S "
2 2018/0111 06:49:41 echo $HISTTIMEFORMAT
3 2018/0111 06:49:58 history
時間永久生效的做法:把定義的時間放到/etc/profile下
#vim /etc/profile
#HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S" 這條語句和#HISTSIZE=1000 放在一起
#:wq
#source /etc/profile
chattr +a ~/.bash_history 命令,記錄永遠保存,不能被刪除,只能追加
!!表示執行上一條指令
[root@localhost ~]# pwd
/root
[root@localhost ~]# !!
pwd
/root
!n 表示執行歷史中的第n條指令
[root@localhost ~]# history
1 2018/01/11 06:49:18 HISTTIMEFORMAT="%Y/%m%d %H:%M:%S "
2 2018/01/11 06:49:41 echo $HISTTIMEFORMAT
3 2018/01/11 06:49:58 history
4 2018/01/11 06:53:55 vim /etc/profile
5 2018/01/11 06:56:34 source /etc/profile
6 2018/01/11 06:57:59 echo $HISTTIMEFORMAT
7 2018/01/11 06:58:06 history
8 2018/01/11 06:58:16 ls
9 2018/01/11 06:58:23 history
10 2018/01/11 07:06:22 chattr +a ~/.bash_history
11 2018/01/11 07:08:34 pwd
12 2018/01/11 07:10:03 history
[root@localhost ~]# !8
ls
!字符串表示歷史執行最近一條以字符串開頭的指令
tab 命令補全
alias 別名命令-把常用的長命令換成短命令
自定義別名 是在.bashrc ls/etc/profile.d/中定義
自定義的alias 放在~/.bashrc/
alias restartnet=‘systmectl restart network.service‘
通配符
表示匹配零個或是多個字符
?匹配一個字符*
[root@localhost ~]# touch {1..5}.txt
[root@localhost ~]# ls
1.txt 3.txt 5.txt filename test.txt
2.txt 4.txt anaconda-ks.cfg test.tar
[root@localhost ~]# touch bb.txt
[root@localhost ~]# touch cc.txt
[root@localhost ~]# ls ?.txt
1.txt 2.txt 3.txt 4.txt 5.txt
[root@localhost ~]# ls [34].txt
3.txt 4.txt
重定向符號

表示輸出重定向
<表示輸入重定向
2>錯誤重定向
>追加重定向
[root@localhost ~]# mkdir /tmp/10
[root@localhost ~]# cd /tmp/10
[root@localhost 10]# echo "123" > 1.txt
[root@localhost 10]# ls
1.txt
[root@localhost 10]# vi 1.txt
[root@localhost 10]# echo "123">> 1.txt
[root@localhost 10]# ls
1.txt
[root@localhost 10]# cat 1.tx
cat: 1.tx: 沒有那個文件或目錄
[root@localhost 10]# cat 1.txt
123
123

[root@localhost 10]# dddddd
-bash: dddddd: 未找到命令
[root@localhost 10]# dddddd 2> 1.txt
[root@localhost 10]# cat 1.txt
-bash: dddddd: 未找到命令
[root@localhost 10]# echo "12345" > 1.txt
[root@localhost 10]# cat 1.txt
12345
正確和錯誤的輸出 指定到一個文件中
[root@localhost ~]# ls {1..3}.txt aaa.txt &>5.txt
[root@localhost ~]# cat 5.txt
ls: 無法訪問aaa.txt: 沒有那個文件或目錄
1.txt
2.txt
3.txt

shell基本知識