shell 非_Shell基本操作(一)
linux
? 內部命令和外部命令
? Shell不需要啟動一個單獨的進程來運行內部命令
? Shell需要創建(fork)和執行(exec)一個新的子進程來運行外部命令
? 盡量使用內部命令有助于性能的提升
bash-->cd --解析內部命令的流程
bash-->forks-->bash(子進程)-->ls --解析外部命令的流程
# type ls --外部命令
ls is aliased to `ls --color=tty'
# type cd --內部命令
cd is a shell builtin
1、文件名
? 你可以用任何你認為合適的可見字符或不可見字符為文件或目錄命名,但不要給自己找麻煩
? 從安全和方便角度
? 大小寫字符
? 下劃線
? 圓點
? 逗號
? 盡量不要出現“空格”
? 擴展名
? 方便分類,非必須
? 命令通過文件內結構判斷其類型(file)
-type c
File is of type c:
b block (buffered) special
c character (unbuffered) special
d directory
p named pipe (FIFO)
f regular file
l symbolic link; this is never true if the -L option or the -fol-
low option is in effect, unless the symbolic link is broken.
If you want to search for symbolic links when -L is in effect,
use -xtype.
s socket
D door (Solaris)
linux
2、通配符
? ? 任意單個字符,不能匹配空
? * 任意零個或多個字符組(不能匹配以點開頭的文件)
? [ab] a或者b
? [a-z] a到z之間的任意字符,包括端點在內
? 是Shell 而非命令本身處理通配符,命令后的通配符會在命令執行前就被代換了
? 如果需要命令而非Shell處理通配符,請用“"將通配符轉義,跳脫字符
# ll /dev/sd?
brw-r----- 1 root disk 8, 0 03-14 09:03 /dev/sda
# ll /dev/sd*
-rw-r--r-- 1 root root 0 03-14 11:30 /dev/sd
brw-r----- 1 root disk 8, 0 03-14 09:03 /dev/sda
brw-r----- 1 root disk 8, 1 03-14 09:03 /dev/sda1
brw-r----- 1 root disk 8, 2 03-14 09:03 /dev/sda2
brw-r----- 1 root disk 8, 3 03-14 09:03 /dev/sda3
brw-r----- 1 root disk 8, 4 03-14 09:03 /dev/sda4
brw-r----- 1 root disk 8, 5 03-14 09:03 /dev/sda5
# ll /dev/sd?[1-5]
brw-r----- 1 root disk 8, 1 03-14 09:03 /dev/sda1
brw-r----- 1 root disk 8, 2 03-14 09:03 /dev/sda2
brw-r----- 1 root disk 8, 3 03-14 09:03 /dev/sda3
brw-r----- 1 root disk 8, 4 03-14 09:03 /dev/sda4
brw-r----- 1 root disk 8, 5 03-14 09:03 /dev/sda5
# ll /dev/tty[1-5][0-9]
# ll /dev/sda[13]
brw-r----- 1 root disk 8, 1 03-14 09:03 /dev/sda1
brw-r----- 1 root disk 8, 3 03-14 09:03 /dev/sda3
# ll /dev/sda[1-3]
brw-r----- 1 root disk 8, 1 03-14 09:03 /dev/sda1
brw-r----- 1 root disk 8, 2 03-14 09:03 /dev/sda2
brw-r----- 1 root disk 8, 3 03-14 09:03 /dev/sda3
3、轉義
# touch test*?[].sh
4、跳脫字符
# ls
> /root
> a
> /b
5、bash中的引號
雙引號 “ ” :允許通過$符號引用其他變量值,會把引號的內容當成整體來看待
單引號 ‘ ’ :禁止引用其他變量值,shell中特殊符號都被視為普通字符,會把引號的內容當成整體來看待
6、反撇號 ``
$() 在執行命令的過程中會優先執行
; 可對一行命令進行分割,在執行過程中不考慮上一個命令執行是否是正確的
&& 可對一行命令進行分割,在執行過程中考慮上一個命令執行是否是正確的
||
7、! 命令歷史
# echo "This system is "HOSTNAME""
This is system is HOSTNAME
# echo "This system is "$HISTNAME""
This is system is
# echo "This system is "$HOSTNAME""
This is system is desktop8.example.com
# echo 'This system is "$HOSTNAME"'
This is system is "$HOSTNAME"
[root@desktop8 ~]# echo $5.00
.00
[root@desktop8 ~]# echo $5.00
$5.00
# host=`ifconfig eth0 |grep 'inet addr'|cut -d : -f2|cut -d ' ' -f1`
# host2=$(ifconfig eth0 |grep 'inet addr'|cut -d : -f2|cut -d ' ' -f1)
8、運算符
$[]
# a=$((1 + 10))
# echo $a
# a=$[1 + 100]
# echo $a
9、shell的配置文件(軟件+配置文件)
? csh
? $HOME/.cshrc
? 登錄時執行$HOME/.login
? 登出時執行$HOME/.logout
? bash
用戶加載shell配置流程:
# user01-->login-->bash-->/etc/profile-->$HOME/.bash_profile-->$HOME/.bashrc-->/etc/bashrc
全局配置文件:
/etc/profile --bash工作環境的配置(環境變量)
/etc/profile.d/*.sh --/etc/profile的擴展配置文件
/etc/bashrc --bash的配置文件
針對每個用戶的配置文件:
$HOME/.bash_history--存放命令歷史
$HOME/.bash_logout--注銷/退出shell的時候執行的腳本
$HOME/.bash_profile
$HOME/.bashrc
10、命令的編輯
ctrl + u --刪除當前光標至行首內容
ctrl + k --刪除當前光標至行尾內容
ctrl + c --中斷
ctrl + l --清屏
ctrl + a --跳到行首HOME
crtl + e --路到行尾END
ctrl + r --快速搜索history命令
ctrl + z --轉入后臺運行 fg bg
Ctrl + d --退出shell,logout
↑(Ctrl+p) 顯示上一條命令
↓(Ctrl+n) 顯示下一條命令
alias--查詢系統中所有已經存在的別名
alias 別名=‘真名’
unalias--取消系統中的別名
unalias 別名
unalias -a刪除所有的別名
臨時:
alias la='ls -a'
固定:
可以寫至以下文件,定義完成需要使用source來刷新,或者注銷重新登錄用戶:
/etc/profile
$HOME/.bash_profile
$HOME/.bashrc
/etc/bashrc
/etc/profile.d/*.sh
11、命令歷史
HISTSIZE=1000設置命令歷史的條數
history 查詢當前用戶用過的所有命令歷史(內存)
cat $/HOME/.bash_history | grep xx(硬盤)
history -w 同步內存中的命令至硬盤($/HOME/.bash_history)
自動同步:exit/注銷
history -c
echo "" > $HOME/bash_history--清空命令歷史
# :> $HOME/bash_history
調用命令歷史:
!101通過編號
!!調用最后一條命令歷史
!vim調用離我最近一條以vim開頭的命令歷史
!$調用最后一條命令歷史中的參數
命令字 + [選項] + 參數
ls -l /
ctrl + r查找命令歷史
12、bash的特殊符號
< << > >> &> |
總結
以上是生活随笔為你收集整理的shell 非_Shell基本操作(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: clickhouse原理解析与应用实践_
- 下一篇: qt designer 自定义插件找不到