linux shell基礎-1
阿新 • • 發佈:2017-07-03
... tail sea targe 命令解釋 -a keywords %d 管理
- shell
- history
- Tab鍵
- alias
- 通配符
- 重定向功能
shell
shell是一個命令解釋器,提供用戶和機器之間的交互;Shell 是一個用 C 語言編寫的程序,它是用戶使用 Linux 的橋梁。Shell 既是一種命令語言,又是一種程序設計語言。
Shell 是指一種應用程序,這個應用程序提供了一個界面,用戶通過這個界面訪問操作系統內核的服務。
history
作用:bash內建命令,管理命令歷史
語法:
history [n] 不加數字默認列出內存中HISTSIZE參數設定條數的歷史命令
history [參數]
參數:
c:清除目前內存中的命令記錄
"!"的用法:
!n 執行命令歷史中的第n條命令記錄(ps:"!"與"n"之間沒有空格)
!! 執行命令歷史中的最後一條命令
!$ 代表上一條命令裏最後一個參數
!命令keywords 執行以keywords開頭最後一次執行的命令
配置文件:
~/.bash_history history記錄存檔文件 每個用戶家目錄下擁有自己獨立的記錄文件 /etc/profile HISTSIZE參數配置文件 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S" 記錄命令執行的時間 chattr +a ~/.bash_history 永久保存
歷史命令快捷鍵
# ctrl+r,可查詢history中的關鍵字來查找命令
(reverse-i-search)‘cat‘: cat .bash_history |wc -l
## 操作步驟:
# 1. 按下ctrl+r進入搜索界面
# 2. 輸入命令關鍵字,搜索出最近執行的命令
# 3. 按下enter鍵執行此命令
# 4. 按左右鍵可編輯此命令
# 5. 按上下鍵可查看此命令前後輸入的命令歷史
Tab鍵
作用:
命令及文檔名稱補全
使用方法:
輸入命令或文檔名稱頭幾個字母
按一次或兩次Tab鍵即可補全或列出所有符合條件的命令、路徑或文件
實例:
# 命令補全 # 連續按兩次Tab,顯示所有匹配結果 if[Tab][Tab] if ifcfg ifconfig ifdown ifenslave ifnames ifup # 此處依然保留原輸入內容,以便繼續輸入 if # 路徑及文件名稱補全 # 路徑用Tab補全,最後文件名列出匹配結果 ls /etc/syscon[Tab]fig/net[Tab]work-s[Tab]cripts/ifcfg-[Tab][Tab] ifcfg-eth0 ifcfg-lo # 最後依然保留原輸入內容 ls /etc/sysconfig/network-scripts/ifcfg-
alias
作用:
為命令行設置別名
語法:
alias 查詢別名
alias alias_cmd=‘command line‘ 添加別名
配置文件: ~/.bashrc
實例:
# 查詢alias別名
alias
alias cp=‘cp -i‘
alias l.=‘ls -d .* --color=auto‘
alias ll=‘ls -l --color=auto‘
alias ls=‘ls --color=auto‘
alias mv=‘mv -i‘
alias rm=‘rm -i‘
alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘
# 臨時添加alias
alias vi=‘vim‘
which vi
alias vi=‘vim‘
/usr/bin/vim
# 配置文件~/.bashrc,可永久配置alias
vi ~/.bashrc
******************************************************
# User specific aliases and functions
......
alias mv=‘mv -i‘
## 新增alias,並保存退出
alias vi=‘vim‘
......
******************************************************
註:
# "source"或"."重新加載配置文件
source ~/.bashrc
# /etc/profile.d/目錄下可以放置需要開機啟動的shell腳本文件
ls /etc/profile.d/
colorls.csh glib2.csh lang.csh less.csh vim.csh which2.sh
colorls.sh glib2.sh lang.sh less.sh vim.sh
通配符
通配符含義:
* 0個或多個任意字符
? 1個任意字符
# 命令中使用的話跟shell中一樣,後面的東西不生效
實例:
# "*"匹配任意個任何字符
# 匹配1開頭的所有文件
ls 1*
1 12 124.log 13.log
# "?"匹配1個任意字符
# 匹配"1"+"1任意字符"+".log"的文件
ls 1?.log
13.log
# "#"註釋掉其之後的命令
# "#"註釋掉了第二條命令的1*匹配
ls 1? 1*
1 12 12 124.log 13.log
ls 1? #1*
12
重定向功能
作用:
把標準輸出、標準輸入、錯誤輸出重定向至文件或設備
符號及其意義:
> 輸出重定向
>> 輸出追加重定向
< 輸入重定向
<< 輸入追加重定向
1 正確信息標準輸出
2 錯誤信息標準輸出
語法:
1>right.destination 2>error.destination 正確及錯誤信息分別存放
1>right.destination 2>&1 正確及錯誤信息放在一起
2>/dev/null 把錯誤信息輸出到系統黑洞
實例:
# 輸出重定向">"、">>"
# 輸出重定向,然後用追加重定向增加內容
cat /etc/passwd > ./passwd
echo "this is a new line" >> ./passwd
# 用tail命令查看最後三行內容,找到新增內容
tail -3 ./passwd
test:x:500:500::/home/test:/bin/bash
su-test:x:501:501::/home/su-test:/bin/bash
this is a new line
# 追加重定向會覆蓋文件原內容
echo "this is only content will leave" > ./passwd
cat ./passwd
this is only content will leave
# 輸入重定向"<"、"<<"
# cat可以以此形式創建文件並輸入內容,最後ctrl+d退出
cat > stdin
yes , i can do the same thing like vim
now exit by pushing ctrl and d together
# 查看結果
cat stdin
yes , i can do the same thing like vim
now exit by pushing ctrl and d together
# 用文件做標準輸入到stdin
cat > stdin < /etc/passwd
# 查看兩個文件的不同,發現是一致的
diff ./stdin-redirect /etc/passwd
# 把正確和錯誤信息放在一個文件中記錄
# 下面的語句有正確輸出和錯誤輸出
ll stdin-redirect i_am_not_exist
ls: cannot access i_am_not_exist: No such file or directory
-rw-r--r-- 1 root root 934 May 6 22:37 stdin-redirect
# 將其重定向到1個文件中
ll stdin-redirect i_am_not_exist 1>list.log 2>&1
cat list.log
ls: cannot access i_am_not_exist: No such file or directory
-rw-r--r-- 1 root root 934 May 6 22:37 stdin-redirect
# 把錯誤信息輸出到系統黑洞
# 將錯誤信息或正確信息輸入到黑洞
ls stdddd
ls: cannot access stdddd: No such file or directory
ls stdddd 2> /dev/null
linux shell基礎-1